[英]trying to use Powershell to look in an OU in active directory for computers to see if they specific software
我對PowerShell還是很陌生,並且仍在學習如何使用它。 我看到我可以使用一個名為Get-WmiObject的cmdlet,但是我不確定這是正確的選擇。 我見過有人說使用它時,它可能會在用戶端引發錯誤,這會使用戶感到困惑。
因此,閑逛的人們說我可以在OU中查詢所有計算機的注冊表,該注冊表將在此處搜索“ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall”,並可能在此處搜索“ SOFTWARE \\ Wow6432Node \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall” ”。 我遇到的問題是我不確定要開始使用哪個cmdlet(如果根本沒有cmdlet),如何告訴它在特定注冊表中進行搜索,同時還告訴它要通過OU中的每台計算機?檢查。
我需要搜索所有安裝了“ Microsoft Office”的計算機。 誰能指出我正確的方向? 我該怎么做呢?
首先,您需要在OU中獲取計算機名稱的列表,為此,您需要獲取該OU的DistinghuishedName
屬性。 (在ADUC中-> OU屬性->屬性-> DistinghuishedName中查找)
使用該功能,您可以使用下面的功能測試已安裝的軟件:
function Get-InstalledSoftware {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
[string[]]$ComputerName = $env:COMPUTERNAME,
[string]$NamePattern = '*',
[switch]$ExcludeUpdates
)
begin {
$UninstallPaths = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\',
'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\'
}
process {
foreach ($computer in $ComputerName) {
if ([string]::IsNullOrEmpty($computer) -or $computer -eq '.') { $computer = $env:COMPUTERNAME }
# if the computername is its SamAccountName, it ends in a dollar sign.
$computer = $computer -replace '\$$', ''
if (!(Test-Connection -ComputerName $computer -Count 1 -Quiet)) {
Write-Warning "Computer '$computer' cannot be reached."
continue
}
$system = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
$baseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$computer)
foreach ($regPath in $UninstallPaths) {
$key = $baseKey.OpenSubKey($regPath)
# if the key exists
if ($key) {
$key.GetSubKeyNames() | ForEach-Object {
$subKey = $baseKey.OpenSubKey("$regPath$_")
$application = $subKey.GetValue('DisplayName')
if (($application) -and ($application -like $NamePattern)) {
if (!$ExcludeUpdates -or ($application -notlike "*update*")) {
[PSCustomObject]@{
'Computer' = $system.Name
'Application' = $application
'Version' = $subKey.GetValue('DisplayVersion')
'InstallLocation' = $subKey.GetValue('InstallLocation')
'UninstallString' = $subKey.GetValue('UninstallString')
'Publisher' = $subKey.GetValue('Publisher')
'LoggedOnUser' = $system.UserName
}
}
}
# close $subKey
if ($subKey) { $subKey.Close() }
}
# close $key
if ($key) { $key.Close() }
}
}
# close $baseKey
if ($baseKey) { $baseKey.Close() }
}
}
}
注意:如果您具有PowerShell 3.0或更高版本,則可以將行$system = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $computer
$system = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -Verbose:$false
為$system = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $computer -Verbose:$false
這將執行快點
像這樣使用它:
Import-Module ActiveDirectory
$OuDn = 'DistinghuishedName of the OU you are interested in'
# get a list of computer names in the given OU
$computers = Get-ADComputer -Filter * -SearchBase $OuDn | Select-Object -ExpandProperty Name
# use the function to get a list of installed applicationa
$software = Get-InstalledSoftware -ComputerName $computers -NamePattern "Microsoft Office*" -ExcludeUpdates
# output to console or export to a CSV file
$software | Export-Csv -Path 'D:\software.csv' -NoTypeInformation -Encoding UTF8
注意:此功能可以在您的PC上完成所有工作,因此必須確保以具有讀取所有計算機上注冊表項權限的用戶身份運行它。
另外,為了遠程打開密鑰,服務器和客戶端計算機都必須正在運行遠程注冊表服務 ,並且啟用了遠程管理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.