System version with script powershell

David Sarrià Ibarra 1 Reputation point
2022-10-14T15:32:02.567+00:00

Hi,

I need know what build version have the all computers in my company. I try with this script but I get the same error. Where is the problem?

Get-ADComputer -Filter * -Properties OperatingSystem | Get-CimInstance -ClassName Win32_OperatingSystem

Error:

+ ... erties OperatingSystem | Get-CimInstance -Class Win32_OperatingSystem  
+                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
    + CategoryInfo          : InvalidArgument: (:) [Get-CimInstance], CimException  
    + FullyQualifiedErrorId : HRESULT 0x80041017,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand  
   
Get-CimInstance : Invalid query  

I never programmed in powershell, is my first time.

Regards,

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
40,239 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,576 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,466 Reputation points
    2022-10-14T19:40:26.843+00:00

    If you have a lot of computers to check then you'll want to introduce some way to do concurrent queries, especially if you have computers that may be offline or inaccessible.

    Here's a version that used jobs to run up to 32 queries at the same time:

    $cn = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name  
    foreach ($name in $cn){  
        Start-Job -ScriptBlock {  
            $dd = Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Using:name 2>&1 # redirect STDERR to STDOUT  
            # you should probably check the object type of $dd here to see if it's an ErrorRecord instead of  
            # blindly accepting it as the output that contains the data you expected!  
            # if it's an ErrorRecord you should build another PSCustomObject and place  
            # the result in either Version of Build  
            [PSCustomObject]@{  
                Name = $Using:name  
                Version = $dd.Version  
                Build = $dd.BuildNumber  
            }  
        } | Out-Null  
    }  
      
    Do{  
        Foreach ($j in (Get-Job -State Completed)) {  
            Receive-Job $j.Id |  
                Select-Object Name,Version,Build  
            Remove-Job $j.id | Out-Null  
        }  
    } While (Get-Job)  
    
    0 comments No comments

  2. Rich Matheisen 47,466 Reputation points
    2022-10-15T14:32:46.27+00:00

    This might be a less resource intensive way of gathering the same information:

    $cn = Get-ADComputer -Filter * | Select-Object -ExpandProperty Name  
    Invoke-Command -ComputerName $cn -ScriptBlock{  
        $dd = Get-WmiObject -ClassName Win32_OperatingSystem -ComputerName $Using:name 2>&1 # redirect STDERR to STDOUT  
        # you should probably check the object type of $dd here to see if it's an ErrorRecord instead of  
        # blindly accepting it as the output that contains the data you expected!  
        # if it's an ErrorRecord you should build another PSCustomObject and place  
        # the result in either Version of Build  
        [PSCustomObject]@{  
            Name = $Using:name  
            Version = $dd.Version  
            Build = $dd.BuildNumber  
        }    
    }  
    

    My previous answer is more suitable for long-running tasks.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.