1

This is the Powershell script I wrote:

$varCompList = Get-ADComputer -Filter "Name -like '*Name of Computers*'" -Properties OperatingSystemVersion | select DNSHostName, OperatingSystemVersion
foreach ($System in $varCompList){
    $Restult=switch ($System.OperatingSystemVersion){
        "10.0 (20348)"{"Server 2022"}
        "10.0 (19042)"{"Server 2019 20H2"}
        "10.0 (18363)"{"Server 2019 1909"}
        "10.0 (17763)"{"Server 2019 1809"}
        "10.0 (14393)"{"Server 2016"}
    }
}
echo $varCompList

It displays all the Servers like it should but the OperatingSystemVersion is still displayed as 10.0 (14393).

What am I missing?

4
  • $Restult is assigned but never used, later only $varCompList is printed. Is that a copy-paste error? Commented Mar 2, 2022 at 9:16
  • I know that $Result isn't used but I have no idea where to put it. If I echo the $Result, it'll only show the version of my own PC/Server this script runs on. I'd somehow have to combine those. Commented Mar 2, 2022 at 9:23
  • Replace $Restult= with $System.OperatingSystemVersion= Commented Mar 2, 2022 at 10:28
  • Yup, that worked. Thanks @MathiasR.Jessen Write this as an answer, if you will. Commented Mar 2, 2022 at 10:33

1 Answer 1

1

You're currently assigning the mapped OS names to a variable, but you never use it for anything and you never update the original input object.

Instead of assigning the result to a variable, assign it to the OperatingSystemVersion property on each object instead:

foreach ($System in $varCompList){
    $System.OperatingSystemVersion = switch ($System.OperatingSystemVersion){
        "10.0 (20348)"{"Server 2022"}
        "10.0 (19042)"{"Server 2019 20H2"}
        "10.0 (18363)"{"Server 2019 1909"}
        "10.0 (17763)"{"Server 2019 1809"}
        "10.0 (14393)"{"Server 2016"}
        default { $_ }
    }
}

The default case will ensure you preserve the original version string for any computer that doesn't have any of the listed versions installed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.