Write-host command output

I’m looking to capture the output of the following:

$process = invoke-wmimethod win32_process -name create -cn $ipaddress -ArgumentList “ipconfig /registerdns”

so that I could say "Running invoke-wmimethod win32_process -name create -cn 10.x.x.x -argumentlist “ipconfig /registerdns”

and all i get when i wrote-host $process is:

System.Management.ManagementBaseObject

The output of that command is a WMI object. You’re wanting to output the command itself, which would just be a string. That can be a little bit awkward, but you can cheese it with a script block:

$scriptBlock = { invoke-wmimethod win32_process -name create -cn $ipaddress -ArgumentList "ipconfig /registerdns" }
Write-Host "Running $scriptBlock"
$process = & $scriptBlock

Great, thanks, better! Although its not passing in the variable $ipaddress:

output

You can Concatenate the String maybe!

$scriptBlock = '{ invoke-wmimethod win32_process -name create -cn ' + $ipaddress + ' -ArgumentList "ipconfig /registerdns" }'

That’s going to get ugly. Would be cleaner to just output the IP address on a second line, in my opinion.

Awesome, thanks for all the great feedback gents!

Dave is right, it can get ugly quick, but here is a simple way to do it.

$ipaddress = "mytestserver.domain.local"
$scriptBlock = { invoke-wmimethod win32_process -name create -cn $ipaddress -ArgumentList "ipconfig /registerdns" }
$process = & $scriptBlock
Write-Host "Running: $($scriptBlock -replace '\$ipaddress',$ipaddress)"

Results:

Running:  invoke-wmimethod win32_process -name create -cn mytestserver.domain.local -ArgumentList "ipconfig /registerdns" 

thanks @curtis for following up, love all the different options and that one for sure appears to work