簡體   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