Write-host command output

This topic contains 7 replies, has 4 voices, and was last updated by Profile photo of Duane Haas Duane Haas 5 months, 2 weeks ago.

  • Author
    Posts
  • #44201
    Profile photo of Duane Haas
    Duane Haas
    Participant

    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

  • #44205
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    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
    
    • #44207
      Profile photo of Duane Haas
      Duane Haas
      Participant

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

      output

  • #44209
    Profile photo of Solomon Acquah
    Solomon Acquah
    Participant

    You can Concatenate the String maybe!

    $scriptBlock = '{ invoke-wmimethod win32_process -name create -cn ' + $ipaddress + ' -ArgumentList "ipconfig /registerdns" }'
  • #44211
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

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

  • #44215
    Profile photo of Duane Haas
    Duane Haas
    Participant

    Awesome, thanks for all the great feedback gents!

  • #44283
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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" 
    
  • #44328
    Profile photo of Duane Haas
    Duane Haas
    Participant

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

You must be logged in to reply to this topic.