Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Converting a script to powershell

$
0
0

I have little experience in Powershell and VBScript even less if not nothing. I was asked to convert the script. How do I start?

I am wanting to learn how to do it. But does that mean I have to learn VB before I could ever attempt to convert this?

Option Explicit

' elevate script to run as administrator
Call Elevate()

' instantiate variables/constants
Dim objFSO, updateProp, objSleep, loopCounter, newestVersionProp, currentVersionProp
Dim objVersionTXT, objShell, propNetworkPath
Dim strComputer, strValue, oReg, strProcess
Dim installStatusValue, propTypeValue, propTypeKey
Dim installStatusKey, propVersionKey, strRunning, strDone
Dim objPropFolder, colFilesInFolder, objFile, colFoldersInFolder, objFolder
const HKEY_CURRENT_USER = &H80000001
const HKEY_LOCAL_MACHINE = &H80000002
Dim proprietaryBetaMember, proExcelBetaMember, proprietaryDisabled, strTempRead
const ForReading = 1

loopCounter = 0

Set objShell = createObject("wscript.shell")

installStatusKey = "PropInstallStatus"
propVersionKey = "ProprietaryVer"
propTypeKey = "ProprietaryType"
strRunning = "RUNNING"
strDone = "DONE"
strProcess = "PropSetup.exe"

Set objFSO = CreateObject("Scripting.FileSystemObject")
'------------------Program Body---------------------

' AD group. Machines in the group will not get proprietary
proprietaryDisabled = IsMemberOfGroup("cn=App-Proprietary-Disabled-Machine,ou=Proprietary,ou=Groups - Software,ou=Groups,ou=Master,dc=hbk,dc=com")

