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

[玩转系统] 使用 PowerShell 配置文件

作者:精品下载站 日期:2024-12-14 07:15:38 浏览:14 分类:玩电脑

使用 PowerShell 配置文件


有时,我需要配置每次打开 PowerShell 或加载某些函数或变量时都会应用的特定设置。这可以通过使用 PowerShell 配置文件脚本轻松实现。

配置文件脚本可以被描述为启动脚本,并且是每次启动 PowerShell 时都会在当前范围内运行的脚本,非常适合加载自定义函数、设置或变量。用户配置文件脚本的位置存储在包含字符串的变量 $profile 中。

此字符串引用所谓的 CurrentUserCurrentHost 配置文件脚本,并且可以具有如下值:C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

是的,您猜对了,CurrentHost 意味着该脚本仅适用于一台主机。主机是托管 PowerShell 的程序,Windows 中默认有两个可用的主机:powershell.exe 和 PowerShell ISE,每个主机都有自己的配置文件脚本。

Microsoft.PowerShell_profile.ps1 是 powershell.exe 的 CurrentUserCurrentHost 配置文件。如果我在 PowerShell ISE 中检查 $profile 的值,则会得到以下值:C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1

这两个文件都位于用户的文档文件夹中,并且特定于每个用户。您可以安全地在这些配置文件中加载您的功能和自定义设置,而不会影响任何其他用户。

但是等等,还有更多! $Profile 是一个常见字符串,但如果我们将其通过管道传输到 cmdlet Get-Member,我们可以看到它有四个额外的注释属性,名称为:

所有用户所有主机

所有用户当前主机

当前用户所有主机

当前用户当前主机

其中每个都有一个引用配置文件脚本的值,并且每次启动 PowerShell 时都会加载所有这些值。它们的加载顺序与上面列出的相同,这意味着如果我在 AllUsersAllHosts 和 CurrentUserCurrentHost 中设置相同的变量,则 CurrentUserCurrent 主机中设置的值将覆盖 AllUsersAllHosts 中设置的值。

要显示所有配置文件脚本的路径,请使用参数 -Property * 通过管道 $profile 到 cmdlet Select-Object。这将显示对象的所有属性。这是我在 powershell.exe 中得到的输出:

[玩转系统] 使用 PowerShell 配置文件

如果我在 PowerShell ISE 中运行相同的操作,我会得到以下结果:

[玩转系统] 使用 PowerShell 配置文件

我们可以很快看到两张图片上的 AllHosts 配置文件相同,而 CurrentHost 配置文件不同,这意味着实际上有六个不同的配置文件脚本!

但这还不是全部!如果您运行的是 64 位操作系统,则 32 位和 64 位版本的 PowerShell 的 AllUsers 配置文件脚本会有所不同。如果启动 32 位版本的 PowerShell、Windows PowerShell (x86),AllUsers 配置文件将存储在 C:WindowsSysWOW64WindowsPowerShellv1.0 中。如果您有只想在使用 32 位或 64 位主机时加载的特定设置,这会派上用场,只需记住这些设置将应用于计算机上的所有用户。

总共有九个配置文件可用!以下是完整列表:

AllUsersAllHosts (x86)            : C:WindowsSysWOW64WindowsPowerShellv1.0profile.ps1
AllUsersAllHosts (x64)            : C:WindowsSystem32WindowsPowerShellv1.0profile.ps1

AllUsersCurrentHost (ISE x86)     : C:WindowsSysWOW64WindowsPowerShellv1.0Microsoft.PowerShellISE_profile.ps1
AllUsersCurrentHost (ISE x64)     : C:WindowsSysWOW64WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1
AllUsersCurrentHost (Console x86) : C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShellISE_profile.ps1
AllUsersCurrentHost (Console x64) : C:WindowsSystem32WindowsPowerShellv1.0Microsoft.PowerShell_profile.ps1

CurrentUserAllHosts               : C:UsersSimonDocumentsWindowsPowerShellprofile.ps1

CurrentUserCurrentHost (ISE)      : C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShellISE_profile.ps1
CurrentUserCurrentHost (Console)  : C:UsersSimonDocumentsWindowsPowerShellMicrosoft.PowerShell_profile.ps1

在开始自定义 PowerShell 默认设置之前,我们必须创建配置文件脚本。我使用以下代码片段创建所有配置文件脚本并在 PowerShell ISE 中打开它们。这适用于 ISE 和 powershell.exe

powershell_ise.exe -File ((
    $profile.AllUsersAllHosts,
    $profile.AllUsersCurrentHost,
    $profile.CurrentUserAllHosts,
    $profile.CurrentUserCurrentHost | 
    ForEach-Object {
        $Path = $_
        Try {
            $ParentPath = Split-Path $Path
            if(-Not(Test-Path $ParentPath)) {
                New-Item -Path $ParentPath -ItemType Directory -ErrorAction Stop
            }
            if(-Not(Test-Path $Path)) {
                New-Item -Path $Path -ItemType File -ErrorAction Stop
            }
            $_
        } Catch {
            Write-Warning -Message "Failed to create file $Path"
        }
    }
)-join ',')

好的!现在所有配置文件都已创建,我该在其中放入什么?!

有一篇关于“嘿,脚本专家!”的精彩帖子。博客列出了有关在您的个人资料中添加哪些内容的建议:http://blogs.technet.com/b/heyscriptingguy/archive/2014/05/22/what-s-in-your-powershell-profile-powershell-mvps -收藏夹.aspx

快乐的分析!

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

取消回复欢迎 发表评论:

关灯