Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing parameters to PowerShell from bat file

I'm desperately trying to pass parameters from a batch file to a PowerShell script which checks if Windows services are running or not.

Here is my fully operational command:

.\nsrservices.ps1 -cmd running nsrexecd,stisvc

But the problem is the behaviour of the command :

  • In a PowerShell window, the service names are seen as two (or more), thus checking for 'nsrexecd' then checking for 'stisvc'
  • In the batch file, the service names are passed to PowerShell as a unique value, thus checking for 'nsrexecd,stisvc'

Here is my bat file :

powershell.exe -File .\nsrservices.ps1 -cmd running nsrexecd,stisvc
ECHO.%ERRORLEVEL%
exit /B %ERRORLEVEL%

In my PS file, parameters are defined like that :

param([string]$cmd=$(throw "Type d'action manquante !"),[string[]]$srv)

In a log file, I've got these two results (sorry for the French output).

From the PowerShell window:

24/12/2015 09:02:50-Paramètres appelés : cmd=running srv=stisvc nsrexecd
24/12/2015 09:02:50-Début test de l'état running du service : stisvc
24/12/2015 09:02:50-    Début évaluation de l'état du service : stisvc
24/12/2015 09:02:50-    Fin évaluation de l'état du service : stisvc
24/12/2015 09:02:50-Le service tourne
24/12/2015 09:02:50-Fin test de l'état running du service : stisvc
24/12/2015 09:02:50-Début test de l'état running du service : nsrexecd
24/12/2015 09:02:50-    Début évaluation de l'état du service : nsrexecd
24/12/2015 09:02:50-    Fin évaluation de l'état du service : nsrexecd
24/12/2015 09:02:50-Le service tourne
24/12/2015 09:02:50-Fin test de l'état running du service : nsrexecd

Which means that services are tested sequentially

From the .bat file :

24/12/2015 09:04:24-Paramètres appelés : cmd=running srv=nsrexecd,stisvc
24/12/2015 09:04:24-Début test de l'état running du service : nsrexecd,stisvc
24/12/2015 09:04:24-    Début évaluation de l'état du service : nsrexecd,stisvc
24/12/2015 09:04:24-        Le service nsrexecd,stisvc n'existe pas !
24/12/2015 09:04:24-    Fin évaluation de l'état du service : nsrexecd,stisvc
24/12/2015 09:04:24-Fin test de l'état running du service : nsrexecd,stisvc

Which means that the service named 'nsrexecd,stisvc' does not exist.

The comma between my two names seems to cause confusion when using a .bat file

So I'm pulling my last hairs to find a workaround to win against this bloody .bat file.

I've tried quotes, single-quote, -File, -Command, but no success.

Any clue or solution would be very appreciated since my research on this forum hasn't given me something usable.

And of course, after that I would like to call the .bat file with parameters.

like image 491
Denis.A Avatar asked Dec 24 '15 10:12

Denis.A


Video Answer


1 Answers

In your case I'll use :

[parameter(Mandatory=$true,
           ValueFromRemainingArguments=$true)]
[String[]]
$services

Your batch call will look like this

powershell.exe -File .\nsrservices.ps1 -cmd running nsrexecd stisvc

For more details have a look to about_Functions_Advanced_Parameters

like image 50
JPBlanc Avatar answered Oct 15 '22 01:10

JPBlanc