简体   繁体   中英

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

I have a basic menu script with options to show info regarding a specific Computer, users are prompted for domain admin rights and Computername (Computername is saved in $ComputerName) when the script is started, and that works as intended.

All the options in the script open a second window with Start-Process, where the code is executed.

example:

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

I want the first option in my script to open a new window, run

Get-WmiObject Win32_PhysicalMemory -computername $ComputerName

and fetch RAM Partnumber (Put it in $Partnumber), and give users the option to look it up in Chrome using Google.

My issue is (besides being a novice at coding) that only the first line of code is run in the new window, the rest of the code seems to revert back to the main window.

The first option looks like this:

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
}

All the RAM info is shown in the "new window" as I want it, but the prompt to "Google it" is shown on the main window, is there a way around this, so that all the lines of code run in the "new window"?

This is my first post btw.

If you want everything to be done in the new window, create a script and execute it in one call to 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

BTW... using google.com/search?q= will actually start the search instead of opening the chrome to google site with part number in the search box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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