It is necessary that PsExec does not close at the end of the PowerShell script

109 views Asked by At

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?

2

There are 2 answers

2
mklement0 On
  • To make the Start-Process call 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 -i from your psexec.exe call; using -i when targeting a remote machine causes the process to run in a window on the remote machine.

  • In order to pause after execution of the psexec command, so you can examine its output (and potential error messages), call psexec via cmd.exe, which allows you to append a pause call.

Therefore, try the following (using a here-string to simplify quoting):

Start-Process -Wait -Verb RunAs cmd -ArgumentList @"
/c "psexec \\$computer -u $computer\$login -p $password -s net localgroup "$group" & pause"
"@

An alternative is to run your PowerShell script itself with elevation, in which case you don't need Start-Process at all and you can call psexec directly, given that child processes launched by elevated processes are elevated too.

#requires -RunAsAdministrator

$computer=Read-Host "Computer"
$login=Read-Host "Login"
$password=Read-Host "Password" -AsSecureString
$group=Read-Host "Group"

# Now that you know the script is running with elevation,
# you can invoke psexec directly.
# This is not only syntactically easier, but is synchronous by
# default and allows you to capture output, if needed.
psexec \\$computer -u $computer\$login -p $password -s net localgroup $group
  • Note the #requires -RunAsAdministrator Requires statement, which makes PowerShell refuse to run the script unless the calling process (PowerShell session) is elevated.

    • A more convenient solution is to make your script self-elevating, i.e. to make it re-invoke itself with elevation on demand - see this answer.
0
jbans On

The solution I got. Displays a list of users and does not close the window.

*You need to run with administrator rights, because NoNewWindow is used instead of Verb Runas.

$computer=Read-Host "Computer"
$login=Read-Host "Login"
$password=Read-Host "Password" -AsSecureString
$group=Read-Host "Group"

Start-Process -Wait -NoNewWindow psexec -ArgumentList "\\$computer -u $computer\$login -p $password -s net localgroup ""$group"""
pause