If proprietaryDisabled = False Then
    ' machine not excluded form proprietary through the AD group, so check to see if it's disabled through the registry
    
    ' create key if necessary
    If KeyExists("HKLM\SOFTWARE\HBK\") = False Then
        Call createRegistryKey(HKEY_LOCAL_MACHINE,"SOFTWARE\HBK")
    End If

    propTypeValue = readRegistryValue("SOFTWARE\HBK", propTypeKey)
    
    If propTypeValue <> "Disabled" Then ' make sure install of proprietary isn't disabled on the computer
    
        ' check if computer is a member of either Beta groups
        proprietaryBetaMember = IsMemberOfGroup("cn=App-Proprietary-Beta,ou=Proprietary,ou=Groups - Software,ou=Groups,ou=Master,dc=hbk,dc=com")
        proExcelBetaMember = IsMemberOfGroup("cn=App-ProExcel-Beta,ou=Proprietary,ou=Groups - Software,ou=Groups,ou=Master,dc=hbk,dc=com")
    
        ' change source path if computer is a member of the Proprietary Beta group
        If proprietaryBetaMember = True Then
            Call updateRegistryValue("SOFTWARE\HBK", propTypeKey, "Beta")
            propNetworkPath = "\\hbk.com\apps\all\Proprietary\Beta"
        Else
            Call updateRegistryValue("SOFTWARE\HBK", propTypeKey, "Prod")
            propNetworkPath = "\\hbk.com\apps\all\Proprietary\Production"
        End If
    
        ' check to make sure source files exist
        If (objFSO.FileExists(propNetworkPath & "\version.txt")) Then
            Set objVersionTXT = objFSO.OpenTextFile(propNetworkPath & "\version.txt", ForReading)
            newestVersionProp = objVersionTXT.ReadLine
            objVersionTXT.Close
            set objVersionTXT = Nothing
            currentVersionProp = readRegistryValue("SOFTWARE\HBK", propVersionKey)
    
            ' check if local and network versions are the same
            If newestVersionProp <> currentVersionProp Then
                 ' check to make sure TIBCO is installed on the local machine
                 If (objFSO.FileExists("C:\TIBCO\TIBRV\bin\rvd.exe")) Then
                     ' versions aren't the same, so check to see if setup is running. If it is, then check every two seconds for up to a minute.
                     If checkProcessAndWait(strProcess) Then
                        ' at the end of a minute, another PropSetup.exe process is still running, so installation will exit
                        Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR1")
                     Else
                         ' PropSetup.exe isn't running, so read in version key and check one more time
                         ' because PropSetup.exe might have finished and updated Prop to the newest version
                         currentVersionProp = readRegistryValue("SOFTWARE\HBK", propVersionKey)
                         If newestVersionProp <> currentVersionProp Then
                            ' PropSetup.exe process isn't running and install is outdated, so begin install.
                            installProp
                            ' after proprietary is installed, check to see if computer is a part of the group to install the beta version of ProExcel
                            If proExcelBetaMember = True Then
                                strTempRead = readRegistryValue("SOFTWARE\HBK", propTypeKey)
                                'append on the the Proprietary Install Type key to reflect that the beta version of pro excel was added
                                Call updateRegistryValue("SOFTWARE\HBK", propTypeKey, strTempRead & "AndProExcelBeta")
                                If objFSO.FileExists("\\hbk.com\apps\all\Proprietary\Beta\ProExcel\ProExcel.xla") AND objFSO.FolderExists("C:\Proprietary\ProExcel")Then
                                   objFSO.CopyFolder "\\hbk.com\apps\all\Proprietary\Beta\ProExcel", "C:\Proprietary\ProExcel", True
                                End If
                            End If
                         End If
                     End If
                 Else
                     'tibco not found
                     Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR7")
                 End If
             End If
        Else
            ' version.txt not found
            Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR0")
        End If
    End If
End If

' Clean up

Set objFSO = Nothing
Set objShell = Nothing


' -------------------- Functions -----------------------

' This function installs Proprietary using the force installation executable.
Function installProp
    Dim objShell
    Set objShell = createObject("wscript.shell")

    ' check one more time to make sure PropSetup.exe exists on the network
    If objFSO.FileExists(propNetworkPath & "\PropSetup.exe") Then
        ' Check if Installers folder exists. If not, create it.
        If Not objFSO.FolderExists("C:\Installers") Then
            objFSO.CreateFolder("C:\Installers")
        End If

        ' delete old setup executable
        If objFSO.FileExists("C:\Installers\PropSetup.exe") Then
           objFSO.DeleteFile "C:\Installers\PropSetup.exe", True
        End If

        ' copy over new setup executable
        objFSO.CopyFile propNetworkPath & "\PropSetup.exe", "C:\Installers\", True

        ' Wipe Proprietary folder
        If objFSO.FolderExists("C:\proprietary") Then
           Set objPropFolder = objFSO.GetFolder("C:\proprietary")
           Set colFilesInFolder = objPropFolder.Files

           ' Delete all files in the folder
           For Each objFile In colFilesInFolder
           On Error Resume Next
                objFSO.DeleteFile(objFile), True
           Next
      
           ' Try to delete all subfolders and their containing files
           Set colFoldersInFolder = objPropFolder.SubFolders

           For Each objFolder In colFoldersInFolder
               On Error Resume Next
               DeleteFilesInFolder(objFolder.Path)
               objFSO.DeleteFolder(objFolder), True
           Next
        End If

        ' ensure local copy of setup executable exists after copy command
        If objFSO.FileExists("C:\Installers\PropSetup.exe") Then
            Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, strRunning)
            ' install Proprietary by calling executable
            objShell.Run "C:\Installers\PropSetup.exe /SILENT /NOCANCEL /NORESTART /DIR=C:\Proprietary",,True
            If objFSO.FolderExists("C:\Proprietary") Then
                Dim propFolder
                Set propFolder = objFSO.GetFolder("C:\Proprietary")
                If (propFolder.Size > 150000000) Then
                    If objFSO.FileExists("C:\Proprietary\ProCore.dll") Then
                        ' No errors during install! Update registry keys to reflect successful install
                        ' Go home early. You done good.
                        Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, strDone)
                        Call updateRegistryValue("SOFTWARE\HBK", propVersionKey, newestVersionProp)
                    Else
                        ' ProCore.dll doesn't exist. Install corrupted.
                        Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR6")
                    End If
                Else
                    ' proprietary folder too small. Install corrupted.
                    Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR5")
                End If
            Else
                ' C:\proprietary doesn't exist
                Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR4")
            End If
        Else
           ' PropSetup.exe doesn't exist at source location
           Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR3")
        End If
    Else
       ' computer can't find network location of PropSetup.exe
       Call updateRegistryValue("SOFTWARE\HBK", installStatusKey, "ERROR2")
    End If
