Having problems with Get-WmiObject in AD

Welcome Forums General PowerShell Q&A Having problems with Get-WmiObject in AD

  • This topic has 12 replies, 5 voices, and was last updated 4 months ago by
    Participant
    .
Viewing 6 reply threads
  • Author
    Posts
    • #178965
      Participant
      Topics: 3
      Replies: 7
      Points: 45
      Rank: Member

      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.

    • #178971
      Participant
      Topics: 0
      Replies: 28
      Points: 142
      Helping Hand
      Rank: Participant

      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?

      • #178983
        Participant
        Topics: 3
        Replies: 7
        Points: 45
        Rank: Member
        $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
        }
        }
    • #178977
      Participant
      Topics: 5
      Replies: 105
      Points: 264
      Helping Hand
      Rank: Contributor

      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

      
      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
      }
      
      

      Or

      
      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
      }
      
      

       

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

       

      PCSystemType

      Data type: uint16
      Access type: Read-only
      Qualifiers: MappingStrings ("")

      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)

       

      • #178986
        Participant
        Topics: 3
        Replies: 7
        Points: 45
        Rank: Member

        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

         

      • #179004
        Participant
        Topics: 5
        Replies: 105
        Points: 264
        Helping Hand
        Rank: Contributor

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

         

      • #179007
        Participant
        Topics: 3
        Replies: 7
        Points: 45
        Rank: Member

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

    • #179010
      Participant
      Topics: 3
      Replies: 7
      Points: 45
      Rank: Member

      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?

       

      • #179022
        Participant
        Topics: 5
        Replies: 105
        Points: 264
        Helping Hand
        Rank: Contributor

        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?

    • #179052
      Participant
      Topics: 42
      Replies: 171
      Points: 610
      Helping Hand
      Rank: Major Contributor

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

    • #179079
      Participant
      Topics: 1
      Replies: 4
      Points: 28
      Rank: Member

      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)

    • #179124
      Participant
      Topics: 5
      Replies: 105
      Points: 264
      Helping Hand
      Rank: Contributor

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

      
      $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
      }
      }
      
      
      • #179547
        Participant
        Topics: 3
        Replies: 7
        Points: 45
        Rank: Member
        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 🙂
Viewing 6 reply threads
  • The topic ‘Having problems with Get-WmiObject in AD’ is closed to new replies.