Mag*_*ndi 2 powershell scripting performance powershell-2.0
我正在使用PowerShell V2运行许多脚本,并且在控制台窗口首次加载时我注意到了很长的暂停.我该怎么做才能提高脚本的性能?
谢谢,MagicAndi
除了最小化您在各种配置文件脚本中放置的内容(如下所示)之外,您无能为力:
C:\PS> $profile | fl * -force
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\hillr\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\hillr\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Run Code Online (Sandbox Code Playgroud)
检查配置文件脚本是否导致延迟的一种方法是使用-noprofile选项启动PowerShell.如果启动时间不同,则可能是由于您的配置文件脚本.您可以像这样使用.NET秒表:
function TimeThis([scriptblock]$scriptblock, $msg)
{
if (!$stopWatch)
{
$script:stopWatch = new-object System.Diagnostics.StopWatch
}
$stopWatch.Reset()
$stopWatch.Start()
. $scriptblock
$stopWatch.Stop()
if ($msg -eq $null) { $msg = "$scriptblock" }
"Execution time: $($stopWatch.ElapsedMilliseconds) mS for $msg"
}
. TimeThis {Import-Module $Module -args ~\Pscx.UserPreferences.ps1}
Run Code Online (Sandbox Code Playgroud)
虽然您可以使用Measure-Command,但它不会显示执行的内容,也不会显示命令输出(只有非常详细的时间).
以前的CTP中曾经存在一个问题,即安装程序不会使用PowerShell程序集,这可能会导致明显的加载时间延迟.但是我很确定在最终的2.0安装中已经修复了(当然还有内置在Windows 7和Windows Server 2008 R2中的PowerShell).如果跟随dir及其内容存在,那么你应该知道:
dir 'C:\Windows\assembly\NativeImages_v2.0.50727_32\Microsoft.PowerShel#' -r
Run Code Online (Sandbox Code Playgroud)