Run remote batch file from Powershell

Rick Mann
Tera Expert

I'm working on a "Run Powershell" Orchestration activity where I need to run a batch file on a remote server.   The command line will be used to add users to our Good Enterprise server.   I can run the following command from the command line of the Good server to add a user:

 

"C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin>GoodLinkAddUser.bat -Url=dom1\joeadmin:password@https://localhost:19005 -GLS=server123 -UserDisplayName="Joe Employee" -PolicySet="DH Default Personal Device Good Mobile Messaging" -LogFile=GoodLinkAddUser.log"


I'm having troubles setting up Powershell to execute the same command.   I've tried:


invoke-expression "C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin>GoodLinkAddUser.bat -Url=dom1\joeadmin:password@https://localhost:19005 -GLS=server123 -UserDisplayName="Joe Employee" -PolicySet="DH Default Personal Device Good Mobile Messaging" -LogFile=GoodLinkAddUser.log"


invoke-command "C:\Program Files (x86)\Good Technology\Good Mobile Control\jre\bin>GoodLinkAddUser.bat -Url=dom1\joeadmin:password@https://localhost:19005 -GLS=server123 -UserDisplayName="Joe Employee" -PolicySet="DH Default Personal Device Good Mobile Messaging" -LogFile=GoodLinkAddUser.log"


With the Invoke-expression, the command is returning the message Parameter set cannot be resolved using the specified named parameters.


With the Invoke-command, the command is returning the message A positional parameter cannot be found that accepts argument 'Joe'


Any assistance with formatting the Powershell command is most appreciated.


Rick



2 REPLIES 2

oprax02
Tera Contributor

Rick,


Have you solved this issue?   I am struggling with a similar issue calling a batch file from network share ex." \\sharename\myscript.bat arg1". I found that if I leave the invoke-expression off the beginning of the line, the script will execute.   The only issue at the moment is that messages output from the script are not captured.



Joe


Rick Mann
Tera Expert

Hey James



Here is what I did to get mine to work.   Powershell seemed to handle any response from the remote executable as an exception even if the executable script was successful.   I was able to get mine working by writing any response from the Invoke-Command to $Out and then echoed the string at the end of the command.



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\userid:pw@https://localhost:19005 -GLS=servername -UserDisplayName="${workflow.scratchpad.uname}" -PolicySet="DH Default Personal Device Good Mobile Messaging" -LogFile=GoodLinkAddUser.log' | Out-String; echo $Out}



Then I take the output and build the following 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;


}




Hope this helps.


Rick