I wrote a PowerShell script to get a list of users in the groups of the local computer. When executed, a CMD opens with the input of variable values. After entering variables, PsExec opens in the second CMD window, and after executing the command, both windows close.
$computer=Read-Host "Computer"
$login=Read-Host "Login"
$password=Read-Host "Password" -AsSecureString
$group=Read-Host "Group"
Start-Process psexec -Verb runAs -ArgumentList "-i \\$computer -u $computer\$login -p $password -s net localgroup ""$group"""
I need CMD with PsExec not to close. So that I can see the list of users. Adding "pause" at the end of the code does not allow you to close only the first CMD window.
The "-NoNewWindow" parameter should prevent the second window from opening. And together with "pouse" it would be a solution. But "-NoNewWindow" doesn't work(
What other options are there? Or how to make "-NoNewWindow" work?
To make the
Start-Processcall synchronous, add-Wait-Verb Runas, i.e. requesting elevation (running with admin privileges) invariably runs the elevated process in a new window - that's why it cannot be combined with-NoNewWindow.Remove
-ifrom your psexec.exe call; using-iwhen targeting a remote machine causes the process to run in a window on the remote machine.In order to pause after execution of the
psexeccommand, so you can examine its output (and potential error messages), callpsexecviacmd.exe, which allows you to append apausecall.Therefore, try the following (using a here-string to simplify quoting):
An alternative is to run your PowerShell script itself with elevation, in which case you don't need
Start-Processat all and you can callpsexecdirectly, given that child processes launched by elevated processes are elevated too.Note the
#requires -RunAsAdministratorRequires statement, which makes PowerShell refuse to run the script unless the calling process (PowerShell session) is elevated.