Powershell 脚本如何 运行 在新的 powershell window 中使用启动进程多行代码

Powershell Script how to run multiple lines of code in new powershell window with start-process

我有一个基本的菜单脚本,其中包含用于显示有关特定计算机的信息的选项,当脚本启动时,系统会提示用户输入域管理员权限和计算机名(计算机名保存在 $ComputerName 中),并且按预期工作。

脚本中的所有选项都会打开第二个 window 使用 Start-Process 来执行代码。

示例:

start-process powershell.exe -argument "-noexit -nologo -noprofile -command

我希望脚本中的第一个选项打开一个新的 window、运行

Get-WmiObject Win32_PhysicalMemory -computername $ComputerName

并获取 RAM Partnumber(将其放入 $Partnumber),并让用户可以使用 Google.

在 Chrome 中查找它

我的问题是(除了编码方面的新手)在新 window 中只有第一行代码是 运行,其余代码似乎恢复到主要代码window.

第一个选项如下所示:

start-process powershell.exe -argument "-noexit -nologo -noprofile -command Get-WmiObject Win32_PhysicalMemory -computername $ComputerName"
$ToChrome = Read-Host 'Do you wish to Google the partnumber? Y or N'
if ($ToChrome -eq 'j') {
    $Partnumber = Get-WmiObject Win32_PhysicalMemory -computername $ComputerName | select -expandproperty Partnumber
    Start-Process "chrome.exe" ("https://www.google.com/?q=$Partnumber")
} 

if ($ToChrome -eq 'n') {
    Continue
}

所有 RAM 信息都显示在 "new window" 中,但 "Google it" 的提示显示在主 window 中,有没有办法解决这个问题,以便 "new window" 中的所有代码行 运行?

这是我的第一个 post 顺便说一句。

如果您希望在新 window 中完成所有操作,请创建一个脚本并在对 Start-Process

的一次调用中执行
$script = {
  $ComputerName = $env:COMPUTERNAME  
  $ToChrome = Read-Host 'Do you wish to Google the partnumber? Y or N'
  if ($ToChrome -eq 'j') {
    $Partnumber = (Get-WmiObject Win32_PhysicalMemory -computername $ComputerName | select -expandproperty PartNumber)[0]

    Start-Process "chrome" "https://www.google.com/search?q=$Partnumber"
  } 
}

Start-Process powershell -ArgumentList $script

顺便说一句...使用 google.com/search?q= 将实际开始搜索,而不是打开 chrome 到 google 站点,零件号在搜索框。