How to capture all output from Invoke-Command

Hello,

How do I capture all output and assign it to variable from Invoke-Command like below for example

$b = (Invoke-Command -ScriptBlock {Write-Verbose "333"} -ComputerName localhost -Verbose) 

You can so something like this

$b = Invoke-Command -ScriptBlock {$VerbosePreference='Continue'; Write-Verbose "333"} 4>&1

This works for this simple example but it still fails in actual implementation I’m trying to use it at below

$scriptToExecute = 
{
Import-Module Webadministration
foreach ($apppool in (dir IIS:\AppPools\))
{
$VerbosePreference='Continue'
$appPool | set-itemproperty  -name enable32bitapponwin64 -value $false  -Verbose 
}
}
$b = Invoke-Command -ScriptBlock $scriptToExecute -ComputerName localhost

First it does not allow me to define $VerbosePreference=‘Continue’ outside of foreach loop and secondly it does not allow me to redirect a line
$appPool | set-itemproperty -name enable32bitapponwin64 -value $false -Verbose 4&>1

You don’t need to set your VerbosePreference each time you loop, and you didn’t use all of the example. The key piece you are missing is redirecting the verbose output to standard output (4>&1) so that it is set in the variable.

$scriptToExecute = 
{
$VerbosePreference='Continue'
Import-Module Webadministration
foreach ($apppool in (dir IIS:\AppPools\))
{
$appPool | set-itemproperty  -name enable32bitapponwin64 -value $false  -Verbose 
}
}
$b = Invoke-Command -ScriptBlock $scriptToExecute -ComputerName localhost 4>&1

Your example did not provide any output for $b. It provided Verbose output to output stream but did not store result in variable

What version of PoSh are you running? I’m using 4.0 and this works great for me.

$scriptToExecute = 
{
    $VerbosePreference='Continue'
    Import-Module F:\Personal\Projects\GIT\ADO365Licensing.git\ADO365Licensing.psd1
    Get-ADO365License -Identity user1 -DomainName domain.local -LicenseAttribute customAttribute -Verbose
}
$b = Invoke-Command -ScriptBlock $scriptToExecute 4>&1
Write-Host "-----Content of variable B-----"
$b

Results:

-----Content of variable B-----
VERBOSE: Loading module from path 'F:\Personal\Projects\GIT\ADO365Licensing.git\ADO365Licensing.psd1'.
VERBOSE: Importing function 'Clear-CloudCredential'.
VERBOSE: Importing function 'Compare-ADtoCloudO365License'.
VERBOSE: Importing function 'Get-ADO365License'.
VERBOSE: Importing function 'Register-CloudCredential'.
VERBOSE: Importing function 'Request-CloudCredential'.
VERBOSE: Importing function 'Set-CloudO365License'.
VERBOSE: Finding Domain Controller for domain.local
VERBOSE: Closest active Domain Controller DC1
VERBOSE: Identity Specified user1
VERBOSE: Performing search for user1 on DC1.domain.local
VERBOSE: Checking if multiple licenses are defined for user1@domain.local
VERBOSE: Single license found. Checking if options are defined.
VERBOSE: No options found for License: ENTERPRISEPACK
VERBOSE: Returning CloudUser Object


SamAccountName    : user1
Name              : User 1
Licenses          : @{License=ENTERPRISEPACK}
UserPrincipalName : user1@emaildomain.com
UsageLocation     : US
DistinguishedName : CN=User 1,OU=Normal Users,OU=Users,DC=domain,DC=local

I’m also on PS 4.0 and sample below does not return anything for me

$scriptToExecute = 
{
$VerbosePreference='Continue'
Write-Verbose "dd"
}
$b = Invoke-Command -ScriptBlock $scriptToExecute -ComputerName localhost 4>&1
$b

It returns following

VERBOSE: dd
Content of variable B

Found the problem 4>&1 shall follow after ScriptBlock not after anything else

this works

$b = Invoke-Command -ScriptBlock $scriptToExecute 4>&1 -ComputerName localhost

But this does not

$b = Invoke-Command -ScriptBlock $scriptToExecute -ComputerName localhost 4>&1

hum, try executing without -computername localhost. That’s what I’ve been testing with because -ComputerName localhost errors out on me. Maybe be making the difference. I’m troubleshooting to see why localhost is not working for me.

Nice catch

I spoke too soon, addition of -ComputerName prevents redirection from taking place, the only way it works if syntax does not specify computername which obviously makes Invoke-Command useless

Found a way to do that. Redirection needs to happen within expression itself and it will work

clear
$scriptToExecute = 
{
$VerbosePreference='Continue'
Write-Output "Standard"
Write-Verbose "Verbose" 4>&1
}
$b = Invoke-Command -ComputerName localhost -ScriptBlock $scriptToExecute 
Write-Output "Content of variable B"
$b