有的时候,豆子使用GPO安装软件之后,希望查看在客户端是否成功安装,Windows客户端可能有几百上千台,于是豆子写了个简单的程序来进行查看。
思路如下:
传递参数软件名字和操作系统名字,搜索到对应的计算机对象,对于每一个对象创建远程session,如果失败(比如关机或者其他情况),输出失败计算机的名字;对于成功连接的对象,根据32bit或者64bit查询注册表,输出结果。如果该软件不存在,返回n/a的一个自定义的对象
#查询域内的软件安装情况 function Get-Software{ <# .SYNOPSIS Function that allows you to Query Software in AD domain .DESCRIPTION You can add the function to your scripts, dot source it to load into your current PowerShell session or add it to your $Profile so it is always available. To dot source: .".\Get-Software.ps1" .PARAMETER Property Property, or column that you will be keying on. .PARAMETER software The software name you want to search .PARAMETER os operating system name, you don't need to input the full name, any keyword will work .PARAMETER dn Specifies the searchbase of your AD query .EXAMPLE Get-Software -software globalprotect -DN "ou=aaa Group Workstations,ou=aaa Group Machines, dc=aaa,dc=com,dc=au" -os 7 Assume check PaloAlto *** agent installation status for all windows 7 machines in a designated OU .EXAMPLE Get-Software -software silver | export-csv c:\temp\result.csv Get the SilverLight Installation status in the domain and export to a csv file .NOTES Author: Yuan Li QQ: 38144205 Blog: beanxyz.blog.51cto.com #> [cmdletbinding()] param( [parameter(mandatory=$true,position=1)][string]$software, [string]$OS='', #当前域的DN,根据自己情况修改 [string]$DN='dc=aaa,dc=com,dc=au' ) Write-Verbose "Scanning Computers..." if($os -ne ''){ $a=Get-ADComputer -filter "operatingsystem -like '*$os*' " -Searchbase "$dn" -Properties operatingsystem,ipv4address | Where-Object{$_.ipv4address -ne $null} | select -ExpandProperty name } else { $a=Get-ADComputer -Filter * -Searchbase "$dn" -Properties operatingsystem,ipv4address | Where-Object{$_.ipv4address -ne $null} | select -ExpandProperty name } Write-Verbose "Scanning Software ..." $s=Invoke-Command -ComputerName $a -erroraction SilentlyContinue -ErrorVariable disconnect{ param([string]$name) #如果是64位系统 if ([System.IntPtr]::Size -eq 4) { $result=Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object{$_.displayname -like "*$name*"} | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate, @{n='OS';e={(Get-WmiObject Win32_OperatingSystem).name.split("|")[0]}} #如果没有找到那么返回值为N/A的一个空的对象 if ($result -eq $null){ $info = "" | select Displayname,DisplayVersion, Publisher, InstallDate, OS $info.Displayname = 'N/A' $info.DisplayVersion='N/A' $info.Publisher='N/A' $info.InstallDate='N/A' $info.OS=Get-WmiObject -Class win32_operatingsystem | select -expand caption $info } else{ $result } } #如果是32位系统 else { $result=Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*,HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Where-Object{$_.displayname -like "*$name*"} | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate,@{n='OS';e={(Get-WmiObject Win32_OperatingSystem).name.split("|")[0]}} if ($result -eq $null){ $info = "" | select Displayname,DisplayVersion, Publisher, InstallDate, OS $info.Displayname = 'N/A' $info.DisplayVersion='N/A' $info.Publisher='N/A' $info.InstallDate='N/A' $info.OS=Get-WmiObject -Class win32_operatingsystem | select -expand caption $info } else{ $result } } } -ArgumentList $software if($disconnect.taregetobject -eq "*"){ Write-host "Disconnected Computers" -ForegroundColor red $disconnect.targetobject } return $s } Get-Software -software globalprotect -DN "ou=DDB Group Workstations,ou=DDB Group Machines, dc=omnicom,dc=com,dc=au" | Out-GridView
结果如下