根据服务器状态更新输入CSV文件

oug3syen  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(111)

我的计算机.csv文件:

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

初始运行我的脚本后,
我想在我的电脑.csv文件中添加新的列如下。Server01,Server03,Server06在线,Server02,Server04,Server05离线。

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

在第二次运行我的脚本后,Server02在线。我将像下面这样更新它。

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

会一直这样下去。
脚本:

$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

更新代码:

$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'
2skhul33

2skhul331#

您只需要在循环的每次迭代期间修改$computer对象,然后将其通过管道传输到Export-Csv
还要注意,csv文件中的空白“单元格”将被读取为空字符串,而不是null,因此我修改了if语句以反映这一点

$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'

相关问题