当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 测量 PowerShell 配置文件性能

作者:精品下载站 日期:2024-12-14 08:00:20 浏览:14 分类:玩电脑

测量 PowerShell 配置文件性能


[玩转系统] 测量 PowerShell 配置文件性能

今天的主题是我不知道为什么以前从未讨论过的事情之一。嗯,我自己有一个手动过程。希望您会发现它很有用。

您可能知道,PowerShell 使用一组配置文件脚本。这些脚本具有硬编码路径,PowerShell 按从最宽到最窄的顺序运行它们。

  • 所有用户 所有主机
  • 所有用户当前主机
  • 当前用户所有主机
  • 当前用户 当前主机

主机是指您在查看 $host 时看到的 PowerShell 主机。 $profile 变量显示当前用户当前主机脚本的值。但您可以轻松查看所有值。

[玩转系统] 测量 PowerShell 配置文件性能

这是经常出现的“问题”。有人会提到 PowerShell(包括 PowerShell Core)加载时间太长。事实上,PowerShell 现在会向您显示加载时间。问题几乎总是与个人资料相关。有时命令运行时间太长,或者配置文件可能需要一些清理。我知道我的 PS7 加载时间很长,直到我清理了一些项目并重新构建了一些命令。

为了使这一过程变得更容易,我整理了一个简单的脚本,您可以在 Windows PowerShell 中运行该脚本,或者将运行您的配置文件脚本并报告完成所需时间的 PowerShell(甚至是跨平台)。

#requires -version 5.1

#test profile script performance

<#
C:\scripts\Test-ProfilePerf.ps1
C:\scripts\Test-ProfilePerf.ps1 -outvariable p | measure-object -Property TimeMS -sum
$p

#>

[cmdletbinding()]
Param()

#this is the scriptblock of commands to run in a new Powershell/pwsh session
$sb = {
    $profiles = [ordered]@{
        AllUsersAllHosts       = $profile.AllUsersAllHosts
        AllUsersCurrentHost    = $profile.AllUsersCurrentHost
        CurrentUserAllHosts    = $profile.CurrentUserAllHosts
        CurrentUserCurrentHost = $profile.CurrentUserCurrentHost
    }

    #only need to get these values once
    $psver = $PSVersionTable.PSVersion
    $computer = [System.Environment]::MachineName
    $user = "$([System.Environment]::UserDomainName)$([system.Environment]::userName)"

    foreach ($prof in $profiles.GetEnumerator()) {
        If (Test-Path $prof.value) {
            # Write-Host "Measuring script for $($prof.key)" -ForegroundColor cyan
            $m = Measure-Command { . $prof.value }

            #create a result
            [pscustomobject]@{
                Computername = $computer
                Username     = $user
                PSVersion    = $psver
                Profile      = $prof.key
                Path         = $prof.value
                TimeMS       = $m.totalMilliseconds
            }
        } #if test path

    } #foreach profile
} #scriptblock

#use the detected PowerShell version
if ($PSEdition -eq 'Core') {
    #Windows uses pwsh.exe and non-Windows uses pwsh. 
    $cmd = (Get-Command pwsh).source
    $result = &$cmd -nologo -noprofile -command $sb
}
else {
    $result = powershell.exe -nologo -noprofile -command $sb
}

#use $result as the output of this command
$result

该脚本文件在没有配置文件的新 PowerShell 或 pwsh 会话中运行脚本块。脚本块按顺序运行每个配置文件脚本,并测量每个检测到的配置文件脚本运行的时间。表演时间应该非常接近。

[玩转系统] 测量 PowerShell 配置文件性能

[玩转系统] 测量 PowerShell 配置文件性能

如果您想查看总数,可以运行如下命令:

C:\scripts\Test-ProfilePerf.ps1 -outvariable p | measure-object -Property TimeMS -sum

该脚本使用 cmdletbinding 进行配置,因此您可以使用 OutVariable 等通用参数。

[玩转系统] 测量 PowerShell 配置文件性能

如果您进行测试,并且其中一个脚本似乎需要很长时间,请稍等一下并重复测试。如果你一直得到一个你不喜欢的值,至少现在你有一些东西可以调查。

注意事项

该脚本假设您的配置文件脚本没有采取激烈的步骤,例如创建新的 Active Directory 用户或删除文件。我假设您正在使用配置文件脚本来定义变量和 PSDrive 等内容,并导入模块或定义函数。

此版本的脚本也不会在 VS Code 或 PowerShell ISE 等主机下测试配置文件脚本。

我希望你觉得这有帮助。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