Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

Invoke ifconfig inside a script which opens multiple PowerShell's windows

Goal: executing commands (ifconfig, pwd etc.), which usually are written in PowerShell console, in a script that calls multiple PowerShell's windows.

E.g. I'd like to invoke the command ifconfig inside multiple PowerShell's windows by using a script; to do that I tried to write the following code:

$i=1
for(; $i -le 2; $i++)
{
    Start-Process powershell.exe
    Invoke-Expression -Command "ipconfig"
}

When I executed the above code in PowerShell ISE, the result is two powershell's windows, but inside them there isn't the output of the command ifconfig.

question from:https://stackoverflow.com/questions/65905181/invoke-ifconfig-inside-a-script-which-opens-multiple-powershells-windows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is you're not running any commands at all in the new shells, you're just starting them and then independently doing "ipconfig" in your current session. To do what you want you need to pass the commands as arguments to the new PowerShell processes.

$i=1
for(; $i -le 2; $i++)
{
    Start-Process powershell.exe -ArgumentList '-NoExit','-Command ipconfig'
}

The -NoExit part is necessary if you want the PowerShell windows to stay open, without it they'll close after running ipconfig.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...