Powershell Get-Item into Array for Remote Server

AJ Garcia 1 Reputation point
2021-08-05T01:47:38.123+00:00

All, I've got a script that gets the installed Oracle client versions on our server using a Get-ChildItem command inside a script block. The output should list all the different versions found on the server in a nice tablular format with custom headers but instead puts everything in one row instead of two.

$array = @()
invoke-command -Session $s -Scriptblock { foreach-object {
for ($i=0; $i -le 5; $i++) {
($v = Get-ChildItem -Path D:\ -recurse -filter 'oraclient*.dll').VersionInfo | select-object FileVersion, FileName | Out-Null
$Version = $v.VersionInfo.FileVersionRaw
$FileName = $v.VersionInfo.FileName
$item = New-Object System.Object
$item | Add-Member -MemberType NoteProperty -Name "HostName" -Value "$Using:server"
$item | Add-Member -MemberType NoteProperty -Name "Version" -Value "$Version"
$item | Add-Member -MemberType NoteProperty -Name "FileName" -Value "$FileName"
}
$array += $item
Write-Output $array | Format-Table -AutoSize | out-string
exit-pssession
}
}

Here's what the output looks like

HostName Version FileName


MyServer01 18.0.0.0 18.0.0.0 D:\Oracle_18\product\18.0.0\client_64\bin\oraclient18.dll D:\Oracle_18_32\product\18.0.0\client_32\bin\oraclient18.dll

Here's what it should look like

HostName Version FileName


MyServer01 18.0.0.0 D:\Oracle_18\product\18.0.0\client_64\bin\oraclient18.dll
MyServer01 18.0.0.0 D:\Oracle_18_32\product\18.0.0\client_32\bin\oraclient18.dll

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,580 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Rich Matheisen 47,471 Reputation points
    2021-08-05T02:43:44.71+00:00

    Is this what you're trying to accomplish?

    Invoke-Command -Session $s -Scriptblock {
        Get-ChildItem -Path D:\ -recurse -filter 'oraclient*.dll').VersionInfo |
            ForEach-Object{
                [PSCustomObject]@{
                    HostName = $env:COMPUTERNAME
                    Version = $_.VersionInfo.FileVersionRaw
                    FileName = $_.VersionInfo.FileName
                }
    }
    
    0 comments No comments

  2. AJ Garcia 1 Reputation point
    2021-08-05T17:27:59.257+00:00

    Hi Rich, thanks for your suggestion. I'm having trouble getting it to work. How do I add the values for HostName, Version and FileName to the array? Here's the new code:

    $array = @()
    Invoke-Command -Session $s -Scriptblock {
    (Get-ChildItem -Path D:\ -recurse -filter 'oraclient*.dll').VersionInfo | ForEach-Object {[PSCustomObject] @{
    HostName = $env:COMPUTERNAME
    Version = $.VersionInfo.FileVersionRaw
    FileName = $
    .VersionInfo.FileName
    }
    }
    $item = New-Object System.Object
    $item | Add-Member -MemberType NoteProperty -Name "HostName" -Value Hostname
    $item | Add-Member -MemberType NoteProperty -Name "Version" -Value Version
    $item | Add-Member -MemberType NoteProperty -Name "FileName" -Value FileName
    $array += $item
    Write-Output $array | Format-Table -AutoSize | out-string
    exit-pssession
    }

    0 comments No comments

  3. Rich Matheisen 47,471 Reputation points
    2021-08-05T18:07:20.91+00:00

    A few questions for you:

    1. Is it necessary that you use the "-Session $s" instead of "-Computer <hostname-or-fqdn>"?
    2. Are you getting the information from more than one remote computer?
    3. Did you know that a single Invoke-Command can run against multiple remote computers?

    If you want the example code I posted to be loaded into an array, all you need to do is this:

    $array = @()
    $array += Invoke-Command -Session $s -Scriptblock { etc. }
    

    The example code I posted returns a single PSCustomObject that has all three of the properties (populated with values) each time it's run. If you make the above change then you can use "$array | Export-CSV c:\junk\file.csv -NoTypeInfo" when you finish and you'll have the information you want in comma-separated values file.

    0 comments No comments

  4. AJ Garcia 1 Reputation point
    2021-08-09T17:33:43.56+00:00

    Hi Rich, thanks for your help. I finally got this to work....

    $array = @()
    invoke-command -Session $s -Scriptblock { @($array = Get-ChildItem C:\,D:\ -recurse -filter 'oraclient*.dll' | ForEach-Object { $.VersionInfo } `
    | select-object @{Name='HostName';Expression={$env:ComputerName}},@{Name='AppName';Expression={$
    .FileDescription}}, @{Name='Version';Expression={$.FileVersionRaw}}, @{Name='Publisher';Expression={$.CompanyName}}, @{Name='Location';Expression={$_.FileName}})
    $array | Format-Table -AutoSize | out-string
    Exit-Pssession
    }

    I do plan to run this using a list of servers and send the output to a csv file.


  5. AJ Garcia 1 Reputation point
    2021-08-09T23:03:21.02+00:00

    Hi Rich,
    The security on our servers forces me to use a pssession. I was using format-table while I'm testing so that I can visually see the output.
    You're right, I don't need Exit-Pssession. I removed it.
    You're snippet works with a list of servers, but it's adding additional columns in the csv file - "PSComputerName","RunspaceId","PSShowComputerName". How do I make it exclude these headers? Here's the code....

    $servers = Get-Content 'c:\users\me\list.txt'
    foreach ($server in $servers) {
    Invoke-Command -Session $s -Scriptblock {
    Get-ChildItem D:\ -recurse -filter 'oraclient*.dll' |
    ForEach-Object {
    [PSCustomObject]@{
    HostName = $env:ComputerName
    AppName = $.VersionInfo.FileDescription
    Version = $
    .VersionInfo.FileVersionRaw
    Publisher = $.VersionInfo.CompanyName
    Location = $
    .VersionInfo.FileName
    }
    }
    } | Export-CSV -path 'c:\users\me\apps.csv' -NoTypeInformation -Append
    }

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.