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 :
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With