简体   繁体   English

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

[英]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列表上运行pingnslookup 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.

相关问题 是否有任何 Powershell 脚本,或者如果虚拟机有多个 ip 地址,我如何修改此脚本以将多个 ip 作为 csv 文件导入? - Is there any Powershell script or how can i modify this script to import multiple ips as a csv file if a vm has multiple ip addresses? 如何使用 PowerShell 脚本修改 excel 数据并导出到文本文件? - How to modify excel data and export to text file using PowerShell script? 如何将 Export-CSV 链接到 Send-MailMessage 而不必在 PowerShell 中将 CSV 保存到磁盘? - How can I chain Export-CSV to Send-MailMessage without having to save the CSV to disk in PowerShell? 如何修改此 Powershell 脚本? - How can I modify this Powershell Script? Powershell 如何将脚本修改为硬编码“ | export-csv c:\\temp\\filename.csv -notypeinformation” - Powershell How to Modify Script to Hardcode " | export-csv c:\temp\filename.csv -notypeinformation" 如何在PowerShell中将数据导出到CSV? - How to export data to CSV in PowerShell? 将Powershell脚本导出到CSV - Export powershell script to CSV 在Powershell中,如何将一个表附加到另一个表并导出为CSV? - In powershell, how can I append one table to another and export to CSV? 如何使用export-csv为PowerShell 2附加文件? - How can I append files using export-csv for PowerShell 2? 如何在PowerShell中将多个对象导出到不同的CSV列 - How can I export multiple objects to different CSV columns in PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM