如何通过 PS 远程访问多台远程计算机以发现一个特定的应用程序并确定每台远程设备上的版本号?无输出
How do I remote into multiple remote computers via PS to discover one specific app and determine the version number on each remote device? No output
仅供参考:我是 PS 的新手,我将此作为学习机会。再一次,我试图找到一个
多个远程设备列表上的特定应用程序并确定版本号
在其相应的主机系统上的应用程序。我通过注册表查询尝试了这个(发现这个
具有挑战性),然后我使用了 Get-WMIObject。截至目前,我正在使用它作为我的脚本。它是
不产生任何输出;相反,它 returns 到命令提示符,没有错误或消息。
在多个远程设备中查找特定应用程序和版本的脚本:
$Servers = Get-Content -Path C:\files\Serverlist.txt
$CIMSession = New-CIMSession -ComputerName $Servers Get-Credentials
$Vendor = "App Name"
foreach($Serv in $Servers) {
If(Test-Connection -ComputerName $Serv -Count 1 -Quiet) {
$Status = Get-Ciminstance Win32_Product -Computername $Serv | Where-object {$_.Version -contains
$Vendor}
if($Status) {
Out-file -Filepath C:\files\AppVerResults.txt
}
}
}
我还尝试调整脚本的以下部分,如下所示,但它向我显示错误“Get-CimInstance:访问被拒绝。”此错误消息是由于组策略还是其他原因造成的?我可以通过 RDP 远程访问与消息对应的设备。
if($Status) {
$Servers + " - "
$Status | Out-file -Filepath C:\files\AppVerResults.txt
}
}
}
我应该通过调用命令还是注册表查询来解决这个问题?我正在慢慢收拾东西,所以我会继续我的研究,但我希望在此期间得到一些建议。
我仍然认为搜索注册表是更简单的方法,除非您有 .exe.
的特定文件路径
使用此功能可在远程或本地 PC 上查找软件。通过指定 -SoftwareName
有一个 filter 选项( 查找 )。
Find-Software -ComputerName Remote_ComputerName -SoftwareName 'SQL'
也接受管道输入,以及要查询的多个计算机名称。
Find-Software -ComputerName ComputerOne, ComputerTwo, ComputerThree -SoftwareName 'SQL'
'ComputerOne','ComputerTwo' | Find-Software -SoftwareName 'SQL'
- 也可以通过管道传输到
Export-*
cmdlet 进行导出。
代码如下:
Function Find-Software {
[cmdletBinding()]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true)]
[Alias('cn','name')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[String]$SoftwareName
)
Begin{
#Get Computer Names to check software version for
$Server_List = Get-Content -Path "C:\files\Serverlist.txt"
#Get Credentials for Script Scope once.
$Credentials = Get-Credential
}
Process{
if($PSBoundParameters.ContainsKey('SoftwareName')){
foreach($Computer in $ComputerName){
Try{
$PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop
$Software_List = Invoke-Command -ScriptBlock {
Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession
$Software_List = $Software_List | Where-Object -FilterScript {$_.DisplayName -match $SoftwareName} | Sort-Object -Property DisplayName
foreach($Software in $Software_List){
if($Software){
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = $Software.DisplayName
" Version " = $Software.DisplayVersion
}
} else {
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = "Not found"
" Version " = $null
}
}
}
} Catch {
"Unable to connect to PC: $Computer"
"Error: $($Error[0].Message.Split('.')[1].Trim())"
}
}
} else {
foreach($Computer in $ComputerName){
Try{
$PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop
$Software_List = Invoke-Command -ScriptBlock {
Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession
$Software_List = $Software_List | Sort-Object -Property DisplayName
foreach($Software in $Software_List){
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = $Software.DisplayName
" Version " = $Software.DisplayVersion
}
}
} Catch {
"Unable to connect to PC: $Computer"
"Error: $($Error[0].Message.Split('.')[1].Trim())"
}
}
} #end ELSE statement
} #end PROCESS block
End {
if(Get-PSSession){
Get-PSSession | Remove-PSSession
}
} #end END block - Perform Session Clean Up
} #end FUNCTION
只需修改它以满足您的需要:)
仅供参考:我是 PS 的新手,我将此作为学习机会。再一次,我试图找到一个 多个远程设备列表上的特定应用程序并确定版本号 在其相应的主机系统上的应用程序。我通过注册表查询尝试了这个(发现这个 具有挑战性),然后我使用了 Get-WMIObject。截至目前,我正在使用它作为我的脚本。它是 不产生任何输出;相反,它 returns 到命令提示符,没有错误或消息。
在多个远程设备中查找特定应用程序和版本的脚本:
$Servers = Get-Content -Path C:\files\Serverlist.txt
$CIMSession = New-CIMSession -ComputerName $Servers Get-Credentials
$Vendor = "App Name"
foreach($Serv in $Servers) {
If(Test-Connection -ComputerName $Serv -Count 1 -Quiet) {
$Status = Get-Ciminstance Win32_Product -Computername $Serv | Where-object {$_.Version -contains
$Vendor}
if($Status) {
Out-file -Filepath C:\files\AppVerResults.txt
}
}
}
我还尝试调整脚本的以下部分,如下所示,但它向我显示错误“Get-CimInstance:访问被拒绝。”此错误消息是由于组策略还是其他原因造成的?我可以通过 RDP 远程访问与消息对应的设备。
if($Status) {
$Servers + " - "
$Status | Out-file -Filepath C:\files\AppVerResults.txt
}
}
}
我应该通过调用命令还是注册表查询来解决这个问题?我正在慢慢收拾东西,所以我会继续我的研究,但我希望在此期间得到一些建议。
我仍然认为搜索注册表是更简单的方法,除非您有 .exe.
的特定文件路径使用此功能可在远程或本地 PC 上查找软件。通过指定 -SoftwareName
有一个 filter 选项( 查找 )。
Find-Software -ComputerName Remote_ComputerName -SoftwareName 'SQL'
也接受管道输入,以及要查询的多个计算机名称。
Find-Software -ComputerName ComputerOne, ComputerTwo, ComputerThree -SoftwareName 'SQL'
'ComputerOne','ComputerTwo' | Find-Software -SoftwareName 'SQL'
- 也可以通过管道传输到
Export-*
cmdlet 进行导出。
代码如下:
Function Find-Software {
[cmdletBinding()]
Param(
[Parameter(Mandatory=$false,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true)]
[Alias('cn','name')]
[string[]]$ComputerName = $env:COMPUTERNAME,
[Parameter(Mandatory=$false)]
[String]$SoftwareName
)
Begin{
#Get Computer Names to check software version for
$Server_List = Get-Content -Path "C:\files\Serverlist.txt"
#Get Credentials for Script Scope once.
$Credentials = Get-Credential
}
Process{
if($PSBoundParameters.ContainsKey('SoftwareName')){
foreach($Computer in $ComputerName){
Try{
$PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop
$Software_List = Invoke-Command -ScriptBlock {
Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession
$Software_List = $Software_List | Where-Object -FilterScript {$_.DisplayName -match $SoftwareName} | Sort-Object -Property DisplayName
foreach($Software in $Software_List){
if($Software){
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = $Software.DisplayName
" Version " = $Software.DisplayVersion
}
} else {
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = "Not found"
" Version " = $null
}
}
}
} Catch {
"Unable to connect to PC: $Computer"
"Error: $($Error[0].Message.Split('.')[1].Trim())"
}
}
} else {
foreach($Computer in $ComputerName){
Try{
$PSSession = New-PSSession -ComputerName $Computer -Credential $Credentials -EnableNetworkAccess -ErrorAction Stop
$Software_List = Invoke-Command -ScriptBlock {
Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*",
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*" } -Session $PSSession
$Software_List = $Software_List | Sort-Object -Property DisplayName
foreach($Software in $Software_List){
[PSCustomObject]@{
"Computer Name" = $Computer
"Software Name" = $Software.DisplayName
" Version " = $Software.DisplayVersion
}
}
} Catch {
"Unable to connect to PC: $Computer"
"Error: $($Error[0].Message.Split('.')[1].Trim())"
}
}
} #end ELSE statement
} #end PROCESS block
End {
if(Get-PSSession){
Get-PSSession | Remove-PSSession
}
} #end END block - Perform Session Clean Up
} #end FUNCTION
只需修改它以满足您的需要:)