【问题标题】:Powershell - Need help exporting IP address from test-connection into CSVPowershell - 需要帮助将 IP 地址从测试连接导出到 CSV
【发布时间】:2020-02-06 00:29:22
【问题描述】:

我有一个脚本来检查 Windows 7 计算机的活动目录,ping 它们并报告 IP 地址。

export-csv 函数写入正确的字段和数据,但 IP 地址除外。 IP 地址显示为“Microsoft.ActiveDirectory.Management.ADPropertyValueCollection”

脚本副本

Function Get-Win7 {
        $Win7list = get-adcomputer -filter {(operatingsystem -like "Windows 7 Professional") -and (enabled -eq "True")} -properties operatingsystem, lastlogondate 
        foreach ($pc in $Win7list){
            $pingtest = test-connection $pc.name -erroraction silentlycontinue -errorvariable pingfail
            if (get-variable -name pingfail -erroraction silentlycontinue) {
                if ($pingfail.exception -match "failed"){
                 $IP3 = "No IP"
                 $pingfall = $null
                }       
            }
            $pc.IP = New-Object -TypeName PSCustomObject -Property @{IPAddress = $pingtest.IPV4Address.ToString | select-object -first 1}
            $PC |select-object name,operatingsystem, lastlogondate, IP |sort-object lastlogondate | export-csv -path c:\users\{user}\desktop\Win7-GSN.csv -notypeinfo -append
        }
}

感谢任何帮助。

【问题讨论】:

  • 仍在学习和解决这个 PS 问题 :) 我读到 new-object 将允许我将字符串传递到 export-csv。你能这么好心输入可行的语法吗?

标签: powershell export-csv


【解决方案1】:

您可以使用Select-Object 中的计算属性,它允许您添加自定义属性并为其赋予自定义值。在这里,我们可以添加一个自定义属性IP 并给它IPV4Address

$PC | Select-Object name,operatingsystem, lastlogondate, @{n='IP';e={$pingtest[0].IPV4Address}}

因此,您可以完全删除 $pc.IP = ... 行。

【讨论】:

  • 非常感谢。同等欣赏语法和解释:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多