Sign in to follow this  
Followers 0
SuckaMCs

Script To Deploy PowerShell 2.0

7 posts in this topic

Hello,

I am really new at scripting. I just need some advice on this script I am working on.   I am not sure if i am doing this the best way. I have a ton of if statements already and i am getting  a bit confused. It looks like it will be hard to troubleshoot if i had any problems with my script: Here is what I have so far before i explain to what i am trying to do:

$osv = @OSVersion
$ost = @OSArch


; First If Statement is to Check OS Version
If $osv = "WIN_2008" Then
; Second If is to check  OS archetechture 
      If $ost = "x86" Then
             SetError(0)
             RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\","Install")
; Third If is to check .net Version          
             If @error <> 0 Then
                  MsgBox(0,"Info", ".NET v3.5 or higher is required!")
             Else
                 MsgBox(0,"Info", ".NET v3.5 Is Not required!")
             EndIf
           ElseIf $ost = "x64" Then
              MsgBox(0, "", "64 Bit")
      EndIf
    Break(1)
ElseIf $osv = "WIN_2003" or $osv = "WIN_2003R2" Then
      If $ost = "x86" Then
             SetError(0)
             RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\","Install")
             If @error <> 0 Then
                  MsgBox(0,"Info", ".NET v3.5 or higher is required!")
             Else
                 MsgBox(0,"Info", ".NET v3.5 Is Not required!")
             EndIf

           ElseIf $ost = "x64" Then
              MsgBox(0, "", "64 Bit")



           ElseIf $ost = "x64" Then
              MsgBox(0, "", "64 Bit")
      EndIf
    Break(1)
Else
    MsgBox(0, "", "Not a Supported OS Version")
 EndIf
#include <MsgBoxConstants.au3>
Local $var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\", "PowerShellVersion")

If $var <> "1.0" Then
   MsgBox(0, "", "Install PowerShel 2.0")
Else
  MsgBox(0, "", "Uninstall Install PowerShell 1.0")


EndIf 

So what i have so far is this. I want to first check to see what OS version it is. Because I want to deploy PowerShell 2.0 i only want this script to install on either Windows Server 2003, Windows Server 2003 R2 or Windows Server 2008. I also want to check weather or not the OS is x64 or x86, then i need it to if the OS has PowerShell 2.0 already, After it checks that, i need it to check if it has .net 3.5 SP1. After it checks for .net  or powershell i will need it to do something.  After it checks .net 3.5 i have a series of instal commands that i need to run.

I hope this makes sense if anyone needs any other information please let me know.

Share this post


Link to post
Share on other sites

As you're really searching on one item, the OS Version. I would do a switch. Determining whether it is x86 or x64 could be secondary. Something like this:

Switch @OSVersion
    Case "WIN_2008"
        If @OSArch = "x86" Then
            RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\","Install")
                If @error <> 0 Then
                    MsgBox(0,"Info", ".NET v3.5 or higher is required!")
                Else
                    MsgBox(0,"Info", ".NET v3.5 Is Not required!")
                EndIf
        ElseIf @OSArch = "x64" Then
             MsgBox(0, "", "64 Bit")
        EndIf
    Case "WIN_2003","WIN_2003R2"
        If @OSArch = "x86" Then
            RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\","Install")
                If @error <> 0 Then
                    MsgBox(0,"Info", ".NET v3.5 or higher is required!")
                Else
                    MsgBox(0,"Info", ".NET v3.5 Is Not required!")
                EndIf
        ElseIf @OSArch = "x64" Then
              MsgBox(0, "", "64 Bit")
         EndIf
    Case Else
        MsgBox(0, "", "Not a Supported OS Version")
EndSwitch

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Sorry for my late response but i wasn't able to access the site for a while for some reason. This is what i have now. Tell me what you think.

$osv = @OSVersion
$ost = @OSArch

Local $var = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine\", "PowerShellVersion")
Local $var2 = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5\","Version")


Switch #OSVersion
        Case "WIN_2008"

         If $ost = "x86" Then

               If $var <> "1.0" Then
                        
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\BITSx86.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\WMF2k8x86.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)


               Else

                    $CMD = "ServerManagerCmd.exe -remove PowerShell -restart"
                        RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\BITSx86.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\WMF2k8x86.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                        M
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)

               EndIf

         ElseIf $ost = "x64" Then


           MsgBox(0, "", "64 Bit Win 2008")

               If $var <> "1.0" Then
                        
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\BITSx64.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\WMF2k8x64.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                           
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)

               Else

                    $CMD = "ServerManagerCmd.exe -remove PowerShell -restart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                     
                        $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                     
                    $CMD = "C:\Install\BITSx64.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                     
                    $CMD = "C:\Install\WMF2k8x64.msu  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)

               EndIf
         EndIf

Break(1)

    Case "WIN_2003","WIN2003R2"

         If $ost = "x86" Then

            

               If $var <> "1.0" Then
                  
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                 
                    $CMD = "C:\Install\WMF2k3x86.exe  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)


               Else

                    
                    Sleep (360000)
                  
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                  M
                    $CMD = "C:\Install\WMF2k3x86.exe  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)


               EndIf

         ElseIf $ost = "x64" Then


            

               If $var <> "1.0" Then
                  
                     $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                        
                    $CMD = "C:\Install\WMF2k3x64.exe  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                       
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)


               Else

                    M
                    Sleep (360000)
                  
                    $CMD = "C:\Install\dotnetfx35setup.exe /q /norestart"
                       RunWait(@comspec & " /k " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "C:\Install\WMF2k3x86.exe  /quiet /norestart"
                       RunWait(@comspec & " /c " & $CMD)
                    Sleep (360000)
                         
                    $CMD = "Shutdown /R"
                       RunWait(@comspec & " /c " & $CMD)


               EndIf


         EndIf


Break(1)



   Case Else

   MsgBox(0, "", "Not a Supported OS Version")

EndSwitch
Edited by SuckaMCs

Share this post


Link to post
Share on other sites

First off, the use of @OSVersion in the switch negates the need for this line:

$osv = @OSVersion

Also, I don't see any benefit to assigning @OSArch the variable name $ost, but that is personal preference.

If $ost = "x86" Then

   ;vs

If @OSArch = "x86" Then

Lastly, as I don't see any point where you are disabling Break, not sure why you need this on lines 100 and 182. Unless you are doing it elsewhere in the script.

Break(1)

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

I am new to this. So i can change that. I am still learning. Will make the changes you suggested. Other than those do you see something else that I should be fixing? 

Share this post


Link to post
Share on other sites

Nope, I think it looks good. And we were all on Day1 once, so no worries on being new. The most important thing in the beginning is - does the script do what you want it to? Polish can come later :)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

Thanks! I appreciate all your input.  and yes the script actually works lol i was  surprised. But again thanks for all your help

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0