2

I have been struggling getting powershell to run Psexec. It works fine for single line commands. but i need to call SCOM to put the server in maintenance mode through Psexec...

 $output = .\pstools\PsExec.exe \\scomserver-u $user -p $password -w c:\scom -h "c:\scom\agentmm.exe servertoputinmaintenancemode 4 Automated Maintenance Mode Now +45m" 

I have tried to put quotes after every sentence but it doenst help.. I get this error

PsExec v1.98 - Execute processes remotely Copyright (C) 2001-2010 Mark Russinovich Sysinternals - www.sysinternals.com

The handle is invalid.

Connecting to scomservername...Starting PsExec service on scomservername...C onnecting with PsExec service on scomservername...Error communicating with PsExec servi ce on scomservername:

2 Answers 2

2

I wrote the following function to start a process on a remote server:

Function StartRemote { param([string] $computer = $(Read-Host "Computername"), [string] $cmd = $(Read-Host "Commandline")) $remoteProcess = "" | Select-Object ProcessID, ComputerName, ` ReturnValue, ReturnMsg, Cmd $remoteProcess.ComputerName = $computer $remoteProcess.Cmd = $cmd $mc = New-Object System.Management.ManagementClass ` "\\$computer\root\cimv2:Win32_Process" $result = $mc.create($cmd) $remoteProcess.ReturnValue = [int]$result.ReturnValue $remoteProcess.ProcessID = $result.ProcessId switch($remoteProcess.ReturnValue) { 0 { $remoteProcess.ReturnMsg = "OK" } 2 { $remoteProcess.ReturnMsg = "Access denied" } 3 { $remoteProcess.ReturnMsg = "Insufficient privilege" } 8 { $remoteProcess.ReturnMsg = "Unknown failure" } 9 { $remoteProcess.ReturnMsg = "Path not found" } 21 { $remoteProcess.ReturnMsg = "Invalid parameter" } } return $remoteProcess } 

It requires the computername and your commandline as a string, be sure to escape additional quotes, and returns a custom object with info about your process:

PoSH> StartRemote -computer TEST-VM -cmd "gpupdate /force" ProcessID : 6992 ComputerName : TEST-VM ReturnValue : 0 ReturnMsg : OK Cmd : gpupdate /force 

Perhaps you can modify it to allow alternative credentials. Hope this is helpful.

1

You may want to make sure the same command line works fine in cmd.exe. Powershell has slightly different arguments parsing rules for a command line.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.