如何在用户输入时重新启动powershell程序

如何在用户输入时重新启动powershell程序,powershell,Powershell,好的,这是我第一次在这里发帖,也是我第一次写PowerShell。我的学校计算机维护和修理课要求所有学生中的一个创建一个计算计算机ram的PowerShell脚本。我创建的一个计算总物理ram、用户可用的总ram、计算机中的模块以及每个模块上的内存。然而,在成功地编写代码之后,我需要一些建议来调整代码,以便它可以用于我的学校 我的程序的第一部分将打开并讨论每一行的含义,接下来是总物理ram、用户可访问ram,然后是卡的设置方式。这就引出了一段文字,上面写着关闭程序。我想添加的内容(顺便说一下,我

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

我的程序的第一部分将打开并讨论每一行的含义,接下来是总物理ram、用户可访问ram,然后是卡的设置方式。这就引出了一段文字,上面写着关闭程序。我想添加的内容(顺便说一下,我是PowerShell的初学者)是,如果程序中的任何变量为零,用户可以重新运行应用程序(因为如果计算机正在运行,显然计算机具有某种ram)。现在它是一个
读取主机“重新运行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兼容。这也不是我班的成绩,这是额外的学分

  • 如果将代码封装在
    函数中
    ,则可以在需要时再次调用它。例如,如果第二个问题的用户输入是
    y

  • 存储计算机名或IP地址的用户输入,以便您可以在脚本中进行的WMI调用中使用它,并使用
    -ComputerName
    参数

  • 示例代码:

    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

    提示:您不需要在变量中存储字符串就可以输出它。只需将其写在单独的行上,如“在屏幕上打印”,它就会被输出


    有关PowerShell结构和功能的详细信息,请阅读和

    1。如果我选择“重新运行”作为“否”,它会自动终止程序并打开命令行吗?2.如果我循环回到计算机名输入所在的原始行,代码是否会再次为同一台计算机运行该行,或者必须重新插入设置才能在该计算机上重新运行测试?3.“将代码包装到函数中”是什么意思。如果您输入的内容不是
    y
    ,程序将终止2。您必须再次输入计算机名。2.我将对我的代码进行编辑,向您展示如何避免再次键入计算机名3。查看我的代码:第一行有一个函数声明,您的代码被
    {}
    包围。无论何时键入函数名
    Show MemoryReport
    ,代码都将运行。因此,现在输入计算机名或ip,或运行本地,我有这个$computerName=Read Host“输入计算机名或IP,或为本地留空”if($computerName-eq$null){if($computerName-eq”“){$computerName=$env:computerName}否,请参阅我的(更新版)示例:测试
    $null
    ,然后请求输入,然后测试空字符串。您可以检查我添加的链接以更好地理解。否。就在询问用户计算机名之前。如果是null,您可以询问,否则您将继续处理。您可能需要输入“由Michael Meli编写的代码”或“作者:Michael Meli”([email address/twitter handle/bitcoin hash]),在脚本中的
    NOTES
    字段中,而不是我该怎么做?请阅读我链接的帮助文件,或查看PowerShell ISE中高级函数片段中的示例