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

[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本

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

周五乐趣:创建所有 PowerShell 配置文件脚本


[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本

配置文件路径是硬编码的。您可以通过查看内置的 $profile 变量来查看所有这些。

PS C:\> $profile | select *

AllUsersAllHosts       : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps
                         1
CurrentUserAllHosts    : C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 : 74

您还可以仅查看 $profile,它将显示当前主机上当前用户的脚本。如果您在 ISE 中查看此内容,您会发现有些东西有所不同。

[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本

这个变量有点棘手。尽管您可以在此处看到属性,但如果将 $profile 通过管道传递给 Get-Member,您得到的只是一个字符串定义。这是您发现这些的另一种方法。

PS C:\> $profile.psobject.properties | format-table Name,Value -auto

Name                   Value
----                   -----
AllUsersAllHosts       C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
Length                 74

虽然我不太关心长度,所以让我们过滤掉它。

PS C:\> $profile.psobject.properties | select -first 4 | format-table Name,Value -auto

Name                   Value
----                   -----
AllUsersAllHosts       C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost    C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts    C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

现在是有趣的部分。该值是一个路径,因此我可以使用 Test-Path 来查看它是否存在。如果没有,我可以创建配置文件并至少表明该配置文件未被使用。由于大多数文件默认情况下不存在,并且事实上 WindowsPowerShell 文件夹在您创建之前并不存在于 Documents 下,因此您可能必须首先创建父文件夹。所以我想出了一个名为 Create-AllProfiles.ps1 的小脚本。

#requires -version 3.0

[cmdletbinding(SupportsShouldProcess=$True)]
Param()

$profile.psobject.properties.value[0..3] | where { -Not (Test-path $_)} |
foreach {
  #create the parent folder if it doesn't exist
  $parent = Split-Path $_
  if (-Not (Test-Path -path $parent)) {
    mkdir $parent
   }

  #create the profile with this text
  #the here string must be left justified
$txt=@"
#requires -version $($psversiontable.PSVersion)

<#
This profile is not used and intentionally left blank.
 $env:userdomain$env:username
 $(Get-Date)
#>

"@ 

#write the text to the profile script
Write-Host "Creating $_" -ForegroundColor Green
$txt | Out-File -FilePath $_

}

该脚本利用了 PowerShell 3 中自动枚举属性的功能。在 PowerShell 2.0 中我需要这样做:

PS C:\> $profile.psobject.properties | select -ExpandProperty Value -first 4
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

但现在在 v3 及更高版本中我可以这样做:

PS C:\> $profile.psobject.properties.value[0..3]
C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\profile.ps1
C:\Users\Jeff\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

value 属性将显示所有值,包括长度,因此因为我正在获取一个数组,所以我可以使用一系列索引号来选择我想要的值。在我的脚本中,每个值都会被测试,那些不存在的值将被传递给 Foreach-Object。

对于每个路径,我首先将其拆分,以便测试父路径是否存在。如果没有,则创建它。然后我创建一些文本,将插入到每个新的配置文件脚本中。该脚本支持 -WhatIf,因此您可以在实际运行它之前了解它会做什么。

[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本

我最终得到一个如下所示的配置文件脚本:

[玩转系统] 周五乐趣:创建所有 PowerShell 配置文件脚本

理想情况下,我建议您对配置文件脚本进行数字签名(假设您使用的是 AllSigned 执行策略),以便在您不知情的情况下无法对这些脚本进行任何更改。只是不要忘记也在 PowerShell ISE 中运行它。如果您已经创建了配置文件,它们将被忽略。如果您稍后决定利用任何配置文件脚本,它们已准备好供您编辑。

学习、享受并度过一个愉快的周末。

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

取消回复欢迎 发表评论:

关灯