2

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.

3
  • 1
    You might want to put "Coded by Michael Meli" or "Author: Michael Meli ([email address/twitter handle/bitcoin hash])" in the NOTES field of a comment-based help section in your script instead Commented Dec 5, 2015 at 17:05
  • how would i do that? Commented Dec 5, 2015 at 17:26
  • Read the help file I linked, or see the examples in the advanced function snippets in PowerShell ISE Commented Dec 5, 2015 at 17:29

1 Answer 1

1
  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

Sign up to request clarification or add additional context in comments.

5 Comments

1.If I select rerun as no, will it automatically terminate the program and bring up the command line? 2. If I loop back to the original line where computer name imput is located, will the code rerun the line for the same computer again or does the setting have to be reinserted in order to rerun a test on that computer? 3. What do you mean by "wrapping my code in a function"?
1. if you input anything that is not y the program will terminate 2. you will have to enter the computer name again. 2. I'll make an edit to my code to show you how you can avoid typing the computer name again 3. see my code : there is a function declaration on the first line, and your code is surrounded with {}. Whenever you type the function name Show-MemoryReport, the code will be run.
so right now for entering the computer name or ip, or running local, i have this. $computerName = Read-Host "Enter computer name or IP, or leave blank for local" if($computerName -eq $null) { if($computerName -eq "") { $computerName = $env:COMPUTERNAME }
No, see my (updated) example : test for $null, then ask for input, then test for empty string. You can check the links I have added to understand better
No. Just before asking the user for the computer name. If it is null, you ask, else you go on with the processing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.