Powershell 脚本运行缓慢 [英] Powershell Script Running Slowly
问题描述
我正在编写一个脚本来检查大约 15 个远程服务器上的版本,但脚本的执行时间比我预期的要长得多.
I'm writing a script to check the version on about 15 remote servers and the script is taking much longer to execute than I would expect.
$listServers = @("compName1", "compName2", "compName3", ... "compName15")
"" | Out-File C:\temp\javaVersion.txt
"" | Out-File C:\temp\javaVersionLog.txt
$cred = Get-Credential
ForEach ($server in $listServers)
{
Measure-Command {$javaVersion = Invoke-Command -ComputerName $server -Credential $cred -Authentication Kerberos -ScriptBlock {Get-WmiObject -Class Win32_Product -Filter "Name like 'Java [0-9]%'" | Select -ExcludeProperty Version}} -ErrorAction SilentlyContinue -ErrorVariable errorOutput
$errorOutput | Out-File C:\temp\javaVersionLog.txt -Append
$server + $javaVersion | Out-File C:\temp\javaVersion.txt -Append
}
根据 Measure-Command 输出,这需要大约 21 秒才能完成.我错过了脚本需要很长时间才能完成的原因是什么?
This takes about 21 seconds to complete according to the Measure-Command output. Is there a reason I'm missing that the script is taking so long to complete?
在被其他问题分散注意力之后,我终于完成了脚本.
After being distracted by other issues, I finally finished the script.
Start-Transcript C:\temp\javaVersion.txt
$listServers = @("compName1", "compName2", "compName3", ... "compName15")
$javaVersVerbose = ""
Invoke-Command -ComputerName $listServers -ScriptBlock {
$registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $_);
$javaKey = $registry.OpenSubKey('SOFTWARE\JavaSoft\Java Runtime Environment');
$javaVers = $javaKey.GetValue('CurrentVersion');
$javaVersVerbose = $javaKey.GetValue('Java' + $javaVers.Substring(2, 1) + 'FamilyVersion');
$nameKey = $registry.OpenSubKey('SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName');
$name = $nameKey.GetValue('ComputerName');
$name + " " + $javaVersVerbose | echo
} -ErrorAction SilentlyContinue -ErrorVariable errorOutput
$errorOutput | echo
Write-Host -NoNewLine 'Press any key to continue...'
$null = $Host.UI.RawUI.ReadKey('NoEcho, IncludeKeyDown')
推荐答案
您不需要循环执行此操作,也不需要连续执行此操作.invoke-command
接受 ComputerName
的集合,并且可以并行运行请求.
You don't need to do this in a loop, nor serially. invoke-command
takes a collection of ComputerName
s, and can run the requests in parallel.
$listServers = @("compName1", "compName2", "compName3", ... "compName15")
Invoke-Command -throttlelimit 4 -ComputerName $listServers -Credential $cred -Authentication Kerberos -ScriptBlock {Get-WmiObject -Class Win32_Product -Filter "Name like 'Java [0-9]%'" | Select -ExcludeProperty Version}} -ErrorAction SilentlyContinue -ErrorVariable errorOutput
但是,正如 Tim Ferrell 所指出的,您可以使用 Get-WMIObject 远程 ping 服务器,如果您将其作为作业执行,它将并行运行多个请求.
However, as was pointed out by Tim Ferrell, you can use Get-WMIObject to ping the servers remotely, and if you do it as a job, it will run multiple requests in parallel.
Get-WMIObject Win32_Product -Filter "Name like 'Java [0-9]%'" -computername $listServers -throttlelimit 4 -asjob |select -excludeproperty version
然后使用 Job cmdlet 接收结果.
Then use the Job cmdlets to receive the results.
这篇关于Powershell 脚本运行缓慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!