-1

My computers.csv file:

name
Server01.contoso.local
Server02.contoso.local
Server03.contoso.local
Server04.contoso.local
Server05.contoso.local
Server06.contoso.local

After initial run my script ,

I want to add new column inside my computers.csv file like below. Server01,Server03,Server06 are online and Server02,Server04,Server05 are offline.

name,appx
Server01.contoso.local,Port opened or computer reachable
Server02.contoso.local,Port closed or computer unreachable
Server03.contoso.local,Port opened or computer reachable
Server04.contoso.local,Port closed or computer unreachable
Server05.contoso.local,Port closed or computer unreachable
Server06.contoso.local,Port opened or computer reachable

After running my script for second time , Server02 is online. I will update it like below.

name,appx
Server01.contoso.local,Port opened or computer reachable
Server02.contoso.local,Port opened or computer reachable
Server03.contoso.local,Port opened or computer reachable
Server04.contoso.local,Port closed or computer unreachable
Server05.contoso.local,Port closed or computer unreachable
Server06.contoso.local,Port opened or computer reachable

it will continue like this.

Script :

$computers = Import-Csv "C:\temp\computers.csv"

$AllResults = foreach ($computer in $computers) {
    try {
      if (($computer.Appx -eq 'Port closed or computer unreachable') -Or ($null -eq $computer.Appx)){
       Invoke-Command -ComputerName $computer.name -ScriptBlock { Get-AppxPackage -AllUsers } -ErrorAction 'Stop' | ForEach-Object {
        [pscustomobject]@{
            "ComputerName" = $computer.Name
            "Appx"         = $_.Name -join ";"
          }
        }
      }
    }
    catch {
        [pscustomobject]@{
            "ComputerName" = $computer.Name
            "Appx"         = "Port closed or computer unreachable"
        }
    }
}

$AllResults | Export-Csv -Path "C:\Temp\MSStoreReport.csv" -NoTypeInformation -Encoding 'UTF8' -Append

UPDATED CODE:

$computers = Import-Csv "C:\temp\computers2.csv"

$AllResults = foreach ($computer in $computers) {
    try {
      if (($computer.appx -eq 'Port closed or computer unreachable') -Or ($null -eq $computer.appx)){
       Invoke-Command -ComputerName $computer.name -ScriptBlock { Get-AppxPackage -AllUsers } -ErrorAction 'Stop' | ForEach-Object {
        [pscustomobject]@{
            "ComputerName" = $computer.Name
            "Appx"         = $_.Name -join ";"
          
          }
        }
        $Computer.Appx = "Port open or computer reachable"
      }
    }
      catch {
            $Computer.Appx = "Port closed or computer unreachable"

        }

  
        }
    


$AllResults | Export-Csv -Path "C:\Temp\MSStoreReport.csv" -NoTypeInformation -Encoding 'UTF8' -Append
$computers | Export-Csv -Path "C:\temp\computers2.csv" -NoTypeInformation -Encoding 'UTF8'
Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • What is the issue? – iRon Apr 21 '23 at 11:48
  • @iRon As I mentioned , After initial run my script , I want to add new column inside my computers.csv file . Server01,Server03,Server06 are online and Server02,Server04,Server05 are offline. My issue : How can we add it? may be `Add-Member` ? – Arbelac Apr 21 '23 at 12:18
  • If you using the condition `if (($computer.Appx ...`, I would assume that the property already exists. Besides, it is important to have the properties aligened which each object (/row) otherwise you might run into display issues, see: [not all properties displayed](https://stackoverflow.com/a/44429084/1701026). There are several ways to do this: create a new `[PSCustomObject]`, `Select-Objext` with a calculated property, `Add-Member` ... – iRon Apr 21 '23 at 14:47

1 Answers1

0

You just need to modify your $computer object during each iteration of the loop and then pipe that to Export-Csv.

Also note that a blank "cell" in a csv file will be read as an empty string, not a null so I have modified the if statement to reflect that

$csvFile = "C:\temp\computers.csv"

$computers = Import-Csv $csvFile


foreach ($computer in $computers) 
{
    try 
    {
        if (($computer.Appx -eq 'Port closed or computer unreachable') -Or ($computer.Appx -eq ""))
        {
            #do something
            $Computer.Appx = "Port open or computer reachable"
        }
    }
    catch 
    {
        $Computer.Appx = "Port closed or computer unreachable"
    }
}

$computers | Export-Csv -Path $csvFile -NoTypeInformation -Encoding 'UTF8'
SE1986
  • 2,534
  • 1
  • 10
  • 29
  • I am getting Exception setting "Appx": "The property 'Appx' cannot be found on this object. Verify that the property exists and can be set." – Arbelac Apr 21 '23 at 06:07
  • @Arbelac can you post your updated code please? – SE1986 Apr 21 '23 at 09:01