New-Object Saved as a $variable

This topic contains 10 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 2 years, 5 months ago.

  • Author
    Posts
  • #16631
    Profile photo of H Man
    H Man
    Participant

    Here is my script:

    Invoke-Command -Session $s -ScriptBlock {

    $Result = Get-Hotfix | where {$_.hotfixid -eq 'KB2617858'}
    if ($Result) {

    New-Object PSObject -Property @{Computer = hostname; Installed = $true }

    } else {
    New-Object PSObject -Property @{Computer = hostname; Installed = $false}

    } }

    the output flows right thru the pipeline no problem , i can use select and | it to export-csv and then import-csv into a $variable

    Is there a way to just save the output from the new-object to a $vairiable

  • #16632
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    H Man,

    Are you looking for something like this?

    $Hotfix = 'KB976002'
    
    $Session = New-PSSession -ComputerName localhost
    $Results = Invoke-Command -Session $Session -ScriptBlock {
        $Result = Get-Hotfix -Id $Using:Hotfix
        New-Object -TypeName psobject -Property @{ Installed = ($Result -ne $null) }
    }
    
    $Results | Format-Table -Property PSComputerName, Installed -AutoSize
    
  • #16633
    Profile photo of H Man
    H Man
    Participant

    ill give that a try

  • #16634
    Profile photo of H Man
    H Man
    Participant

    not working! the session i have opened has about 60 computers.

    when i run your code i get an error
    This command cannot find hot-fix on the machine 'localhost'. Verify the input and Run your command again x60 all with the local host error

    In the code im using , im building my object based on if it has it or not so i know the ones that are missing the hotfix

    the script im using works great with pushing out the info i have been export-csv to a file and then $data = import-csv thats how i am building the object now and that way looks great.

    I was just looking fo r a way to get taht pipeline of info and instead of exporting it saving it to a $variable

  • #16635
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    Please share your code based on my example.

  • #16636
    Profile photo of Daniel Krebs
    Daniel Krebs
    Participant

    I've added the ErrorAction parameter to the Get-Hotfix line which will suppress the error message.

    $Hotfix = 'KB976002'
     
    $Results = Invoke-Command -Session $s -ScriptBlock {
        $Result = Get-Hotfix -Id $Using:Hotfix -ErrorAction SilentlyContinue
        New-Object -TypeName psobject -Property @{ Installed = ($Result -ne $null) }
    }
     
    $Results | Format-Table -Property PSComputerName, Installed -AutoSize
    
  • #16637
    Profile photo of H Man
    H Man
    Participant

    $s = New-PSSession -ComputerName (gc c:\computers.txt)
    $Hotfix = 'KB976002'

    $Results = Invoke-Command -Session $S -ScriptBlock {
    $Result = Get-Hotfix -Id $Using:Hotfix
    New-Object -TypeName psobject -Property @{ Installed = ($Result -ne $null) }
    }

    $Results| ft -autosize

  • #16638
    Profile photo of H Man
    H Man
    Participant

    $Result = Get-Hotfix | where {$_.hotfixid -eq 'KB2617858'}
    if ($Result) {

    New-Object PSObject -Property @{Computer = hostname; Installed = $true }

    } else {
    New-Object PSObject -Property @{Computer = hostname; Installed = $false}

    } }

  • #16639
    Profile photo of H Man
    H Man
    Participant

    after the 2nd new-object out-side the icm sricpt block i have been | to export-csv

  • #16640
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    Do you want this object to be in a variable in the remote session (inside the script block that's running with Invoke-Command), or back on your local computer? For the former, assign the result from Invoke-Command to a variable. For the latter, you can either assign the result of the "If" statement to a variable, or put variable assignments for both calls to New-Object:

    $variable = Invoke-Command (etc)
    
    # or
    
    $variable =  if ($Result)
    {
        New-Object (etc)
    }
    else
    {
        New-Object (etc)
    }
    
    # or
    
    if ($Result)
    {
        $variable = New-Object (etc)
    }
    else
    {
        $variable = New-Object (etc)
    }
    
  • #16641
    Profile photo of H Man
    H Man
    Participant

    thanks Dave ! $variable = Invoke-Command (etc) worked like a charm

You must be logged in to reply to this topic.