繁体   English   中英

如何修改此PowerShell脚本以将两个部分都导出到CSV而不切断任何数据?

[英]How can I modify this PowerShell script to export both sections to a CSV without cutting off any data?

我从网上得到了这个脚本。 至少我能弄清楚如何将数据导出到CSV。 事实证明,这远远超出了我的能力范围。 我已经尝试了Export-Csv和pipe,但是都没有成功。

我进行了一次成功的尝试,但是所有数据都被塞入1列并切断, -Width参数也无法解决该问题。 在剩下的时间里,我只会得到诸如Length类的随机信息,或者看起来像是损坏的数据,其中数字和字母混杂在一起。

所有这些脚本均在IP列表上运行pingnslookup 我想将其移动到CSV文件中,以便可以对数据进行排序并查找为空/不再使用的IP,以清理IP空间,甚至确定DNS中的问题。

$InputFile = 'C:\Temp\list.txt'
$addresses = Get-Content $InputFile
$reader = New-Object IO.StreamReader $InputFile
while ($reader.ReadLine() -ne $null) { $TotalIPs++ }
Write-Host ""
Write-Host "Performing nslookup on each address..."    
foreach ($address in $addresses) {
    ## Progress bar
    $i++
    $percentdone = (($i / $TotalIPs) * 100)
    $percentdonerounded = "{0:N0}" -f $percentdone
    Write-Progress -Activity "Performing nslookups" -CurrentOperation "Working on IP: $address (IP $i of $TotalIPs)" -Status "$percentdonerounded% complete" -PercentComplete $percentdone
    ## End progress bar
    try {
        [System.Net.Dns]::Resolve($address) | Select HostName, AddressList
    } catch {
        Write-Host "$address was not found. $_" -ForegroundColor Green
    }
}
Write-Host ""
Write-Host "Pinging each address..."
foreach($address in $addresses) {
    ## Progress bar
    $j++
    $percentdone2 = (($j / $TotalIPs) * 100)
    $percentdonerounded2 = "{0:N0}" -f $percentdone2
    Write-Progress -Activity "Performing pings" -CurrentOperation "Pinging IP: $address (IP $j of $TotalIPs)" -Status "$percentdonerounded2% complete" -PercentComplete $percentdone2
    ## End progress bar
    if (Test-Connection -ComputerName $address -Count 2 -Quiet) {
        Write-Host "$address responded" -ForegroundColor Green 
    } else {
        Write-Warning "$address does not respond to pings"              
    }
}
Write-Host ""
Write-Host "Done!"

简化,简化...

Get-Content $InputFile | ForEach-Object {
    $ip = $_

    try {
        $dns = [Net.Dns]::Resolve($ip)
    } catch {
        Write-Host "$address not found.`n$_"
    }

    New-Object -Type PSObject -Property @{
        'Address'     = $ip
        'Hostname'    = $dns.Hostname
        'AddressList' = $dns.AddressList
        'Online'      = [bool](Test-Connection $ip -Count 2 -Quiet)
    }
} | Export-Csv 'C:\path\to\output.csv' -NoType

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM