vendredi 3 avril 2015

New PC Setup powershell script

Question is: Can you guys help me clean this up and add error checking? There are allot of different things going on in this script so here is the order in detail: Steps:




  1. Run as admin newsetup.bat



    @ECHO OFF
    start C:\ClientApps\NewSetup\Scripts\IEBlocker\IEBLOCKALL.bat
    start C:\ClientApps\NewSetup\Scripts\RegFiles\Disable_NonAdmins_Installing_Windows_Updates.reg
    start C:\ClientApps\NewSetup\Scripts\RegFiles\Disable_UAC.reg
    cd %SystemRoot%\system32\WindowsPowerShell\v1.0
    PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start- Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\ClientApps\NewSetup\Scripts\New Setup.ps1""' -Verb RunAs}"
    End



  2. at the end of the .bat it calls a newsetup.ps1 at the end of the ps1 it will reboot the pc.



    If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))

    {
    $arguments = "& '" + $myinvocation.mycommand.definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    Break
    }

    function New-Sleep {
    [cmdletbinding()]
    param(
    [parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, HelpMessage="No time specified")]
    [int]$s
    )
    for ($i=1; $i -lt $s; $i++) {
    [int]$TimeLeft=$s-$i
    Write-Progress -Activity "Installing Software $s seconds remaining..." -PercentComplete (100/$s*$i) -CurrentOperation "$TimeLeft seconds left ($i elapsed)" -Status "Please wait"
    Start-Sleep -s 1
    }
    Write-Progress -Completed $true -Status "Please wait"
    } # end function New-Sleep

    $pagefile = "C:\ClientApps\NewSetup\Scripts\Pagefile"
    $Wupdates = "C:\ClientApps\NewSetup\Scripts\PSWindowsUpdate"
    $root = "C:\"
    $adobeReader = "C:\ClientApps\NewSetup\Software\AdbeRdr11010_en_US.exe"
    $adobeFlash = "c:\ClientApps\NewSetup\Software\install_flash_player_17_active_x.msi"
    $adobeAIR = "C:\ClientApps\NewSetup\Software\AdobeAIRInstaller.exe"
    $java = "c:\ClientApps\NewSetup\Software\jre1.8.0_40.msi"
    $Bloatware = "C:\ClientApps\NewSetup\Software\pc-decrapifier-3.0.0"
    $log = "C:\clientapps\NewSetup\Serial&MAC.log"

    #Copy/remove & Import Modules
    Copy-Item $Wupdates $root -Recurse -Force
    Copy-Item $pagefile $root -Recurse -Force

    Import-Module C:\Pagefile\pagefile.psm1
    Import-Module C:\PSWindowsUPdate\Get-WindowsUpdateConfig.ps1
    Import-Module C:\PSWindowsUPdate\Get-WUInstall.ps1

    Remove-Item C:\Pagefile -Recurse
    Remove-Item C:\PSWindowsUpdate -Recurse

    #Install Applications
    &"$adobeAIR"
    New-Sleep -s 5
    &"$adobeReader" /msi EULA_ACCEPT=YES /qn
    New-Sleep -s 40
    msiexec /i "$java" /qb
    New-Sleep -s 40
    msiexec /i "$adobeFlash" /qb

    #Run Bloatware remover tool
    & "$Bloatware"

    #Set Virtual Memory.

    Set-OSCVirtualMemory -InitialSize 6144 -MaximumSize 12288 -DriveLetter "C:"

    # Enable Remote Desktop
    (Get-WmiObject Win32_TerminalServiceSetting -Namespace root\cimv2\TerminalServices).SetAllowTsConnections(1,1) | Out-Null
    (Get-WmiObject -Class "Win32_TSGeneralSetting" -Namespace root\cimv2\TerminalServices -Filter "TerminalName='RDP-tcp'").SetUserAuthenticationRequired(0) | Out-Null

    #Set Power Plan to High Perfermance
    Try {
    $HighPerf = powercfg -l | %{if($_.contains("High performance")) {$_.split()[3]}}
    $CurrPlan = $(powercfg -getactivescheme).split()[3]
    if ($CurrPlan -ne $HighPerf) {powercfg -setactive $HighPerf}
    } Catch {
    Write-Warning -Message "Unable to set power plan to high performance"
    }

    #Log Serial & Mac address

    $ErrorActionPreference="SilentlyContinue"
    Stop-Transcript | out-null
    $ErrorActionPreference = "Continue"

    $OutputFileLocation = "$log"
    Start-Transcript -path $OutputFileLocation -append

    gwmi win32_bios | fl SerialNumber

    $mac = Get-WmiObject Win32_NetworkAdapter | Where-Object { $_.MacAddress } |
    Select-Object Name, MacAddress
    Get-WmiObject Win32_NetworkAdapter | Where-Object { $_.MacAddress } |
    Select-Object Name, MacAddress
    Write-Host "$mac"

    Stop-Transcript

    #Change Check for updates to disable

    Set-WindowsUpdateConfig 1

    #start windows updates
    Write-Host "Looking for Updates...."

    Get-WUInstall -AcceptAll -AutoReboot



  3. What I would like it to do is added in I guess a try/catch for the applications to check if the applications are already installed and then a windows version check and if its windows 8 or 8.1 run a second .ps1 called Win8_apps_removal.ps1 script first and don't run adobe flash install. And auto calculate the virtual memory. I found the code below which I feel like will be required to check the installed program lists for already installed software.



    Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate |
    Format-Table –AutoSize



Aucun commentaire:

Enregistrer un commentaire