Author Posts

July 15, 2014 at 9:26 pm

Hey Guys

I have problem with using the if statement with the -and. I've tried several times but never worked, I am writing this simple script and I really need someone to tell me what I am doing wrong. Simply when I run this script against a specific computer list I want it to check if the OS Architecture is (x64) and the OS Edition is Windows 7 or not.

$Computers = Get-Content "F:\WindowsManagementFramework4.0\Computer Lists\Stores\Hollywood.txt"

Foreach ($computer in $computers)
{
    $OSArch = (Get-WmiObject -Class Win32_Operatingsystem -ErrorAction SilentlyContinue -ComputerName $computer).OSArchitecture
    $OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction SilentlyContinue).caption

  If (($OSArch -eq "64-Bit") -and ($OS -eq "Microsoft Windows 7 Enterprise"))
  {
  Write-Host "Update can be Installed to the x64 Machine"}
  
  Else{
  Write-Host "Update cannot be Installed"}
  }

So basically if the computers in the list are (x64 and Windows 7) the outcome should be (Update can be Installed to the x64 Machine) and if they'r not the outcome should be (Update cannot be Installed) but all the result am getting is (Update cannot be Installed) even though 3 computers are x86 and the rest are x64 as you can see below.

PS C:\WINDOWS\system32> $Computers = Get-Content "F:\WindowsManagementFramework4.0\Computer Lists\Stores\Hollywood.txt"

Foreach ($computer in $computers)
{
    $OSArch = (Get-WmiObject -Class Win32_Operatingsystem -ErrorAction SilentlyContinue -ComputerName $computer).OSArchitecture
    $OS = (Get-WmiObject Win32_OperatingSystem -ComputerName $Computer -ErrorAction SilentlyContinue).caption

  If (($OSArch -eq "64-Bit") -and ($OS -eq "Microsoft Windows 7 Enterprise"))
  {
  Write-Host "Update can be Installed to the x64 Machine"}
  
  Else{
  Write-Host "Update cannot be Installed"}
  }

Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed
Update cannot be Installed

I ran this simple script to show the architecture of all the machines in the list.

(Get-WmiObject -Class Win32_Operatingsystem -ErrorAction SilentlyContinue -ComputerName $computers).OSArchitecture

Result as I've said was 3 machines x86 and the rest are x64

PS C:\WINDOWS\system32> (Get-WmiObject -Class Win32_Operatingsystem -ErrorAction SilentlyContinue -ComputerName $computers).OSArchitecture
32-bit
32-bit
32-bit
64-bit
64-bit
64-bit
64-bit
64-bit

So what I've done wrong? 🙁 🙁

July 15, 2014 at 11:47 pm

Hi Mahmoud,

I suspect the Caption of Win32_OperatingSystem is not set to what you're expecting. Please run below and post the output:

(Get-WmiObject Win32_OperatingSystem -ComputerName $Computers -ErrorAction SilentlyContinue).Caption

Regards,
Daniel

July 15, 2014 at 11:47 pm

Hi

Just tested this out on my computer and noticed that if I run only:

 If ($OS -eq "Microsoft Windows 7 Enterprise")

It would still evaluate to be false.
Checking the output shows that it for some reason adds an extra space after "...Enterprise"

So if you just change this:

If (($OSArch -eq "64-Bit") -and ($OS -eq "Microsoft Windows 7 Enterprise"))

To this:

If (($OSArch -eq "64-Bit") -and ($OS -eq "Microsoft Windows 7 Enterprise "))

–Notice the extra space between Enterprise and "

You would get the result you're after.

July 15, 2014 at 11:49 pm

You can trim the $OS string or only match what you're looking for:

Trim example:

If (($OSArch -eq "64-Bit") -and ($OS.Trim() -eq "Microsoft Windows 7 Enterprise"))

Match example:

If (($OSArch -eq "64-Bit") -and ($OS -match "Microsoft Windows 7 Enterprise"))

July 16, 2014 at 12:59 am

other alternative is to use -like

If (($OSArch -eq "64-Bit") -and ($OS -like "*Windows 7 Enterprise*"))

July 16, 2014 at 5:12 am

You could also use the version and then not worry about parsing a string:

PS C:\> [System.Version]$OS = gwmi Win32_OperatingSystem | Select -ExpandProperty Version

PS C:\> If ($OS -gt [System.Version]"6.0.0") { "Windows 7 or higher OS"}
Windows 7 or higher OS

PS C:\> If ($OS -gt [System.Version]"6.8.0") { "Windows 7 or higher OS"}

July 17, 2014 at 2:22 pm

Thanks guys so much for you replies I will check all of these stuff and will get back to you by the results. Appreciate your help so much.

July 17, 2014 at 4:51 pm

[b]Daniel Krebs[/b]

Mate you are a legend I never noticed the space between the (Enterprise and ") I didn't try the trim one because adding the space sorted out the problem am still thinking how did you even noticed it :D, really appreciate your help.

[b]Richard Siddaway [/b]

Thanks man and I liked the your (*) trick it did work too really cool. Thanks man.

[b]Rob Simmers[/b]

Thanks buddy for your help but , it was bit confusing to get what you wrote but I got it all at the end. and realized that

 { "Windows 7 or higher OS"} 

like if I used Write-Host.

Thank you all fellas 3 amazing answers and all work very impressive.