Updated powershell script does not seem to like windows 10 20H2

Kevin Cornett 31 Reputation points
2020-12-09T18:29:01.467+00:00

I have this script I've used to get windows versions on our domain computers, and it has always worked perfectly. I've just updated it to find computers that have installed Windows 10 19042 20H2. But now I get nothing but errors. My thought is the H in the version number is possibly causing this problem, but then I'm dumb and can't be trusted with my suspicions. Any thoughts on the matter are very much appreciated. I'll include the code with domain information redacted.

$List = Get-ADComputer -SearchBase "ou=<REDACTED>,dc=<REDACTED>,dc=com" -filter {enabled -eq $true} -properties OperatingSystemVersion
foreach ($System in $List) {
    $Result = switch ($System.OperatingSystemVersion){

            "10.0 (10240)" {1507}
            "10.0 (10586)" {1511}
            "10.0 (14393)" {1607}
            "10.0 (15063)" {1703}
            "10.0 (16299)" {1709}
            "10.0 (17134)" {1803}
            "10.0 (17763)" {1809}
            "10.0 (18362)" {1903}
            "10.0 (18363)" {1909}
            "10.0 (19041)" {2004}
            "10.0 (19042)" {20H2}

        }
        set n=0
        if ($Result -eq '20H2') {

        echo $System.Name >> c:\apps\20H2_mo_windows_versions.txt
        echo $System.OperatingSystemVersion >> c:\apps\20H2_mo_windows_versions.txt;
        echo $Result >> c:\apps\20H2_mo_windows_versions.txt
        echo _____________________ >> c:\apps\20H2_mo_windows_versions.txt
        set /a n=%n%+1 
        }
        }
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,397 questions
Windows 10 Compatibility
Windows 10 Compatibility
Windows 10: A Microsoft operating system that runs on personal computers and tablets.Compatibility: The extent to which hardware or software adheres to an accepted standard.
472 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,504 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 46,396 Reputation points
    2020-12-09T20:15:34.503+00:00

    All the other build "numbers" as composed of digits. You need to make 20H2 a string.

    "10.0 (19042)" { "20H2" }
    

    You did that when you compared $Result -eq '20H2'.


2 additional answers

Sort by: Most helpful
  1. Bill Stewart 181 Reputation points
    2020-12-09T21:26:31.19+00:00

    I don't think this line of code:

    set n=0
    

    ...does what you think it does. With default aliases in PowerShell, this will set a variable named n=0 with an empty (null) value.

    Also you have this:

    set /a n=%n%+1
    

    That's not going to work in PowerShell either. PowerShell doesn't use % delimiters for environment variables, and the set command is by default aliased to the Set-Variable cmdlet.

    If you goal is to set a PowerShell variable to 0 and increment it, you need these lines instead:

    $n = 0
    

    and

    $n++
    
    1 person found this answer helpful.

  2. Kevin Cornett 31 Reputation points
    2020-12-09T18:29:56.187+00:00

    And here is the error:

    In the error, the "20H2" is what is underlined as the offending bit of code, in case the formatting is off in my reply. Here it is (again, with company identifying information redacted):

    20H2 : The term '20H2' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.
    At C:\<REDACTED>\Desktop\My Scripts\PowerShell\windows version\20H2\MO_windows_version.ps1:15 char:23

    • "10.0 (19042)" {20H2}
    • ~~~~
    • CategoryInfo : ObjectNotFound: (20H2:String) [], CommandNotFoundException
    • FullyQualifiedErrorId : CommandNotFoundException
    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.