Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

If statement with -and Never worked

$
0
0

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? :( :(


Viewing all articles
Browse latest Browse all 13067

Trending Articles