End Function

' function that will create a registry key
Sub createRegistryKey(constHive, strKeyPath)
    strComputer = "."
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

    oReg.CreateKey constHive,strKeyPath
End Sub



' function that will check to see if a key exists
Function KeyExists(key)
    On Error Resume Next
    Set objShell = CreateObject("WScript.Shell")
        objShell.RegRead (key)
    Set objShell = Nothing
    If Err = 0 Then
         KeyExists = True
    Else
        KeyExists = False
    End If
End Function

' This function reads the registry value containing the currently installed version of Proprietary
Function readRegistryValue(strKeyPath, valueName)
    strComputer = "."
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
  
    oReg.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,valueName,strValue
    If IsNull(strValue) Then
      readRegistryValue = "NoExist"
    Else
      readRegistryValue = strValue
    End If
End Function

' Update Registry Value
Sub updateRegistryValue(strKeyPath, valueName, valueString)
    strComputer = "."
    Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

    oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,valueName,valueString
End Sub

' Checks if a computer is a member of a group.
' Returns True if it is, False if not
Function IsMemberOfGroup(strGroup)
    IsMemberOfGroup = False
    
    Dim objSysInfo, strComputerDN, objComputer
    Dim objGroup

    ' Bind to local computer object.
    Set objSysInfo = CreateObject("ADSystemInfo")
    strComputerDN = objSysInfo.ComputerName
    Set objComputer = GetObject("LDAP://" & strComputerDN)
    
    ' Bind to group whose membership will be checked.
    Set objGroup = GetObject("LDAP://" & strGroup)

    ' Check if the computer is a member of this group.
    If (objGroup.IsMember(objComputer.AdsPath) = True) Then
         IsMemberOfGroup = True
    End If
End Function

' Function that checks to see if process is running and will wait for up to a minute to see if it finishes
' checks every two seconds for thirty iterations
' returns false if process isn't running
' returns true if process is running
Function checkProcessAndWait(strProcessName)
    Dim i

    ' Check if setup is running
    For i = 0 to 30
        If isProcessRunning(strProcessName) Then
           checkProcessAndWait = true
           wscript.Sleep 2000 ' sleep two seconds between checks
        Else
           ' setup isn't running, so continue install
           checkProcessAndWait = false
           If i <> 0 Then
              ' PropSetup process was running when the loop began, so a wait is necessary
              wscript.Sleep 10000 ' wait ten seconds to make sure registry has a chance to update
           End If
           Exit For
        End If
    Next

End Function

' Function to check if a process is running
Function isProcessRunning(strProcessName)
        strComputer = "." ' local computer
Dim objWMIService, strWMIQuery

strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

If objWMIService.ExecQuery(strWMIQuery).Count > 0 then
isProcessRunning = true
Else
isProcessRunning = false
End if

End Function


Sub Elevate()
    If WScript.Arguments.Named.Exists("elevated") = False Then
       'Launch the script again as administrator
       CreateObject("Shell.Application").ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 1
       WScript.Quit
    Else
         'Change the working directory from the system32 folder back to the script's folder.
         Dim oShell
         Set oShell = CreateObject("WScript.Shell")
         oShell.CurrentDirectory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
    End If
End Sub


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>