Я немного прибрал ваш код и добавил блок try{..} catch{..}
, потому что чтение значений реестра на удаленных машинах может привести к ошибкам по разным причинам.
Как и Симон Бегелунд, я добавил тест только для добавленияновый PSCustomObject к результатам, если вы действительно что-то нашли.
Важно, я думаю: При открытии ключей реестра всегда выполняйте очистку, закрывая их снова после того, как вы закончили их использовать.
В любом случае, вот код:
# the $Computers is an array of remote machine names
$Results = @()
ForEach ($Computer in $Computers) {
# don't know what the $Computers list contains, but.. is this correct?
$User = $computer -Replace '\D*\d*(\w*)', '$1'
# these properties are returned by default for Get-ADUser:
# DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
$ADUser = Get-ADUser -Server server.com -Identity $User
$CompProps = Get-ADComputer -Server server.com -Identity $Computer
$SID = $ADUser.SID.Value
$KeyPath = "$SID\Software\Microsoft\windows\Shell\Associations\UrlAssociations\http\UserChoice"
try {
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey("Users", $Computer)
$key = $reg.OpenSubKey($KeyPath)
$Browser = $key.GetValue('Progid')
if ($Browser) {
$Results += [PSCustomObject]@{
FullName = $ADUser.Name
UserName = $ADUser.SamAccountName
Computer = $CompProps.Name
DefaultBrowser = Switch ($Browser) {
'ChromeHTML' { "Google Chrome" }
'IE.HTTP' { "Internet Explorer" }
'AppXq0fevzme2pys62n3e0fbqa7peapykr8v' { "Microsoft Edge" }
}
}
}
}
catch {
Write-Warning "Could not open registry key on computer $computer. Reason: $($_.Exception.Message)"
}
finally {
# close the registry keys when done!
if ($key) { $key.Close() }
if ($reg) { $reg.Close() }
}
}
$Results