none
IF true - do these 2 things RRS feed

  • Question

  • HI All,

    I'm working on my first really functional PSH script. 

    I have an IF statement that if TRUE I would need it to run 2 things, I'm not that you can do, that since I can't find any examples, but it would look like this.. maybe...  I'm i going about this all wrong?

    $Win32OS = [WMICLASS]"\\.\ROOT\CIMV2:Win32_OperatingSystem"
    $Architecture =  $Win32OS.OSArchitecture
    $OSCaption = $Win32OS.Caption
    $Win32Product = [WMICLASS]"\\.\ROOT\CIMV2:win32_Product"

    IF ($OSCaption -eq 'Microsoft Windows XP Professional')
    {
     $Win32Product.Install($WIN32_Path_IE,"ADDLOCAL=ALL",$true),$Win32Product.Install($WIN32_Path_FF,"ADDLOCAL=ALL",$true)
    }

    Thursday, January 26, 2012 9:47 PM

Answers

  • HI All,

    I'm working on my first really functional PSH script. 

    I have an IF statement that if TRUE I would need it to run 2 things, I'm not that you can do, that since I can't find any examples, but it would look like this.. maybe...  I'm i going about this all wrong?

    $Win32OS = [WMICLASS]"\\.\ROOT\CIMV2:Win32_OperatingSystem"
    $Architecture =  $Win32OS.OSArchitecture
    $OSCaption = $Win32OS.Caption
    $Win32Product = [WMICLASS]"\\.\ROOT\CIMV2:win32_Product"

    IF ($OSCaption -eq 'Microsoft Windows XP Professional')
    {
     $Win32Product.Install($WIN32_Path_IE,"ADDLOCAL=ALL",$true),$Win32Product.Install($WIN32_Path_FF,"ADDLOCAL=ALL",$true)
    }


    Close. The two "install" statments in the if-block should be separated by a semi-colon (not a comma), or each placed on its own line. The second would generally be preferred as more readable:

       IF ($OSCaption -eq 'Microsoft Windows XP Professional')
       {
          $Win32Product.Install($WIN32_Path_IE,"ADDLOCAL=ALL",$true)
          $Win32Product.Install($WIN32_Path_FF,"ADDLOCAL=ALL",$true)
       }
    
    

     

    • Marked as answer by KipKasper Thursday, January 26, 2012 10:28 PM
    Thursday, January 26, 2012 10:03 PM

All replies

  • HI All,

    I'm working on my first really functional PSH script. 

    I have an IF statement that if TRUE I would need it to run 2 things, I'm not that you can do, that since I can't find any examples, but it would look like this.. maybe...  I'm i going about this all wrong?

    $Win32OS = [WMICLASS]"\\.\ROOT\CIMV2:Win32_OperatingSystem"
    $Architecture =  $Win32OS.OSArchitecture
    $OSCaption = $Win32OS.Caption
    $Win32Product = [WMICLASS]"\\.\ROOT\CIMV2:win32_Product"

    IF ($OSCaption -eq 'Microsoft Windows XP Professional')
    {
     $Win32Product.Install($WIN32_Path_IE,"ADDLOCAL=ALL",$true),$Win32Product.Install($WIN32_Path_FF,"ADDLOCAL=ALL",$true)
    }


    Close. The two "install" statments in the if-block should be separated by a semi-colon (not a comma), or each placed on its own line. The second would generally be preferred as more readable:

       IF ($OSCaption -eq 'Microsoft Windows XP Professional')
       {
          $Win32Product.Install($WIN32_Path_IE,"ADDLOCAL=ALL",$true)
          $Win32Product.Install($WIN32_Path_FF,"ADDLOCAL=ALL",$true)
       }
    
    

     

    • Marked as answer by KipKasper Thursday, January 26, 2012 10:28 PM
    Thursday, January 26, 2012 10:03 PM
  • Here's some code I use for a similar purpose, this is how I detect and use WMI calls for OS name and OS Architecture, hopefully you find it helpful.

     

    #detect OS and install
    #yields "X86-based PC" for 32 bit and "x64-based PC" for 64 bit
    OSType = Get-WmiObject Win32_ComputerSystem
    $OSArchitecture = $OSType.SystemType
    
    switch ($OSArchitecture)
    {
    	"X86-based PC"
    	{
    		#do stuff for 32 bit
    
    	}
    	"x64-based PC"
    	{
    		#do stuff for 64 bit
    	}
    	Default
    	{
    		[console]::ForegroundColor = "red"
    		$a = Read-Host "Could not determine OS. Press enter to exit script."
    		$a
    		[console]::ResetColor()
    		exit
    	}
    } #end OSType switch
    
    #get OS name
    $os = Get-WMIObject Win32_OperatingSystem
    $temp = $os.Caption
    if ($temp.Contains("2008") -eq $true)
    {
    	#do stuff if it's 2008
    }
    if ($temp.Contains("2003") -eq $true)
    {
    	#do stuff if it's 2003	
    }

    Thursday, January 26, 2012 10:06 PM
  • Useful suggestion, Robert. The only change I'd make to your code would be to change this:

        if ($temp.Contains("2008") -eq $true)

    to this:

        if ($temp.Contains("2008"))

    The Contains method returns a boolean true or false value, so comparing it to true is redundant.

     

    Thursday, January 26, 2012 10:23 PM
  • Thanks Al

    i don't know why i coudn't find that any where...

     

     - Kip


    -- Kip
    Thursday, January 26, 2012 10:29 PM
  • Thanks Robert,

     

    I havn't looked in Win32_ComputerSystem yet.. the seams more neat and tidy than using the Caption from Win32_OperatingSystem

     

     - Kip


    -- Kip
    Thursday, January 26, 2012 10:32 PM
  • Thanks Al

    i don't know why i coudn't find that any where...

     

     - Kip


    -- Kip

    That usually happens to me when I am looking directly at what I am looking for, whether it is something I want from the fridge or a coding error! ;-)
    Thursday, January 26, 2012 11:01 PM
  • Yes, even
     get-help about_if | more
    will not show examples of more than one statement within an if statement clause.
     
    I just did a large number of web searches and could not find an example where this was shown as an
    example of the if statement.
     
    The closest I got was this sample page from Bruce Payette's book Windows PowerShell in Action:
     
    Anyway, once you know it, it seems obvious.
     
     
    Friday, January 27, 2012 1:02 AM