繁体   English   中英

Powershell Script 如何使用 start-process 在新的 powershell 窗口中运行多行代码

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

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

脚本中的所有选项都会打开带有 Start-Process 的第二个窗口,代码将在其中执行。

例子:

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

我希望脚本中的第一个选项打开一个新窗口,运行

Get-WmiObject Win32_PhysicalMemory -computername $ComputerName

并获取 RAM Partnumber(将其放入 $Partnumber),并为用户提供使用 Google 在 Chrome 中查找它的选项。

我的问题是(除了是编码新手)只有第一行代码在新窗口中运行,其余代码似乎恢复到主窗口。

第一个选项如下所示:

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 信息都显示在我想要的“新窗口”中,但是“Google it”的提示显示在主窗口上,有没有办法解决这个问题,以便所有代码行都在“新窗户”?

顺便说一句,这是我的第一篇文章。

如果您希望在新窗口中完成所有操作,请创建一个脚本并在一次调用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 网站。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM