优化 Powershell 脚本以查询远程服务器 OS 版本
Optimizing Powershell Script to Query Remote Server OS Version
我想优化一个简单的任务:将服务器 OS 版本拉成整洁的 table。但是,我们环境中的某些服务器禁用了 Powershell。在下面填写找到我的脚本,它有效!但是,每台服务器大约需要 20 秒左右,因为它会等待服务器 return 调用命令的结果,然后再移动到列表中的下一台服务器。我知道有一种方法可以从 PS 命令中异步提取结果,但是当我需要为无法处理 PS 的服务器求助于 cmd 行语法时,这是否可行,如 catch 中所示声明?
$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$Array = @()
$servers = get-content $referencefile
foreach ($server in $servers){
#attempt to query the server with Powershell.
try{
$os_version = invoke-command -ComputerName $server -ScriptBlock {Get-ComputerInfo -Property WindowsProductName} -ErrorAction stop
$os_version = $os_version.WindowsProductName
} # If server doesnt have PS installed/or is disabled, then we will resort to CMD Prompt, this takes longer however.. also we will need to convert a string to an object.
catch {
$os_version = invoke-command -ComputerName $server -ScriptBlock {systeminfo | find "OS Name:"} # this returns a string that represents the datetime of reboot
$os_version = $os_version.replace('OS Name: ', '') # Remove the leading text
$os_version = $os_version.replace(' ','') # Remove leading spaces
$os_version = $os_version.replace('Microsoft ','') # Removes Microsoft for data standardization
}
# Output each iteration of the loop into an array
$Row = "" | Select ServerName, OSVersion
$Row.ServerName = $Server
$Row.OSVersion = $os_version
$Array += $Row
}
# Export results to csv.
$Array | Export-Csv -Path $export_location -Force
编辑:这是我想要完成的。立即将命令发送到所有服务器(少于 30 个),让它们同时处理命令,而不是一个接一个地执行。我知道如果他们都可以接受 PowerShell 命令,我可以做到这一点,但因为他们不能,所以我很挣扎。此脚本总共需要 运行 大约 6 分钟。
提前致谢!
如果我没猜错的话,这样的东西应该就是你所需要的:
$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$ComputerName = Get-Content -Path $referencefile
$Result =
Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName $ComputerName |
Select-Object -Property Caption,PSComputerName
$Result
| Export-Csv -Path $export_location -NoTypeInformation
我想优化一个简单的任务:将服务器 OS 版本拉成整洁的 table。但是,我们环境中的某些服务器禁用了 Powershell。在下面填写找到我的脚本,它有效!但是,每台服务器大约需要 20 秒左右,因为它会等待服务器 return 调用命令的结果,然后再移动到列表中的下一台服务器。我知道有一种方法可以从 PS 命令中异步提取结果,但是当我需要为无法处理 PS 的服务器求助于 cmd 行语法时,这是否可行,如 catch 中所示声明?
$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$Array = @()
$servers = get-content $referencefile
foreach ($server in $servers){
#attempt to query the server with Powershell.
try{
$os_version = invoke-command -ComputerName $server -ScriptBlock {Get-ComputerInfo -Property WindowsProductName} -ErrorAction stop
$os_version = $os_version.WindowsProductName
} # If server doesnt have PS installed/or is disabled, then we will resort to CMD Prompt, this takes longer however.. also we will need to convert a string to an object.
catch {
$os_version = invoke-command -ComputerName $server -ScriptBlock {systeminfo | find "OS Name:"} # this returns a string that represents the datetime of reboot
$os_version = $os_version.replace('OS Name: ', '') # Remove the leading text
$os_version = $os_version.replace(' ','') # Remove leading spaces
$os_version = $os_version.replace('Microsoft ','') # Removes Microsoft for data standardization
}
# Output each iteration of the loop into an array
$Row = "" | Select ServerName, OSVersion
$Row.ServerName = $Server
$Row.OSVersion = $os_version
$Array += $Row
}
# Export results to csv.
$Array | Export-Csv -Path $export_location -Force
编辑:这是我想要完成的。立即将命令发送到所有服务器(少于 30 个),让它们同时处理命令,而不是一个接一个地执行。我知道如果他们都可以接受 PowerShell 命令,我可以做到这一点,但因为他们不能,所以我很挣扎。此脚本总共需要 运行 大约 6 分钟。
提前致谢!
如果我没猜错的话,这样的东西应该就是你所需要的:
$referencefile = "ps_servers_to_query.csv"
$export_location = "ps_server_os_export.csv"
$ComputerName = Get-Content -Path $referencefile
$Result =
Get-CimInstance -ClassName CIM_OperatingSystem -ComputerName $ComputerName |
Select-Object -Property Caption,PSComputerName
$Result
| Export-Csv -Path $export_location -NoTypeInformation