1

I am trying to display data within an Excel document where Column A displays the server name and column B displays the .NET version. I'm running into an issue exporting to a .csv because it says that the file path does not exist. I would like some guidance on how I can resolve that issue and how I can display data on the two columns within Excel.

$Servers =
(
"test"
)

foreach ($Server in $Servers)
{

Invoke-Command -ComputerName $Server -ScriptBlock {
Write-Output "$(hostname)"
Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name Version,Release -EA 0 | where { $_.PSChildName -match '^(?!S)\p{L}'} | select PSChildName, Version, Release | Select -ExpandProperty Version | Sort-Object Version | Export-Csv -Path C:\Users\User\Desktop\example.csv

}
Michael
  • 103
  • 11
  • `Export-Csv -Path C:\Users\User\Desktop\example.csv` is inside the `Invoke-Command` scriptblock as it seems. Are you looking to export the result to the local host or to the remote host? – Santiago Squarzon Apr 08 '22 at 20:44
  • @SantiagoSquarzon, I'm looking to export to the local host – Michael Apr 08 '22 at 20:57
  • @SantiagoSquarzon. Is there a better way to add multiple servers and have the script get the .NET version for multiple servers? When I try to run the updated script, it just keeps on going through every server and does not stop – Michael Apr 08 '22 at 21:35
  • There might be a faster way to get the .NET version of a computer ( I mean better than the script block you already have ) however I am not sure regarding this specifically, I just realized I made a mistake with the placement of `Sort-Object` in my answer (see the update). – Santiago Squarzon Apr 08 '22 at 21:39
  • @SantiagoSquarzon, not necessarily referring to a fast way, but I'm saying that the script appears to just be looping the servers and doesn't end when I put multiple servers in the script. – Michael Apr 08 '22 at 21:52
  • I don't see nothing wrong with the code itself, it might be possible that it can't connect to some hosts (the timeout is around 2 minutes) so that might be causing delays. but for sure, the `foreach` loop will be much slower – Santiago Squarzon Apr 08 '22 at 21:58
  • @SantiagoSquarzon, okay, so when it says working on "___", that simply just means that the script is still running and hasn't finished, right? – Michael Apr 08 '22 at 22:03
  • Yes sir, the thing is, since this runs in _parallel_ you would see like a "working on..." for as many hosts as there are in `$computers` (all at the same time probably lol) – Santiago Squarzon Apr 08 '22 at 22:05
  • 1
    @SantiagoSquarzon, I see what you mean. Anyways, thank you for the explanation and the help!! – Michael Apr 08 '22 at 22:07
  • For sure, my pleasure ;) – Santiago Squarzon Apr 08 '22 at 22:08

1 Answers1

1

The main issue is that you're using Export-Csv on the remote hosts since it is inside the Invoke-Command script block, and the likeable error is because the path you are using as export doesn't exist on those hosts.

It's also worth noting that Invoke-Command can run in parallel, -ComputerName as well as -Session can take an array, this removes the need for the foreach loop as well as it is much faster / efficient.

Invoke-Command -ComputerName $servers -ScriptBlock {
    Write-Host "Working on $($env:COMPUTERNAME)..."
    Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
        Get-ItemProperty -Name Version, Release -EA 0 |
        ForEach-Object {
            if($_.PSChildName -notmatch '^(?!S)\p{L}') {
                return # skip this
            }
        
            [pscustomobject]@{
                HostName = $env:COMPUTERNAME
                Version  = $_.Version
            }
        } | Sort-Object Version
} -HideComputerName | Select-Object * -ExcludeProperty RunspaceID |
    Export-Csv -Path C:\Users\User\Desktop\example.csv -NoTypeInformation
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37