使用 powershell 远程删除通配符文件夹中的多个注册表项?
Remotely delete multiple registry keys in a wildcard folder using powershell?
我正在编写一个脚本,该脚本将删除存储在注册表中的 App-V 密钥。当用户打开应用程序时,它会在以下位置创建一个密钥:
HKLM\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\**PackageID**\UserConfigEx\**SID**
PackageID
和 SID
每次都是唯一的,我希望能够删除每个 PackageID
键中的 SID
子键。
用户将输入 SID,然后我想使用通配符(如果可能)导航到存在的每个包 ID。
到目前为止我有以下内容:
#Take user input
$SID = Read-Host "Please enter users SID"
$computer = Read-Host "Please enter computer name"
#Test connection
Write-Host "Connecting to $computer"
if (Test-Connection -ComputerName $computer -Quiet -BufferSize 16 -Count 1) {
#Connect to registry and delete key
try
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $computer)
$regKey = $reg.OpenSubKey(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\*\UserConfigEx\$SID”,$true )
if ($regkey.GetValue(“$SID”))
{
$regKey.DeleteValue(“$SID”)
Write-Host
Write-Host "$SID key deleted successfully" -ForegroundColor Green
}
else
{
Write-Host
Write-Host "No keys with this SID exist." -ForegroundColor Red
}
} catch {
$ErrorMessage = $_.Exception.Message
Write-Host "Unable to connect to $computer. Error: $($ErrorMessage)." -ForegroundColor Red
}
} else
{
Write-Host "Unable to connect to $computer. Please ensure correct computer name / IP address has been entered correctly." -ForegroundColor Red
}
如果我 运行 我收到:
You cannot call a method on a null-valued expression.
At line:51 char:9
+ if ($regkey.GetValue(“$SID”))
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我正在使用我收到的一些帮助 的脚本来远程连接到机器。
看起来像是 ascii 与 unicode 引号问题:
你有:
$regkey.GetValue(“$SID”)
应替换为:
$regkey.GetValue("$SID")
.NET 注册表 API 不支持关键路径中的通配符 (*
).
- 因此,
$regKey.GetValue()
失败了,因为 $regKey = $reg.OpenSubKey(...)
由于找不到密钥而返回 $null
,并且在 $null
上调用方法总是导致问题中引用的错误信息。
相比之下,PowerShell 的注册表提供程序通过 *-Item*
cmdlet 可以,但是您需要 PowerShell remoting 才能使用远程。
PowerShell 远程处理在 Windows Server 2012 及更高版本上默认启用;在较旧的 OS 版本上,您可以通过 运行 Enable-PSRemoting
在目标计算机上启用它(需要 PSv3+)。
启用 PowerShell 远程处理后,您需要将代码包装在 Invoke-Command -ComputerName <name> { ... }
调用中(您可能还必须向其传递凭据)。
如果启用 PowerShell 远程处理不是一个选项,您必须通过嵌套循环模拟基于通配符的匹配基于 .GetSubkeyNames()
.
结果的每个元素通配符匹配
顺便说一句:您永远不需要在 PowerShell 字符串中将 \
转义为 \
; PowerShell 使用 `
作为 "..."
内的转义字符,因此您需要转义的唯一字符是 `
本身,如 ``
.
基于 PowerShell 远程处理的解决方案:
请注意 Invoke-Command -ComputerName ...
必须从 提升的 会话中调用 (Run As Administrator
):
try {
Invoke-Command -ErrorAction Stop -ComputerName $computer {
# Define wildcard-based path.
$keyPath = "registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\*\UserConfigEx$SID"
# See if it matches any keys.
if (Test-Path $keyPath) {
# Note: I'm assuming you want to remove the entire *key*.
# To only remove a key's *value*, use Remove-ItemProperty.
Remove-Item -Path $keyPath
} else {
Write-Warning "No keys with SID $SID exist."
}
}
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
# Note: Depending on the specifics of your Invoke-Command call, the reason may
# be permissions-related; when in doubt, examine $_
Write-Warning "Unable to connect to $computer. Please ensure correct computer name / IP address has been entered correctly:`n$_"
} catch {
# Other, unexpected failure.
Throw
}
我正在编写一个脚本,该脚本将删除存储在注册表中的 App-V 密钥。当用户打开应用程序时,它会在以下位置创建一个密钥:
HKLM\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\**PackageID**\UserConfigEx\**SID**
PackageID
和 SID
每次都是唯一的,我希望能够删除每个 PackageID
键中的 SID
子键。
用户将输入 SID,然后我想使用通配符(如果可能)导航到存在的每个包 ID。
到目前为止我有以下内容:
#Take user input
$SID = Read-Host "Please enter users SID"
$computer = Read-Host "Please enter computer name"
#Test connection
Write-Host "Connecting to $computer"
if (Test-Connection -ComputerName $computer -Quiet -BufferSize 16 -Count 1) {
#Connect to registry and delete key
try
{
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $computer)
$regKey = $reg.OpenSubKey(“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\*\UserConfigEx\$SID”,$true )
if ($regkey.GetValue(“$SID”))
{
$regKey.DeleteValue(“$SID”)
Write-Host
Write-Host "$SID key deleted successfully" -ForegroundColor Green
}
else
{
Write-Host
Write-Host "No keys with this SID exist." -ForegroundColor Red
}
} catch {
$ErrorMessage = $_.Exception.Message
Write-Host "Unable to connect to $computer. Error: $($ErrorMessage)." -ForegroundColor Red
}
} else
{
Write-Host "Unable to connect to $computer. Please ensure correct computer name / IP address has been entered correctly." -ForegroundColor Red
}
如果我 运行 我收到:
You cannot call a method on a null-valued expression.
At line:51 char:9
+ if ($regkey.GetValue(“$SID”))
+ ~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
我正在使用我收到的一些帮助
看起来像是 ascii 与 unicode 引号问题:
你有:
$regkey.GetValue(“$SID”)
应替换为:
$regkey.GetValue("$SID")
.NET 注册表 API 不支持关键路径中的通配符 (
*
).- 因此,
$regKey.GetValue()
失败了,因为$regKey = $reg.OpenSubKey(...)
由于找不到密钥而返回$null
,并且在$null
上调用方法总是导致问题中引用的错误信息。
- 因此,
相比之下,PowerShell 的注册表提供程序通过
*-Item*
cmdlet 可以,但是您需要 PowerShell remoting 才能使用远程。PowerShell 远程处理在 Windows Server 2012 及更高版本上默认启用;在较旧的 OS 版本上,您可以通过 运行
Enable-PSRemoting
在目标计算机上启用它(需要 PSv3+)。启用 PowerShell 远程处理后,您需要将代码包装在
Invoke-Command -ComputerName <name> { ... }
调用中(您可能还必须向其传递凭据)。
如果启用 PowerShell 远程处理不是一个选项,您必须通过嵌套循环模拟基于通配符的匹配基于
.GetSubkeyNames()
. 结果的每个元素通配符匹配
顺便说一句:您永远不需要在 PowerShell 字符串中将
\
转义为\
; PowerShell 使用`
作为"..."
内的转义字符,因此您需要转义的唯一字符是`
本身,如``
.
基于 PowerShell 远程处理的解决方案:
请注意 Invoke-Command -ComputerName ...
必须从 提升的 会话中调用 (Run As Administrator
):
try {
Invoke-Command -ErrorAction Stop -ComputerName $computer {
# Define wildcard-based path.
$keyPath = "registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppV\MAV\Configuration\Packages\*\UserConfigEx$SID"
# See if it matches any keys.
if (Test-Path $keyPath) {
# Note: I'm assuming you want to remove the entire *key*.
# To only remove a key's *value*, use Remove-ItemProperty.
Remove-Item -Path $keyPath
} else {
Write-Warning "No keys with SID $SID exist."
}
}
} catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
# Note: Depending on the specifics of your Invoke-Command call, the reason may
# be permissions-related; when in doubt, examine $_
Write-Warning "Unable to connect to $computer. Please ensure correct computer name / IP address has been entered correctly:`n$_"
} catch {
# Other, unexpected failure.
Throw
}