我正在开发一个函数,用于从远程计算机的注册表中提取 DisplayName 和 UninstallStrings。如果我只使用“$software”参数,它在我的本地 PC 上工作正常。但是,如果我添加“$compname”变量并尝试使用调用命令来让远程 PC 执行相同的操作,它会中断。以下是我目前的情况:
Function Get-Uninstallers {
param (
[parameter(mandatory)]
[string]$compName,
[parameter(mandatory)]
[string]$software,
$soft = ('"' + ("*" + $software + "*") + '"')
)
Invoke-Command -ComputerName $compname {
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty | Where-Object {$_.DisplayName -like $soft} |
Select-Object DisplayName, UninstallString
}
}
我尝试了各种不同的 $soft 变量组合,带/不带引号、单引号、不带 * 和不带 *。我知道我做错了什么,但我无论如何也想不通。我尝试在命令行上为参数添加引号,然后又去掉它们。
下面是我填写变量时得到的输出示例:
PS C:\> Invoke-Command -ComputerName 61966 {
Get-ChildItem -Path HKLM: \SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall,
HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty | Where-Object {$_.DisplayName -like "*calabrio*"} |
Select-Object DisplayName, UninstallString
}
DisplayName : Calabrio Screen Player Plug-in version 8.0.2.2004
UninstallString : "C:\WINDOWS\unins000.exe"
PSComputerName : 61966
RunspaceId : cfdbbc09-42e6-4c1f-a2ba-0bedc623d73b
DisplayName : Calabrio Analytic Services Client
UninstallString : MsiExec.exe /I{4762419C-C004-46F8-AB0D-DACA7C05258F}
PSComputerName : 61966
RunspaceId : cfdbbc09-42e6-4c1f-a2ba-0bedc623d73b
DisplayName : Calabrio Monitoring and Recording Recording
UninstallString : MsiExec.exe /X{5144778B-D71E-4FE5-8AEB-D5A2DAE6DABA}
PSComputerName : 61966
RunspaceId : cfdbbc09-42e6-4c1f-a2ba-0bedc623d73b
DisplayName : Calabrio Monitoring and Recording Services Service Release 0 Engineering Special 2
UninstallString : MsiExec.exe /X{FAEDCE7E-1A2B-4CCC-89D8-8DD28F51B05E}
PSComputerName : 61966
RunspaceId : cfdbbc09-42e6-4c1f-a2ba-0bedc623d73b
下面是我运行指定参数的函数时的示例:
PS C:\> Get-Uninstallers -compName 61966 -software calabrio
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : gmcr61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString : "C:\Program Files (x86)\Common Files\Microsoft Shared\OFFICE14\Oarpmany.exe" /removereleaseinpatch "{90140000-0011-0000-0000-0000000FF1CE}"
"{71949C83-C5B4-43D2-9372-E3838A5E82E6}" "1033" "0"
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
DisplayName :
UninstallString :
PSComputerName : 61966
RunspaceId : 2df812e2-8693-483f-918c-ece84892c5c5
所需的输出如下:
PS C:\> Invoke-Command -ComputerName 61966 {
$software = "*calabrio*"
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall |
Get-ItemProperty | Where-Object {$_.DisplayName -like $software} |
Select-Object DisplayName,UninstallString | FT
}
DisplayName UninstallString
----------- ---------------
Calabrio Screen Player Plug-in version 8.0.2.2004 "C:\WINDOWS\unins000.exe"
Calabrio Analytic Services Client MsiExec.exe /I{4762419C-C004-46F8-AB0D-DACA7C05258F}
Calabrio Monitoring and Recording Recording MsiExec.exe /X{5144778B-D71E-4FE5-8AEB-D5A2DAE6DABA}
Calabrio Monitoring and Recording Services Service Release 0 Engineering Special 2 MsiExec.exe /X{FAEDCE7E-1A2B-4CCC-89D8-8DD28F51B05E}
任何帮助都将不胜感激。谢谢。
答案1
我试过你的函数,结果也是空的。问题在于你对软件参数做了什么:你不必将其转换为变量$soft
,你可以直接$software
在 Where-Object 子句中使用带有星号的 -Variable。只需使用:
Get-ItemProperty | Where-Object {$_.DisplayName -like "*$software*"} | Select-Object DisplayName,UninstallString
这在我的系统上运行良好,通配符也运行良好。
编辑:这是在我的计算机上运行的完整函数 - 将变量转换$software
为的行$soft
只是被禁用,因为您不需要它:
Function Get-Uninstallers {
param(
[parameter(mandatory=$true)][string]$compName,
[parameter(mandatory=$true)][string]$software
#$soft = ('"' + ("*" + $software + "*") + '"')
)
Invoke-Command -ComputerName $compname {
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall, HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall | `
Get-ItemProperty | Where-Object {$_.DisplayName -like "*$using:software*"} | Select-Object DisplayName,UninstallString
}
}
当我使用该功能时,例如
Get-Uninstallers -compname my-pc -software VMWare
我得到一个包含所有软件的列表,其中 DisplayName 包含“VMware“,仅此而已。