How to make a powershell program restart on user input

Michael Meli

Ok, so this is my first time posting on here and my first time writing PowerShell. My school computer maintenance and repair class requested from all the students that one of them create a PowerShell script that calculates a computers ram. The one I created calculates total physical ram, total ram usable by the user, and the modules in the computer and how much is on each module. However, after successfully coding it, I need a bit of advice as to tweaking the code so it can be use for my school.

The first part of my program opens up and talks about what each line means, follows by total physical ram, user accessible ram, and then the way the cards are set up. This leads right into a text that says to close the program. What I want to add in (I am a beginner at PowerShell by the way) is a way for the user to rerun the application if any of the variables from the program come up as zero (cause obviously the computer has ram of some sort if the computer is running). Right now its a Read-Host "Rerun memsrch ('y'/'n')?"

The other thing I want to add in is the ability for the user to select if the code is for the local computer or a distant machine. The user then could select the computer via IP or computer name. Below is the code I have now so everyone can see.

# Mesa Public Schools
$mps="Mesa Public Schools Information Technology Services"
$mps

# User Help
$print="The first section calculates your total physical memory,
        the second line calculates the ram available to the user,
        and the third line shows how the ram is divided up among 
        the ram cards.`n"
$print

#where I want to put a line of code to allow user to select if its local or remote

$ram = get-wmiobject win32_computersystem | select totalPhysicalMemory

Write-Host "Total usable RAM capacity"
$ramOutput = get-wmiobject win32_computersystem | select totalPhysicalMemory | foreach {$_.totalPhysicalMemory}

"RAM: " + "{0:N2}" -f ($ram.TotalPhysicalMemory/1GB) + "GB"
Get-WMIObject -class win32_physicalmemory | Format-Table devicelocator, capacity -a

Write-Host "Summary of System Memory"
Get-WmiObject -class Win32_PhysicalMemory | Measure-Object -Property Capacity -Sum

# Coded BY
$credits="Coded by Michael Meli"
$credits

#where I want to have the code reloop to the part of the code where
#you first select if the computer is local or remote.

Read-Host "Rerun memsrch (y/n)?"

I also have a bit of experience with HTML 4.01 and HTML 5 code, so I understand the basics of constructs and arguments, but aside from that a large part of powershell at the moment is above my head, so don't get to technical cause I don't want my brain to explode. :P Also note that the code if for computers running windows 8.1, but must be compatible with windows 7 as well. This also is not for a grade in my class either, it's extra credit.

sodawillow
  1. If you wrap your code in a function, you will be able to call it again when you want. For instance, if the user input for the second question is y.

  2. Store user input for the computer name or IP address, so you can use it in the WMI calls you make in the script, with the -ComputerName parameter

Example code:

function Show-MemoryReport {

    #...

    #where I want to put a line of code to allow user to select if its local or remote

    #if computer name is null (first pass)
    if($computerName -eq $null) {

        #ask the user
        $computerName = Read-Host "Enter computer name or IP, or leave blank for local"

        #if the string is empty, use the local computer name
        if($computerName -eq "") {
            $computerName = $env:COMPUTERNAME
        }
    }

    $ram = Get-WmiObject -ComputerName $computerName -Class Win32_Computersystem | Select-Object totalPhysicalMemory

    #...

    #where I want to have the code reloop to the part of the code where you first select if the computer is local or remote.

    $rerun = Read-Host "Rerun report (y/n)?"

    if($rerun -eq "y") { Show-MemoryReport }
}

#at first run, make sure computer name will be asked
$computerName = $null

#run report
Show-MemoryReport

After the first pass, $computerName will not be $null anymore.

Tip : you don't need to store a string in a variable to be able to output it. Just write it on a separate line like "print this on the screen" and it will be output.

For more information about PowerShell constructs and functions, you can read this and this

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I make a program that reverse user input of integers in Java?

From Dev

How to make a Haskell program display a preliminary result in response to user input?

From Dev

How do I make a program that reverse user input of integers in Java?

From Dev

How To Make a Python Program Automatically Restart Itself

From Dev

How to make conditional to restart and end program?

From Dev

How to make the program continue/restart when there is an Error?

From Dev

How to make a program restart after an error forces it to stop?

From Dev

How to make a process monitor your program to restart it if it exits

From Dev

How to make a program restart after an error forces it to stop?

From Dev

How can i make C program that scans user's input(text) and save it on a dynamic string

From Dev

How can I make a user input 20 be read by the program as 20% or 0.20

From Dev

How to restart a program

From Dev

How to run a local program with user input in Perl

From Dev

How to open a program using user input

From Dev

How to Make it so that the User ends this program?

From Dev

Trying to make a Simple User input to character output Console program

From Dev

Powershell Parameters: How to read order of user input

From Dev

How to make arrow keys and backspace work correctly when asking input from user in C program using termios.h?

From Dev

How do I make my program check whether user input is within 5,10,15,20, etc of the random integer?

From Dev

How to make an API call based on user input?

From Dev

How to restart a program from a method

From Dev

How to completely restart a python program

From Dev

LLDB Restart process without user input

From Dev

How to give an error msg based on user input in my program?

From Dev

How to slow down my program and getting user input?

From Dev

How to give input values from user to a process run in a program

From Dev

How do I end program with user input in java?

From Dev

How to run program whilst listening for user input in C?

From Dev

How to take user input from HTML form to C program

Related Related

  1. 1

    How do I make a program that reverse user input of integers in Java?

  2. 2

    How to make a Haskell program display a preliminary result in response to user input?

  3. 3

    How do I make a program that reverse user input of integers in Java?

  4. 4

    How To Make a Python Program Automatically Restart Itself

  5. 5

    How to make conditional to restart and end program?

  6. 6

    How to make the program continue/restart when there is an Error?

  7. 7

    How to make a program restart after an error forces it to stop?

  8. 8

    How to make a process monitor your program to restart it if it exits

  9. 9

    How to make a program restart after an error forces it to stop?

  10. 10

    How can i make C program that scans user's input(text) and save it on a dynamic string

  11. 11

    How can I make a user input 20 be read by the program as 20% or 0.20

  12. 12

    How to restart a program

  13. 13

    How to run a local program with user input in Perl

  14. 14

    How to open a program using user input

  15. 15

    How to Make it so that the User ends this program?

  16. 16

    Trying to make a Simple User input to character output Console program

  17. 17

    Powershell Parameters: How to read order of user input

  18. 18

    How to make arrow keys and backspace work correctly when asking input from user in C program using termios.h?

  19. 19

    How do I make my program check whether user input is within 5,10,15,20, etc of the random integer?

  20. 20

    How to make an API call based on user input?

  21. 21

    How to restart a program from a method

  22. 22

    How to completely restart a python program

  23. 23

    LLDB Restart process without user input

  24. 24

    How to give an error msg based on user input in my program?

  25. 25

    How to slow down my program and getting user input?

  26. 26

    How to give input values from user to a process run in a program

  27. 27

    How do I end program with user input in java?

  28. 28

    How to run program whilst listening for user input in C?

  29. 29

    How to take user input from HTML form to C program

HotTag

Archive