PowerShell: Start all VMWare services, self elevate to admin

For reasons, all my VMWare services are set to manual startup.  I like it this way.

I wanted an easy way to start all the services so naturally, a PowerShell script was required.  However the services can’t be started by PowerShell without elevated privileges, and I usually work in a non-elevated ISE.  So this version of the script self-elevates, saving me precious seconds.

# Start All VMware services V2
# WARNING - THIS SCRIPT WILL ATTEMPT TO ELEVATE ITSELF

# Check if the current user role is in the local computer administrator role

            # get-service -displayname 'VMware*' | %{Stop-Service -name $_.Name}

If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
    [Security.Principal.WindowsBuiltInRole] "Administrator")){
               
               # Not running as administrator

                # Create a new process object that starts PowerShell
                   $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
   
                   # Specify the current script path and name as a parameter
                   $script:MyInvocation.MyCommand.Path
                   $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
   
                   # Indicate that the process should be elevated
                   $newProcess.Verb = "runas";
   
                   # Start the new process
                   [System.Diagnostics.Process]::Start($newProcess);
   
            }
               
        Else
        {     
 
        # Elevated Code - will only run if IS Administrator 
            
            # Set some colors so its clear what is going on
            
                $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
                $Host.UI.RawUI.BackgroundColor = "DarkRed"
                clear-host

            # start all the VMware services
            
                $s = get-service -displayname 'VMware*' # i should really list the services in the right startup order
                write-host $s

                    foreach ($svc in $s){

                        write-host $svc.name 'is' $svc.Status
                      
                        if($svc.Status -ne "Running")
                        {
                            write-host 'Starting: ' $svc.Name
                                #Stop-Service $svc.name
                                #set-service $svc.Name -StartupType Manual
                            Start-Service $svc.Name

                        }
                        Else{
                             # Do something else if you want
                        }
                    }


                #start "C:\Program Files (x86)\VMware\VMware Workstation\vmware.exe"      
                    }

 # Re-present the services again

     get-service -displayname 'VMware*'
     
        Write-Host "Sleeping 60" 
        start-sleep -seconds 60

 

 

 

Facebooktwitterredditpinterestlinkedinmail