Having problems with Get-WmiObject in AD

Hi all,

I posted this before and i know that i wrote that everything works fine but i was wrong.

I needed it for something in my job and we delayed the project and that’s why i didn’t use it up to now. The whole script eventually doing half the job.

Now, it seems that whenever i run the command where the computer name comes from the command "Get-ADComputer " it return an error of RPC on the “Get-WmiObject” but if i put manually the name of the computer it runs good.

 

The functions is:

#Function Desktop or Laptop
Function Detect-Laptop
{
Param ( [string] $comp)
$hardwaretype = (Get-Wmiobject -Class 'Win32_computersystem' -ComputerName $comp | Select-Object pcsystemtype)
if ($hardwaretype -ne 2)
{ $isLaptop = $true }
else {$isLaptop = $false}
$isLaptop
}

Thanks in advance.

Are you using this function in a pipeline or calls this function separately in the script?

Can you post the script block where you are calling this function?

Hi,
Your $hardwareType returns as an object so either add -expandProperty in the Select-Object or add .pcsystemtype to your if statetment. Also, I think you should have -eq instead of -ne

[pre]

Function Detect-Laptop
{
Param ( [string] $comp)
$hardwaretype = (Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $comp | Select-Object -ExpandProperty pcsystemtype)
if ($hardwaretype -eq 2)
{ $isLaptop = $true }
else
{$isLaptop = $false}
$isLaptop
}

[/pre]

Or

[pre]

Function Detect-Laptop
{
Param ( [string] $comp)
$hardwaretype = (Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $comp | Select-Object pcsystemtype)
if ($hardwaretype.pcsystemtype -eq 2)
{ $isLaptop = $true }
else
{$isLaptop = $false}
$isLaptop
}

[/pre]

 

https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem

 

PCSystemType
<dl>
<dt>Data type: uint16</dt>
<dt>Access type: Read-only</dt>
<dt>Qualifiers: MappingStrings ("")</dt>
</dl>
Type of the computer in use, such as laptop, desktop, or Tablet.

Unspecified (0)

Desktop (1)

Mobile (2)

Workstation (3)

Enterprise Server (4)

SOHO Server (5)

 

$SourceOU = "OU=TestOU,OU=******,DC=******,DC=local"
$Computers = Get-ADComputer -Filter * -SearchBase $SourceOU -Properties OperatingSystem
foreach ($Computer in $Computers)

{

If(Detect-Laptop $Computer.Name)
{
CheckOSlap $Computer
}
else 
{
CheckOSDesk $Computer
}
}

Thabks Aapeli Hietikko,

But that didn’t help. I’m getting the error on the Get-WmiObject command.

This is the error, where line 23 and char 18 is the start of the command in the Function

Get-Wmiobject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\Users\****\Desktop\Arrange_and Clean_Final_4_win10_only.ps1:23 char:18
+ $hardwaretype = (Get-Wmiobject -Class 'Win32_computersystem' -ComputerName $comp ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

 

Can you run it locally? Maybe this helps you: https://itluke.online/2017/11/08/solved-exception-from-hresult-0x800706ba/

 

I’m running the script from the AD server. We disabled firewalls on all comps in the domain by GPO.

Just a thought… when i run this command in power shell

Get-Wmiobject -Class 'Win32_computersystem' -ComputerName $comp | Select-Object -ExpandProperty pcsystemtype

Does it runs on the object in the AD or checks against the real computer?

 

it runs against the real computer and needs access there. I’d never run such script from domain controller. And how do you populate the $comp?

I suspect you’re getting that error as one of the servers listed in your AD doesn’t exist any more. Perhaps add a step to ping the servers before querying them…

Couple of suggestions:

1.) Can you add a write-output and gettype() to ensure your computername is actually a computername!

Function Detect-Laptop
{
Param ( [string] $comp)

write-output “Computer targetted = $comp”

$comp.gettype()
$hardwaretype = (Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $comp | Select-Object pcsystemtype)
if ($hardwaretype.pcsystemtype -eq 2)
{ $isLaptop = $true }
else
{$isLaptop = $false}
$isLaptop
}

2.) Can you run the command against a single server on its own (preferably a server that failed when run as part of the larger script):

(Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName server1 | Select-Object pcsystemtype)

I wouldnt use function at all, but I’d take a route like this.

[pre]

$adComputers = get-adcomputer -filter *

foreach ($adComputer in $adComputers)
{
try {
$wmiQuery = Get-Wmiobject -Class ‘Win32_computersystem’ -ComputerName $adComputer.name -ErrorAction Stop
if ($wmiQuery.pcsystemtype -eq 2) {$stat = “laptop”}
else {$stat = “somethingElse”}
} #Try
Catch {
$stat = “noConnection”
}

[PSCustomObject][Ordered]@{
‘ComputerName’ = $adComputer.name
‘status’ = $stat
}
}

[/pre]

Thanks Aapeli Hietikko,
I took your script and modified it to my needs and it works perfectly.
I was about to give up and thanks to you and others on this forum now i can continue with my project.
Cheers :-)