Author Posts

June 21, 2016 at 6:40 pm

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

June 21, 2016 at 6:43 pm

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

June 21, 2016 at 7:08 pm

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

output

June 21, 2016 at 7:54 pm

You can Concatenate the String maybe!

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

June 21, 2016 at 7:56 pm

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

June 21, 2016 at 8:03 pm

Awesome, thanks for all the great feedback gents!

June 22, 2016 at 5:41 am

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" 

June 22, 2016 at 1:39 pm

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