AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 858217
Accepted
evenyougreg
evenyougreg
Asked: 2017-06-28 12:05:52 +0800 CST2017-06-28 12:05:52 +0800 CST 2017-06-28 12:05:52 +0800 CST

PowerShell 导出到 CSV/工作流?

  • 772

当服务器被 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

    }

}
powershell
  • 1 1 个回答
  • 871 Views

1 个回答

  • Voted
  1. Best Answer
    Epic Phil
    2017-06-28T13:11:26+08:002017-06-28T13:11:26+08:00

    我认为您的错误源于$pingdaddress = (Test-Connection -ComputerName $thisservername -Count 1 -ErrorAction SilentlyContinue -Verbose).IPV4Address.IPAddressToString. 如果无法解析服务器名称,则返回的连接对象(嗯,Win32_PingStatus 对象)Test-Connection将为$null. 然后,您尝试访问空对象上的属性,这是不允许的。

    我会将“成功”部分分成自己的专栏。您可以通过在 CSV 中添加另一列来做到这一点,例如:NameMatch或IPMatch,任何对您更有意义的内容。这样,您可以将其作为循环中的属性进行访问,$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 一样),脚本将填充它。

    • 5

相关问题

  • 资源锁和电源外壳

  • 脚本 - 如何断开远程桌面会话?

  • 如何限制向通讯组发送到外部地址?

  • Powershell对值与数组的作用不同?

  • Windows Powershell Vim 键绑定

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve