Multiple Get-CimInstance

Guys.

Banging my head against the wall slightly with this one.  I am looking to pull a list of AD names out of a certain OU and pass them along in the script to use.

I am looking to query the machine and obtain the following

Computer Name , Logged On User, Last Reboot time.

Now if i remove the line below then the script runs and returns Computer Name and Logged On user.  However i need last reboot date.

$Reboot = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $PSItem | Select-Object -ExpandProperty lastbootuptime

--------------

The code i have so far is below.

$Computers = Get-QADComputer -SizeLimit 0 -SearchRoot "OU PATH HERE" |
Select-Object -ExpandProperty Name

$Computers | ForEach-Object {
$User = Get-CimInstance Win32_ComputerSystem -ComputerName $PSItem | Select-Object -ExpandProperty UserName
$Reboot = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $PSItem | Select-Object -ExpandProperty lastbootuptime


$Obj = New-Object -TypeName PSObject -Property @{
"Computer" = $PSItem
"User" = $User
"Reboot" =  $Reboot
}
Write-Output $Obj}

July 6th, 2015 10:39am

Am not sure i get the script with some of the VAR try like this for me please. 

$Computers = Get-QADComputer -SizeLimit 0 -SearchRoot "OU PATH HERE" | Select-Object -ExpandProperty Name

foreach ($computer in $Computers)
{
    $User = Get-CimInstance Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty UserName
    $Reboot = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $computer | Select-Object -ExpandProperty lastbootuptime


    $Obj = New-Object -TypeName PSObject -Property @{
        "Computer" = $computer
        "User" = $User
        "Reboot" =  $Reboot
}
Write-Output $Obj}

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 12:16pm

Thanks for the reply. So your changes produces the same error my code is giving. It will resolve the Computer but not the User or Reboot.

Reboot                                     User                                    Computer                                 
------                                         ----                                       --------                                 
                                                                                            Computer01                                
Get-CimInstance : The WinRM client cannot process the request because the server name cannot be resolved.

July 6th, 2015 12:26pm

well this mean you don't have access to the remote computer you are trying to get access to.

Run the following locally on 1 computer and replace the second variable in computers. you will see you will be getting what you need. After you will need to find out why you can`t do WMI query remotely.

The users need to have the proper access and the port use are 135 after that DCOM respond to the dynamic port.

$computers = ("localhost","enter local computername")

foreach ($computer in $Computers)
{
    $User = Get-CimInstance Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty UserName
    $Reboot = Get-CimInstance -ClassName win32_operatingsystem -ComputerName $computer | Select-Object -ExpandProperty lastbootuptime


    $Obj = New-Object -TypeName PSObject -Property @{
        "Computer" = $computer
        "User" = $User
        "Reboot" =  $Reboot
}
Write-Output $Obj}

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 12:58pm

Thanks for the reply. So your changes produces the same error my code is giving. It will resolve the Computer but not the User or Reboot.

Reboot                                     User                                    Computer                                 
------                                         ----                                       --------                                 
                                                                                            Computer01                                
Get-CimInstance : The WinRM client cannot process the request because the server name cannot be resolved.

The error has nothing to do with DCOM. The computer name cannot be resolved via DNS.

Try this:
 Get-QADComputer  -SearchRoot "OU PATH HERE" |%{Test-Connection $_.Name}

YOU likely have computers that are in AD but not registered in DNS.  Perhaps they have been disabled or removed from the network.

July 6th, 2015 1:15pm

Thanks for the replies so far. If I run each of the get commands one at a time it will return the correct value. It's not a DNS issue, as said above my original code and the updates code from Frederick return the computer name with no issue but don't return the logged on username Or last reboot when all of the code is used together. One at a time no problem. $computer = "computer1" Get-CimInstance Win32_ComputerSystem -ComputerName $computer | Select-Object -ExpandProperty UserName Get-CimInstance -ClassName win32_operatingsystem -ComputerName $computer | Select-Object -ExpandProperty lastbootuptime
Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 1:32pm

The only way you can get that error is if the computer name is not resolved.  Try running the snippet I posted.

July 6th, 2015 1:42pm

Here is a cleaner method and should avoid some of your possible issues.  If there is an error please post the complete error message.

Get-QADComputer -SizeLimit 0 -SearchRoot "OU PATH HERE" |
    ForEach-Object {
        $comp=Get-CimInstance Win32_ComputerSystem -ComputerName $_.Name
        $reboot=Get-CimInstance win32_operatingsystem -ComputerName $_.Name   
        New-Object -TypeName PSObject -Property @{
            Computer=$_.Name
            User= $comp.UserName
            Reboot=$reboot.LastBootupTime
       }
    }

Free Windows Admin Tool Kit Click here and download it now
July 6th, 2015 1:46pm

Here's a script I use as CIM doesn't work 100% in my environment (still supporting 2003 servers with Powershell v2).   As for getting the logged on user, I like to cheat and just look at the last modified folder in c:\users filtering out other service account names.  And for getting computers in a particular OU, I first filter out computer accounts that are disable and that have a modify date older than 1 month (computer is likely offline), and I also filter out non windows operating systems (like network appliances, CNO, etc).

Get-QADComputer -SearchRoot "OU PATH HERE" |

%{!$_.AccountIsDisabled}|

%{$_.OperatingSystem -like "*windows*"}|

%{(Get-Date).CompareTo($_.ModifficationDate.addDays(30)) -le 0}


 
July 6th, 2015 5:10pm

Thanks for the reply.  So the out put of the script is below. So its displaying the the last reboot time but not the username or the computer.

Reboot                                     User                                       Computer                                 
------                                          ----                                         --------                                 
15/06/2015 16:08:28                                                                                                           

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 4:39am

Thanks for the reply.  So the out put of the script is below. So its displaying the the last reboot time but not the username or the computer.

Reboot                                     User                                       Computer                                 
------                                          ----                                         ----
15/06/2015 16:08:28


Edit

Just noticed that the reboot time is the reboot time of the machine i am running the script from. Not the computer that should be getting pulled from AD. So it looks like its not passing the computer names.

July 7th, 2015 4:39am

Thanks for the reply.  So the out put of the script is below. So its displaying the the last reboot time but not the username or the computer.

Reboot                                     User                                       Computer                                 
------                                          ----                                         ----
15/06/2015 16:08:28


Edit

Just noticed that the reboot time is the reboot time of the machine i am running the script from. Not the computer that should be getting pulled from AD. So it looks like its not passing the computer names.

Free Windows Admin Tool Kit Click here and download it now
July 7th, 2015 8:38am

Guys.

Just bumping this up again. Still banging my head with this one

July 8th, 2015 5:05am

OK.

So I have been playing around and I now have what I want. The code I have so far is below.

---------------------------

$ADComputers = Get-QADComputer -SizeLimit 0 -SearchRoot "OU NAME HERE" |


ForEach-Object {
$LoggedOnUser = Get-WmiObject win32_computersystem -Computer $_.name | Select username
$RebootTime = Get-WmiObject -class Win32_OperatingSystem -ComputerName $_.name | Select-Object  __SERVER,@{label='LastBootUpTime';expression={$_.ConvertToDateTime($_.LastBootUpTime)}}
$Proc = Get-WmiObject Win32_processor -ComputerName $_.name | Select-Object -First 1

$Object = New-Object PSObject -Property @{

ComputerName           = $proc.SystemName
LoggedOnUser           = $LoggedOnUser.username
RebootTime             = $RebootTime.LastBootUpTime
}
}
Write-Output $Object

Free Windows Admin Tool Kit Click here and download it now
July 8th, 2015 7:24am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics