Powershell

PowerShell 導出到 CSV/工作流?

  • June 27, 2017

當伺服器被 ping 時,將結果地址與庫存進行比較,以使庫存保持最新。

我試圖在下一個相應的單元格中將結果標記為“好”或“壞”。除了我設置的工作流程結果總是“糟糕”之外,它有點工作。

CSV 包含從 excel 清單中提取的伺服器名稱和 IP 地址,格式如下:

name,ipaddress
server1,10.1.24.51
server2,10.1.24.52
server3,10.1.24.53
server4,10.1.27.101
server5,10.1.27.102 <--- purposely wrong IP address for testing

目前腳本:

$serverlist = Import-Csv -Path file.csv

ForEach ($server in $serverlist) {

   $thisservername = $server.name
   $thisserveripaddress = $server.ipaddress

   $pingdaddress = (Test-Connection -ComputerName $thisservername -Count 1 -ErrorAction SilentlyContinue -Verbose).IPV4Address.IPAddressToString

   if ($pingdaddress -ne $thisserveripaddress) {

       #$thisservername + " bad"
       $serverlist | Select-Object name,ipaddress, @{Name='connection';Expression={'bad'}} | `
       Export-Csv -Path file.csv -NoTypeInformation

   } else {

       #$thisservername + " good"
       $serverlist | Select-Object name,ipaddress, @{Name='connection';Expression={'good'}} | `
       Export-Csv -Path file.csv -NoTypeInformation

   }

}

我認為您的錯誤源於$pingdaddress = (Test-Connection -ComputerName $thisservername -Count 1 -ErrorAction SilentlyContinue -Verbose).IPV4Address.IPAddressToString. 如果無法解析伺服器名稱,則返回的連接對象(嗯,Win32_PingStatus 對象)Test-Connection將為$null. 然後,您嘗試訪問空對像上的屬性,這是不允許的。

我會將“成功”部分分成自己的專欄。您可以通過在 CSV 中添加另一列來做到這一點,例如:NameMatchIPMatch,任何對您更有意義的內容。這樣,您可以將其作為循環中的屬性進行訪問,$server.NameMatch並稍後對數據進行過濾/排序。

function Test-ServerNameMapping
{

   [cmdletBinding()]
   param(
       [Parameter(Mandatory=$true)]
       [ValidateScript({Test-Path $_})]
       [String]
       $Path
   )

   $serverList = Import-Csv -Path $Path

   foreach ($server in $serverList)
   {
       Write-Verbose "Testing: $($server.Name), $($server.IPAddress)"

       $connectionObject = (Test-Connection -ComputerName $server.Name -Count 1 -ErrorAction SilentlyContinue)

       if (-not $connectionObject)
       {
           Write-Verbose "Failed to resolve $($server.Name) to an IP address."
           $server.namematch = $false
       }
       else
       {
           $resolvedAddress = $connectionObject.IPV4Address.ToString()
           Write-Verbose "Resolved $($server.Name) to $resolvedAddress"

           if ($resolvedAddress -eq $server.IPAddress)
           {
               Write-Verbose "$($server.IPAddress) matches found address of $resolvedAddress"
               $server.namematch = $true
           }
           else
           {
               Write-Verbose "$($server.IPAddress) does not equal found address of $resolvedAddress"
               $server.namematch = $false
           }
       }
   }

   $serverList | Export-Csv -Path $Path -NoTypeInformation -Force

}

然後,如果您想稍後生成報告,您可以執行以下操作:

$problemServers = Import-Csv -Path c:\example.csv | ? NameMatch -eq $false

範例.csv:

Name,IPAddress,NameMatch
server1,10.0.0.1,"True"
server2,10.0.0.2,
server3,10.0.0.3,"False"

首次執行腳本時,NameMatch 列可以為空(與 server2 一樣),腳本將填充它。

引用自:https://serverfault.com/questions/858217