PowerShell 技能连载 - PowerShell技能连载-检查配置文件脚本(第 2 部分)

在上一个脚本中,我们介绍了一一行代码,用于检查哪些配置文件脚本是存在的。但是,此解决方案仅只适用于单个宿主,因为每个宿主都使用自己的特定于宿主的配置文件路径。

这是一种更通用的方法:它列出了系统上存在的所有 PowerShell 宿主的所有配置文件路径。然后,您可以在安全性或健全性检查中检查以下文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# calculate the parent paths that can contain profile scripts
$Paths = @{
AllUser_WPS = $pshome
CurrentUser_WPS = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath "WindowsPowerShell"
AllUser_PS = "$env:programfiles\PowerShell\*"
CurrentUser_PS = Join-Path -Path ([Environment]::GetFolderPath('MyDocuments')) -ChildPath "PowerShell"
}

# check all paths for PowerShell scripts ending on "profile.ps1"
$Paths.Keys | ForEach-Object {
$key = $_
$path = Join-Path -Path $paths[$key] -ChildPath '*profile.ps1'
Get-ChildItem -Path $Path |
ForEach-Object {
# create a custom object with all relevant details for any
# found profile script

# name of PowerShell host is the prefix of profile file name
if ($_.Name -like '*_*')
{
$hostname = $_.Name.Substring(0, $_.Name.Length-12)
}
else
{
$hostname = 'any'
}
[PSCustomObject]@{
# scope and PowerShell version is found in the
# name of the parent folder
Scope = $key.Split('_')[0]
PowerShell = $key.Split('_')[1]

Host = $hostname
Path = $_.FullName
}
}
}

结果报告了所有主机的现有 PowerShell 配置文件脚本,看起来可能与此类似:

Scope       PowerShell Host                    Path
-----       ---------- ----                    ----
CurrentUser WPS        Microsoft.PowerShellISE C:\Users\tobia\OneDrive\Dokumente\WindowsPowerShell\Microsoft.PowerShellISE_...
CurrentUser WPS        any                     C:\Users\tobia\OneDrive\Dokumente\WindowsPowerShell\profile.ps1
CurrentUser PS         Microsoft.VSCode        C:\Users\tobia\OneDrive\Dokumente\PowerShell\Microsoft.VSCode_profile.ps1

PowerShell 技能连载 - PowerShell技能连载-检查配置文件脚本(第 2 部分)

http://blog.vichamp.com/2020/10/01/checking-profile-scripts-part-2/

作者

吴波

发布于

2020-10-01

更新于

2022-07-06

许可协议

评论