Reputation: 66320
This would write the output to a logfile:
& $Env:WinDir\system32\inetsrv\appcmd.exe >test.log
But what if I wanted to keep the output in a string variable to use it in the email body?
I tried this without any luck..
$test = ""
& $Env:WinDir\system32\inetsrv\appcmd.exe >$test
Write-Host $test
Upvotes: 54
Views: 88976
Reputation: 19
If you are using powershell >= 5.0
The Tee-Object
command supports both variables and files. Reference doc.
& $Env:WinDir\system32\inetsrv\appcmd.exe | Tee-Object -Variable test
Tee-Object
forwards any input further down the pipe, so if you also want to prevent output to console/host you will need to redirect to Out-Null
& $Env:WinDir\system32\inetsrv\appcmd.exe | Tee-Object -Variable test | Out-Null
Upvotes: 1
Reputation: 461
Capturing the output of a executable is as simple as,
$cmdOutput = &"Application.exe" 2>&1
2>&1 - Includes the error stream in the output
Return type of the executable in PowerShell is an array of strings. In case of logging such outputs,
Write-Host $cmdOutput
will output the strings in the array to the output stream separated by spaces
To print them in a string per line fashion, choose
Write-Output $cmdOutput
or
$cmdOutput = &"Application.exe" | Out-String
Write-Host $cmdOutput
Upvotes: 28
Reputation: 301047
You have to do:
$test = & $Env:WinDir\system32\inetsrv\appcmd.exe
If you wanted to redirect error as well, add 2>&1
in the end.
Upvotes: 56