I am having a problem with the result from this code. For some reason, the Distriguished Name portion of the script is giving the value of my workstation instead of the Distinguished Name of the servers in the array…
Function WMILookup {
foreach ($StrComputer in $colComputers){
$GenItems1 = gwmi Win32_OperatingSystem -Comp $StrComputer
$Printers = gwmi Win32_Printer -computername $StrComputer
# Populate General Sheet(1) with information
foreach ($objItem in $GenItems1){
$Sheet1.Cells.Item($intRow, 1) = $StrComputer
$Sheet1.Cells.Item($intRow, 2) = $objItem.Caption
$Sheet1.Cells.Item($intRow, 3) = $objItem.CSDVersion
}
foreach ($objItem in $Printers){
$Sheet1.Cells.Item($intRow, 4) = $objItem.Shared
}
$de = New-Object System.DirectoryServices.DirectoryEntry
$ds = New-Object System.DirectoryServices.DirectorySearcher
$ds.SearchRoot = $de
$ds.Filter = "(&(objectCategory=computer)(objectClass=computer) (samAccountName=$($env:Computername)$))"
$ds.SearchScope = "SubTree"
$r = $ds.FindOne()
$r.Path
$Sheet1.Cells.Item($intRow, 5) = $r.Path
$intRow = $intRow + 1}
}
It gives you what you ask him:
$env:Computernameis the computer name where the function is executed.maybe you want this: