Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - how to replace OS Version number with String

I am querying remote servers for their operating system. I know that I can return the Version, but I want to replace these values with the friendly name. The code I have so far is:

$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
   If ({$BuildVersion.Version -match "5.2.3790"}) 
          {$Build="2003"}
   Elseif ({$BuildVersion.Version -match "6.1.7601"}) 
           {$Build="2008"}
   Elseif ({$BuildVersion.Version -like "6.3.9600"}) 
          {$Build="2012"} 

But this doesn't seem to work and only returns "2003" regardless. Please help, I'm fairly new to PS and coding.

thanks

like image 335
TOGEEK Avatar asked Feb 06 '23 16:02

TOGEEK


2 Answers

The problem is your if statements. Putting the Boolean expression inside squiggly brackets makes it a script block, and that's going to get cast as a string before being cast as a Boolean. Strings cast to Booleans always evaluate to true unless they're empty.

PS C:\> {$BuildVersion.Version -match "5.2.3790"}
$BuildVersion.Version -match "5.2.3790"
PS C:\> ({$BuildVersion.Version -match "5.2.3790"}) -as [bool]
True
PS C:\> $BuildVersion.Version -match "5.2.3790"
False
PS C:\> ($BuildVersion.Version -match "5.2.3790") -as [bool]
False

So what you're running is essentially:

if ([bool]'$BuildVersion.Version -match "5.2.3790"') [...]

And that's always going to be true.

Try:

$Computer = (gc c:\servers.txt)
$BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $Computer -ErrorAction SilentlyContinue
$Build=$BuildVersion.version
If ($BuildVersion.Version -match "5.2.3790") 
{
    $Build = "2003"
}
Elseif ($BuildVersion.Version -match "6.1.7601") 
{
    $Build = "2008"
}
Elseif ($BuildVersion.Version -like "6.3.9600") 
{
    $Build = "2012"
}

Bottom line is that squiggly brackets are not parentheses and you can't use them like they are.

However, there's also a major logic error here. You're potentially fetching an array for $BuildVersion because you're reading from a file, but then you treat it like a single value. You never loop through $BuildVersion. However, I do not have enough information about what you're actually trying to do with your script (like what you do with $Build) to be able to fix that.

like image 80
Bacon Bits Avatar answered Feb 15 '23 00:02

Bacon Bits


I originally said this, but I've since changed my mind

The reason this is only returning 2003 is that you're only running your If code on a single entry in the list.

Wrong

As TessellatingHeckler says, the reason your if wasn't working is that you had too many curly braces, so PowerShell wasn't actually evaluating your logic.

However, you still need to step through each of the computers to do what you're trying to do. We'll do that by adding in a ForEach loop. I also went ahead and replaced your If {} logic with a Switch statement, which I think is easier to understand for a scenario like this with multiple clauses. If's just get way too verbose.

Finally, I'm assuming you want to output the results too, so I added a custom object here, which is just a way of choosing which properties we want to display.

$Computer = (gc c:\servers.txt)
ForEach ($system in $computer){
    $BuildVersion = Get-WmiObject -Class Win32_OperatingSystem -Property Version, CSName -ComputerName $system -ErrorAction SilentlyContinue 
    $Build=$BuildVersion.version

    switch ($build){
        "5.2.3790" {$Build="2003"}
        "6.1.7601" {$Build="2008"}
        "6.3.9600" {$Build="2012"}

    }

    #output results
    [pscustomobject]@{Server=$system;OSVersion=$build;CSName=$buildVersion.CSname}        
}#EndOfForEach

Output

>Server   OSVersion CSName  
------   --------- ------  
dc2012   2012      DC2012  
sccm1511 2012      SCCM1511
like image 30
FoxDeploy Avatar answered Feb 14 '23 23:02

FoxDeploy