В личке добавилось требование обработчика ошибок.
Код:
$FilePath = "C$\Core Test\System\Core.exe"
$SearchBase = "OU=OFT,OU=SU,OU=PRM-CN,DC=contoso,DC=com"
$RegPath1 = "SOFTWARE\Soft Practic\RUSSIA"
$RegPath2 = "SOFTWARE\Soft Practic\Ver"
$RegKeyName1 = "System Version"
$RegKeyName2 = "Version"
$Info = @()
Get-AdComputer `
-Filter {Name -like "msk-004*" -and Enabled -eq $True} `
-SearchBase $SearchBase `
-Properties Name, CanonicalName | %{
$CompName = $_.Name
If ( Test-Connection $CompName -Count 1 -Quiet ) {
[string]$IPAddresses = ""
Try {
$RegErr = $False
Set-Service RemoteRegistry -ComputerName $CompName -Status Running -PassThru -EA SilentlyContinue | Out-Null
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $CompName )
} Catch {
$RegErr = $True
}
Try {
$NICErr = $False
$NIC = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled='True'" -ComputerName $CompName -EA SilentlyContinue
$NIC | %{ $_.IPAddress.Split(",") | ?{ $_ -notlike "*:*" }} | %{ $IPAddresses += ($_ + ",") }
$IPAddresses = ( $IPAddresses -replace (',$') )
} Catch {
$NICErr = $True
}
If ($RegErr) {
$RegVersion = "Registry connection error"
$Setup = "Registry connection error"
} Else {
$RegKey1 = $Reg.OpenSubKey($RegPath1)
$RegKey2 = $Reg.OpenSubKey($RegPath2)
$RegVersion = $RegKey1.GetValue($RegKeyName1)
$Setup = $RegKey2.GetValue($RegKeyName2)
}
If ($NICErr) {
$IPAddresses = "WMI Connection error"
}
If (Test-Path $("\\" + $CompName + "\" + $FilePath) ) {
$FileVersion = (Get-Item $("\\" + $CompName + "\" + $FilePath) ).VersionInfo.ProductVersion
} Else {
$FileVersion = ""
}
Set-Service RemoteRegistry -ComputerName $CompName -Status Stopped -PassThru -ErrorAction SilentlyContinue | Out-Null
} Else {
$IPAddresses = "Offline"
$RegVersion = ""
$Setup = ""
$FileVersion = ""
}
$Member = New-Object PSObject -Property @{
CompName = $CompName
CanonicalName = $_.CanonicalName
IPAddresses = $IPAddresses
RegVersion = $RegVersion
Setup = $Setup
FileVersion = $FileVersion
}
$Info += $Member
}
$Info | Out-GridView
# $Info | Export-CSV C:\Temp\ScanResult.CSV -Encoding UTF8 -Delimiter ";" -NoTypeInformation