Unable to run remote command through Powershell

Rick Mann
Tera Expert

Details : I'm attempting to setup a Run Powershell workflow activity to run a command line utility on a remote server. The WF activity doesn't seem to be connecting to the remote server as desired and allow me to execute the command. It appears to be trying to run against my local midserver.

I can successfully run a "get-wmiobject" against the remote server. I'm also able to open a remote Powershell session from our midserver and execute the command successfully.

Here is the command I am trying to run: C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin\ cmd /c 'GoodLinkAddUser.bat -Url=domain\<username>:<password>@https://localhost:19005 -GLS=<servername> -UserDisplayName="User Name" -PolicySet="DH Default Personal Device Good Mobile Messaging" -LogFile=GoodLinkAddUser.log'

Is there something special I need to add to the workflow activity in order to run the command on the remote server? I've tried Invoke-command and enter-pssession, but no success.

Thank you for any help.

1 ACCEPTED SOLUTION

I finally worked out the Powershell command to run an external executable on a remote machine.


  1. Use the "Invoke-Command" commandlet
  2. Pass in the remote computer name ($computer)
  3. Pass in the credentials for the remote computer ($cred)
  4. Execute you script within -ScriptBlock { }

In the example below, I'm running a batch file on our Good Enterprise Server to add a user



Invoke-Command -ComputerName $computer -credential $cred -ScriptBlock {Set-Location -Path 'C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin\'; $Out = cmd /c 2>&1 'GoodLinkAddUser.bat -Url=domain\username:password@https://localhost:19005 -GLS=servername -UserDisplayName="${workflow.scratchpad.uname}" -PolicySet="Policy name" -LogFile=GoodLinkAddUser.log' | Out-String; echo $Out}



Notes when running external executable within Powershell


  • Powershell will interpret anything returned by the external executable/batch file as an error, which will cause the Run Powershell activity to always fail although the Scriptblock successfully added a user to Good.
  • I worked around this by setting the output to a variable ($out) and then echoing the output at the end of the script.
  • I could then do some further evaluation of the output in order to complete the workflow.


Sample from my Run Powershell Sensor Script:



workflow.scratchpad.results = output;


if(workflow.scratchpad.results.toString().toLowerCase().indexOf('successfully created user') > 0){


  current.work_notes = 'The requester has been successfully setup for Good Messaging and notified of account setup.';


}


else if (workflow.scratchpad.results.toString().toLowerCase().indexOf('good messaging\nenabled') > 0) {


  current.work_notes = 'The requester already Good Messaging enabled.   Notifying Service Desk for further investigation';


}


else{


  current.work_notes = 'The Good setup script encountered an error.   Please investigate further.   \n\nError Log: ' + output;


}



Good PS.png


View solution in original post

7 REPLIES 7

jcraneNYC
ServiceNow Employee
ServiceNow Employee

Hi Rick,



Hope all's well! What's in your ECC queue when you execute this command? The input and output events in the queue could tell us what's happening here.


Thanks, Jon



If I run a simple command like "New-PSSession -ComputerName gb-doc-svv-0555", I will get the following in the ECC input queue, which also writes to the activity log of the task that is running the workflow.   It appears as if WF is successfully establishing a connection to the remote server.



Id Name                       ComputerName       State                 ConfigurationName       Availability


-- ----                       ------------       -----                 -----------------       --------


1 Session1               gb-doc-svv-0555 Opened               Microsoft.PowerShell ...lable


I finally worked out the Powershell command to run an external executable on a remote machine.


  1. Use the "Invoke-Command" commandlet
  2. Pass in the remote computer name ($computer)
  3. Pass in the credentials for the remote computer ($cred)
  4. Execute you script within -ScriptBlock { }

In the example below, I'm running a batch file on our Good Enterprise Server to add a user



Invoke-Command -ComputerName $computer -credential $cred -ScriptBlock {Set-Location -Path 'C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin\'; $Out = cmd /c 2>&1 'GoodLinkAddUser.bat -Url=domain\username:password@https://localhost:19005 -GLS=servername -UserDisplayName="${workflow.scratchpad.uname}" -PolicySet="Policy name" -LogFile=GoodLinkAddUser.log' | Out-String; echo $Out}



Notes when running external executable within Powershell


  • Powershell will interpret anything returned by the external executable/batch file as an error, which will cause the Run Powershell activity to always fail although the Scriptblock successfully added a user to Good.
  • I worked around this by setting the output to a variable ($out) and then echoing the output at the end of the script.
  • I could then do some further evaluation of the output in order to complete the workflow.


Sample from my Run Powershell Sensor Script:



workflow.scratchpad.results = output;


if(workflow.scratchpad.results.toString().toLowerCase().indexOf('successfully created user') > 0){


  current.work_notes = 'The requester has been successfully setup for Good Messaging and notified of account setup.';


}


else if (workflow.scratchpad.results.toString().toLowerCase().indexOf('good messaging\nenabled') > 0) {


  current.work_notes = 'The requester already Good Messaging enabled.   Notifying Service Desk for further investigation';


}


else{


  current.work_notes = 'The Good setup script encountered an error.   Please investigate further.   \n\nError Log: ' + output;


}



Good PS.png


Rick,


I am assuming that your target machine has PSRemoting enabled. I think that is required to successfully execute invoke-command remotely.



How to Run PowerShell Commands on Remote Computers



Please confirm, thanks,


Subash Biswas