如何使用powershell获取多台服务器的操作系统名称、.net框架详细信息



我正在尝试使用下面的PowerShell脚本获取多台服务器的操作系统名称和.net框架的详细信息。

$servers = Get-Content -Path "C:UsersvinayDesktopservers.txt"
Foreach ($s in $servers)
{
write-host $s
$s.PSDrive
$s.PSChildName
Add-Content C:UsersvinayDesktopspecs.txt "Specs:"
$OS = (Get-WmiObject Win32_OperatingSystem).CSName
Add-Content C:UsersvinayDesktopspecs.txt "`nOS:$OS"
$Bit = (Get-WMIObject win32_operatingsystem).name
Add-Content C:UsersvinayDesktopspecs.txt "`nOS Bit: $Bit"
$name = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
Add-Content C:UsersvinayDesktopspecs.txt "`nServer Name: $name"
$dotnet = Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where { $_.PSChildName -Match '^(?!S)p{L}'} | Select PSChildName, version
Add-Content C:UsersvinayDesktopspecs.txt "`n.NET VERSION $dotnet"
$release = (Get-ItemProperty "HKLM:SOFTWAREMicrosoftNET Framework SetupNDPv4Full").Release
Add-Content C:UsersvinayDesktopspecs.txt "`nRelease number: $release"
Add-Content C:UsersvinayDesktopspecs.txt "`n----------------------------"
}

但我在文本文件中得到了运行脚本的服务器的详细信息,但没有得到其他服务器的详细情况。

但是,写入主机$s读取文本文件中的所有服务器。请在我做错的地方帮忙。

继续我的评论,您需要在列表中的服务器上执行代码循环,并让代码在该服务器上运行,而不是在您自己的机器上运行脚本。

此外,我将使用代码输出对象,而不是尝试向文本文件添加行,这样您就可以将结果保存为CSV等结构化格式。

尝试

$servers = Get-Content -Path "C:UsersvinayDesktopservers.txt"
$specs = foreach ($s in $servers) {
if (Test-Connection -ComputerName $s -Count 1 -Quiet) {
Write-Host "Probing server $s"
# you may need to add parameter -Credential and supply the credentials 
# of someone with administrative permissions on the server
Invoke-Command -ComputerName $s -ScriptBlock {
$os = (Get-CimInstance -ClassName Win32_OperatingSystem)
Get-ChildItem 'HKLM:SOFTWAREMicrosoftNET Framework SetupNDP' -Recurse |
Get-ItemProperty -Name Version, Release -Erroraction SilentlyContinue | 
Where-Object { $_.PSChildName -Match '^(?!S)p{L}'} | 
ForEach-Object {
[PsCustomObject]@{
'ComputerName'     = $os.CSName
'Operating System' = $os.Caption
'Architecture'     = $os.OSArchitecture
'Net Version'      = [version]$_.Version
'Net Framework'    = $_.PsChildName
'Net Release'      = $_.Release
}
}
}
}
else {
Write-Warning "Computer '$s' cannot be reached.."
}
}
# remove extra properties PowerShell added
$specs = $specs | Select-Object * -ExcludeProperty PS*, RunspaceId
# output on screen
$specs | Format-Table -AutoSize
# output to CSV file you can open in Excel
$specs | Export-Csv -Path 'C:UsersvinayDesktopspecs.csv' -UseCulture -NoTypeInformation

最新更新