[英]How can I modify this PowerShell script to export both sections to a CSV without cutting off any data?
I got this script from online. 我从网上得到了这个脚本。 Figured at the very least I could figure out how to export the data to a CSV.
至少我能弄清楚如何将数据导出到CSV。 This has proven to be so far past my ability.
事实证明,这远远超出了我的能力范围。 I've tried
Export-Csv
and pipe, but neither have had success. 我已经尝试了
Export-Csv
和pipe,但是都没有成功。
I had one successful attempt but all the data got shoved into 1 column and cut off, the -Width
parameter didn't resolve that either. 我进行了一次成功的尝试,但是所有数据都被塞入1列并切断,
-Width
参数也无法解决该问题。 The rest of the time I just get random information like Length
or what appears to be corrupted data with number and letters jumbled up. 在剩下的时间里,我只会得到诸如
Length
类的随机信息,或者看起来像是损坏的数据,其中数字和字母混杂在一起。
All this script does it run ping
and nslookup
on a list of IPs. 所有这些脚本均在IP列表上运行
ping
和nslookup
。 I want to move it to a CSV file so I can sort the data and find IP's that are empty/not being used anymore to clean up our IP space or even identify problems in our DNS. 我想将其移动到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!"
Simplify, simplify ... 简化,简化...
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.