我正在尝试创建一个脚本,该脚本会列出几百台机器,我想让它输出三项内容:IP 地址、VMware 版本,当然还有计算机名称。我无法将它们全部整合在一起,因此想寻求帮助。
下面是我输出到 CSV 文件的内容,但我不确定如何让它显示 IP。我尝试使用“ping”的一些不同语法,但脚本会挂在第一台机器上,永远不会通过它。
for /f %%i in (C:\Temp\Computers.txt) do (
echo Software on %%i >> C:\Temp\psinfovmware15.csv
psinfo64 -s \\%%i | find /i "VMware" >> C:\Temp\psinfovmware15.csv
echo IP Address on %%i >> C:\Temp\psinfovmware15.csv
ping \\%%i >> C:\Temp\psinfovmware15.csv
)
excel C:\Temp\psinfovmware15.csv
另外,我发现下面的 powershell 脚本显示了计算机名称和 IP 地址,但我希望它显示 VMware 版本:
$servers = Get-Content "computers.txt"
$report = @()
ForEach ($server in $servers) {
Try {
$tempreport = New-Object PSObject
$IP = ((Test-Connection -ea stop -Count 1 -comp $server).IPV4Address).IPAddresstoString
$tempreport | Add-Member NoteProperty Server $server
$tempreport | Add-Member NoteProperty Status "Up"
$tempreport | Add-Member NoteProperty IP $ip
$report += $tempreport
}
Catch {
$tempreport = New-Object PSObject
$tempreport | Add-Member NoteProperty Server $server
$tempreport | Add-Member NoteProperty Status "Down"
}
}
$report | Export-Csv -NoTypeInformation "report.csv"
如果机器不在网络上,有什么办法可以解决超时问题吗?谢谢!
答案1
当然,您可以从psinfo64.exe
PowerShell 中获取所需的信息,如下所示:
$psinfo64 = psinfo64.exe -s -nobanner \\$server 2>$Null|
Where-Object { $_ -match "VMware" }
以下脚本可能会有所帮助(抱歉,我无法测试上述内容信息系统使用我当前的测试数据集合):
$servers = Get-Content -Path 'D:\bat\files\30852528.txt' |
Where-Object { $_.Substring( 0,1 ) -ne ';' } # refuse comment lines
$report = [System.Collections.ArrayList]::new()
ForEach ($server in $servers) {
$IpTry = Measure-Command -Expression {
$IpTest = Test-Connection -ErrorAction SilentlyContinue -Count 1 -ComputerName $server
}
if ( $null -eq $IpTest ) {
$IpStatus = $Error[0].Exception.InnerException.Message
$psinfo64, $IpV4Addr = [string]::Empty
} else {
$IpStatus = 'On'
if ( $IpTest.Address -eq $IpTest.ProtocolAddress ) {
$IpV4Addr = $IpTest.ProtocolAddress
} else {
$IpV4Addr = $IpTest.IpV4Address.IPAddressToString
}
$IpTest.Dispose()
$psinfo64 = psinfo64.exe -s -nobanner \\$server 2>$Null|
Where-Object { $_ -match "VMware" }
}
$null = $report.Add(
[PSCustomObject]@{
server = $server;
IpV4Addr = $IpV4Addr;
IpTimeMs = $IpTry.TotalMilliseconds;
IpStatus = $IpStatus;
psinfo64 = $psinfo64;
}
)
}
$report #| Export-Csv -NoTypeInformation "report.csv"
输出:
D:\PShell\SF\1011340.ps1
server IpV4Addr IpTimeMs IpStatus ------ -------- -------- -------- google.com 216.58.201.110 115,6865 On 77.75.79.53 77.75.79.53 21,5296 On 192.168.1.1 192.168.1.1 13,8537 On 192.168.1.12 1252,0167 A non-recoverable error occurred during... bmw.com 3999,3424 Error due to lack of resources 160.46.244.131 3999,6292 Error due to lack of resources foo.bar 41,9088 No such host is known
测试数据收集来自这是我的答案。