我正在尝试制作一个 PowerShell 脚本来修改为 .ps1
文件扩展名设置默认应用程序的注册表值。遗憾的是,我无法深入了解脚本,因为 PSDrive 没有 HKEY_CLASSES_ROOT
的路径不存在。
使用以下方法在 Microsoft 网站上找到解决方案后:
New-PSDrive -PSProvider registry -Root 'HKEY_CLASSES_ROOT' -Name 'HKCR'
然后我想在继续之前添加一些代码来检查此 PSDrive 路径是否已设置,因此现在它出现在脚本的顶部,如下所示:
#####
IF (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "Requesting administration privileges..."; Start-Sleep -s 2; Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }
Write-Host "Administration privileges have been granted."
Start-Sleep -s 2
#####
Write-Host "Checking if 'HKEY_CLASSES_ROOT' path has been set...`n"
If (Get-PSDrive | Where{$_.Name -CMatch 'HKCR' -and $_.Root -CMatch 'HKEY_CLASSES_ROOT'}) {
Write-Output "Path has already been set"
} Else {
Write-Output "Path has not been set. Setting path now..."
New-PSDrive -PSProvider registry -Root 'HKEY_CLASSES_ROOT' -Name 'HKCR' >$Null 2>&1
}
Write-Host "`nDone"
Start-Sleep -s 2
Write-Host "Setting .PS1 (PowerShell scripts extension) file association with VS Code... " -NoNewLine
Set-ItemProperty -Path "HKCR:\Microsoft.PowerShellScript.1\Shell\Open\Command" -Name "(Default)" -Value {C:\Program Files\Microsoft VS Code\Code.exe "%1"}
Start-Sleep -s 2
Write-Host "Done"
Exit
很遗憾,我无法使用以下 IF 语句:
If (Get-PSDrive | Where{$_.Name -CMatch 'HKCR' -and $_.Root -CMatch 'HKEY_CLASSES_ROOT'}) {
Write-Output "Path has already been set"
} Else {
Write-Output "Path has not been set. Setting path now..."
New-PSDrive -PSProvider registry -Root 'HKEY_CLASSES_ROOT' -Name 'HKCR' >$Null 2>&1
}
在运行命令时,我总是看到以下消息(“Else”语句),每次我重新运行脚本时,我都看不到“If”语句:
Path has not been set. Setting path now...
更新 1
第 2 行的一行代码是将 PowerShell 脚本提升为管理员权限。第 3 行和第 4 行是让用户知道脚本已被提升。
我用 Write-Output
而不是冗长的代码尝试了你的代码,但我仍然看不到与“If”语句匹配的响应。
Write-Host "Checking if 'HKEY_CLASSES_ROOT' path has been set...`n"
If (Get-PSDrive |
Where {
($PSitem.Name -Match 'HKCR') -and
($PSitem.Root -Match 'HKEY_CLASSES_ROOT')
}
)
{Write-Output "Path has already been set"}
Else
{
Write-Output "Path has not been set. Setting path now..."
New-PSDrive -PSProvider registry -Root "HKEY_CLASSES_ROOT" -Name "HKCR" >$Null 2>&1
}
但是,如果我手动设置 PSDrive:
New-PSDrive -PSProvider registry -Root "HKEY_CLASSES_ROOT" -Name "HKCR" >$Null 2>&1
然后检查它是否存在:
PS D:\Users\Will> Get-PSDrive | Where{$_.Name -Match 'HKCR' -and $_.Root -Match 'HKEY_CLASSES_ROOT'}
Name Used (GB) Free (GB) Provider Root CurrentLocation
---- --------- --------- -------- ---- ---------------
HKCR Registry HKEY_CLASSES_ROOT
这工作得很好。我只想让我的脚本检查一下,如果尚未设置 PSDrive,则设置它。
答案 0 :(得分:0)
试试这些重构的部分。
If (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator'
)
)
{
Write-Host 'Requesting administration privileges...'
Start-Sleep -s 2
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
}
If (Get-PSDrive |
Where {
($PSitem.Name -eq 'HKCU') -and
($PSitem.Root -eq 'HKEY_CURRENT_USER')
}
)
{Write-Verbose -Message 'Specified PSDrive valid' -Verbose}
Else
{
Write-Warning -Message 'PSDrive not specific for the registry key specified.'e
}
# Results
<#
VERBOSE: Specified PSDrive valid
#>
If (Get-PSDrive |
Where {
($PSitem.Name -Match 'HKCR') -and
($PSitem.Root -Match 'HKEY_CLASSES_ROOT')
}
)
{Write-Verbose -Message 'Specified PSDrive valid'}
Else
{
Write-Warning -Message 'PSDrive not specific for the registry key specified.'
}
# Results
<#
WARNING: PSDrive not specific for the registry key specified.
#>