I'm trying something like this
for /f "usebackq delims=" %%a in ('
Powershell.exe -ExecutionPolicy Bypass -Command Invoke-Command -ComputerName <AServerNAme> -ScriptBlock { Get-WebAppPoolState AppPoolName }
') do set "value=%%a"
echo What is %value%
I want the value to contain stopping, stopped, starting...etc, but all I can get back is null
Thanks In Advance
CodePudding user response:
Aside from your incorrect use of the 'USEBACKQuotes' option in your For
loop, (because you weren't using them), you also need to define your value
variable using the string value of the object's property, not the object itself.
I would therefore advise that you use something more like this:
Set "value="
For /F Delims^=^ EOL^= %%G In ('
%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -Command
"Invoke-Command -ScriptBlock {(Get-WebAppPoolState AppPoolName).Value}"
2^>NUL') Do Set "value=%%G"
If Defined value Echo What is %value%
You will also note that I have removed the -ExecutionPolicy
option, because that is usually used for running PowerShell scripts (-File
), not for commands (-Command
).