# Having problems with Get-WmiObject in AD

**URL:** https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180
**Category:** PowerShell Help
**Created:** [September 23, 2019, 5:26am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180 "2019-09-23T05:26:43Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 23, 2019, 5:26am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/1 "2019-09-23T05:26:43Z")

</div>

Hi all,

I posted [this](https://powershell.org/forums/topic/function-that-will-determined-if-a-computer-is-laptop-or-desktop/) 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.

&nbsp;

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.

---

<div class="post-metadata">

### Author: ![rejikodiyil](https://avatars.discourse-cdn.com/v4/letter/r/ba8739/32.png) [@rejikodiyil](https://forums.powershell.org/u/rejikodiyil)
#### Post date: [September 23, 2019, 5:37am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/2 "2019-09-23T05:37:56Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)
#### Post date: [September 23, 2019, 5:46am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/3 "2019-09-23T05:46:39Z")

</div>

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]

&nbsp;

[https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem](https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-computersystem)

&nbsp;

**PCSystemType**  
\<dl\>  
\<dt\>Data type: **uint16** \</dt\>  
\<dt\>Access type: Read-only\</dt\>  
\<dt\>Qualifiers: [**MappingStrings**](https://docs.microsoft.com/windows/desktop/WmiSdk/standard-qualifiers) (“”)\</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)

&nbsp;

---

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 23, 2019, 6:05am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/4 "2019-09-23T06:05:31Z")

</div>

```
$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
}
}
```

---

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 23, 2019, 6:09am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/5 "2019-09-23T06:09:35Z")

</div>

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
```

&nbsp;

---

<div class="post-metadata">

### Author: ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)
#### Post date: [September 23, 2019, 7:23am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/6 "2019-09-23T07:23:49Z")

</div>

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

&nbsp;

---

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 23, 2019, 7:29am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/7 "2019-09-23T07:29:37Z")

</div>

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

---

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 23, 2019, 7:45am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/8 "2019-09-23T07:45:49Z")

</div>

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?

&nbsp;

---

<div class="post-metadata">

### Author: ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)
#### Post date: [September 23, 2019, 7:59am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/9 "2019-09-23T07:59:38Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)
#### Post date: [September 23, 2019, 9:08am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/10 "2019-09-23T09:08:47Z")

</div>

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…

---

<div class="post-metadata">

### Author: ![showboat](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@showboat](https://forums.powershell.org/u/showboat)
#### Post date: [September 23, 2019, 10:31am UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/11 "2019-09-23T10:31:28Z")

</div>

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)`

---

<div class="post-metadata">

### Author: ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)
#### Post date: [September 23, 2019, 12:29pm UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/12 "2019-09-23T12:29:57Z")

</div>

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]

---

<div class="post-metadata">

### Author: ![dady](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dady/32/1710_2.png) [@dady](https://forums.powershell.org/u/dady)
#### Post date: [September 24, 2019, 11:24pm UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/13 "2019-09-24T23:24:40Z")

</div>

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 :-)

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:31pm UTC](https://forums.powershell.org/t/having-problems-with-get-wmiobject-in-ad/13180/14 "2024-05-16T20:31:19Z")

</div>


