如何在用户输入上重新启动PowerShell程序

时间:2015-12-05 16:30:53

标签: powershell

好的,这是我第一次在这里发帖,第一次写PowerShell。我的学校计算机维护和维修课程要求所有学生创建一个PowerShell脚本来计算计算机内存。我创建的那个计算总物理ram,用户可用的总ram,以及计算机中的模块以及每个模块的数量。但是,在成功编写代码之后,我需要一些建议来调整代码,以便它可以用于我的学校。

我的程序的第一部分打开并讨论每条线的含义,然后是总物理ram,用户可访问的ram,以及卡的设置方式。这导致直接进入关闭程序的文本。我要添加的内容(顺便说一句,我是PowerShell的初学者)是用户重新运行应用程序的一种方法,如果程序中的任何变量都为零(显然计算机有某种类型的ram)电脑正在运行)。现在是Read-Host "Rerun memsrch ('y'/'n')?"

我想要添加的另一件事是用户能够选择代码是用于本地计算机还是远程计算机。然后,用户可以通过IP或计算机名称选择计算机。下面是我现在的代码,所以每个人都可以看到。

# 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)?"

我对HTML 4.01和HTML 5代码也有一些经验,所以我理解构造和参数的基础知识,但除此之外,目前PowerShell的很大一部分都在我的头上,所以不要得到技术原因我不希望我的大脑爆炸。 :P还要注意代码是否适用于运行Windows 8.1的计算机,但也必须与Windows 7兼容。这也不是我班上的成绩,这是额外的功劳。

1 个答案:

答案 0 :(得分:0)

  1. 如果您将代码包装在function中,则可以在需要时再次调用它。例如,如果第二个问题的用户输入是y

  2. 存储计算机名称或IP地址的用户输入,因此您可以使用-ComputerName参数

  3. 在脚本中进行的WMI调用中使用它

    示例代码:

    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
    

    第一次通过后,$computerName将不再是$null

    提示:您不需要在变量中存储字符串以便能够输出它。只需将其写在"print this on the screen"之类的单独行上即可输出。

    有关PowerShell构造和函数的详细信息,请参阅thisthis