Christian
Christian

Reputation: 37

Discovery of OS version not Windows 10 1809 across the network Powershell command

I need to check which devices in our network, Which does not have the OS version 1809 Windows 10 and I need to do a scan to determine those devices pulling data from AD.

  Try{$Domain = $(get-addomain).dnsroot}
  Catch{$Domain = ""}

  $Log = "C:\Temp\Audit\$Domain OS Compliance $(get-date -f yyyy-MM-dd).csv"

  $Computers = Get-ADComputer -Filter {Enabled -eq $True} -Property * | 
  Select Enabled,Name,OperatingSystem,OperatingSystemVersion

  foreach ($Computer in $Computers)
  {
   #properties
   $Version = $Computer.OperatingSystemVersion
   $Enabled = $Computer.Enabled
   $Name = $Computer.Name
   $OS = $Computer.OperatingSystem
  }

  #Windows10 Build 1908 check
  If($OS -like "Windows 10*")
  {
      $Type = "Computer"
      $CountComputers++
      $Build = "1908"


   $obj = New-Object PSobject
   $obj | Add-Member NoteProperty -Name "Enabled" -Value $Enabled
   $obj | Add-Member NoteProperty -Name "Name" -Value $Computer.Name
   $obj | Add-Member NoteProperty -Name "Operating System" -Value 
       $Computer.OperatingSystem
   $obj | Add-Member NoteProperty -Name "Version" -Value $Version
   $obj | Add-Member NoteProperty -Name "Build" -Value $Build
  }

  $OutData = $OutData | sort -Property "Type","Name"
  $OutData | Export-CSV $Log -notype -Encoding UTF8
  write-host "Log Export Complete to $Log" -foregroundcolor yellow

I seem to be doing something wrong calling out the query I am not that well versed with Powershell anyone can share me or give me any inputs so i can formulate my own code would help out

Upvotes: 2

Views: 981

Answers (1)

codewario
codewario

Reputation: 21408

From a computer with RSAT tools installed (or from a DC) you can run the following using an account that can access all PCs/servers in the domain:

$computers = ( Get-ADComputer -Filter * -Properties DNSHostName ).DNSHostName
$results = Invoke-Command -ComputerName $computers {
  switch ( [System.Environment]::OsVersion.Version.Major ) {
    10 {
      [PSCustomObject]@{ AtLeast1809 = [System.Environment]::OsVersion.Version.Build -ge 18362 }
      break
    }
    default {
      [PSCustomObject]@{ AtLeast1809 = $False }
    }
  }
}

$results | Where-Object { -Not $_.AtLeast1809 } | Select-Object PSComputerName, AtLeast1809

This code looks at the current major version, if it's not 10, we automatically return $false. If it's Windows 10, we check the build version. Once Invoke-Command runs on all computers in the domain, we then output from the $results collection for any computers where the version is not at least Windows 10 1809, then select the PSComputerName property (an automatic property added when using Invoke-Command to note which computer the result came from) and the AtLeast1809 property which we returned from the remote computer.

Note that you may not want to run this against every single computer/server in your domain at once. You may want to adjust the filter parameter to select only computers you want in batches.

Here is the lookup table for Windows 10 versions. 1809 is build 18362.

Upvotes: 1

Related Questions