Thursday, October 6, 2016

PowerShell: Download and Install Java 8 JRE

This script I wrote for our automation system that will:
  1. Download the latest Java 8 JRE from Java.com
  2. Save in a local directory C:\Install
  3. Create a Install Options File
  4. Run the installer silently with the necessary Installer Options
The Installer Options file determines what configuration is used the install of Java.

For example, in the script I have configured. This will perform a silent install, auto updates enabled, no reboot after the installation, Sponsors/ads install disabled and will uninstall any older existing installations of Java JRE.

  • INSTALL_SILENT=Enable
  • AUTO_UPDATE=Enable
  • REBOOT=Disable
  • SPONSORS=Disable
  • REMOVEOUTOFDATEJRES=Enable
You can find these and other options to use from the Java website:


This has been tested to work on PowerShell v2 and higher.


--------------------------------------------------------------------------
                Deploy Java JRE Software
                  Date: 21-Sept-2016
               Created by: Daniel Burrowes
--------------------------------------------------------------------------

Description:
Downloads the Java installer and installs silently

-------------------------------------------------------------------------- 

# -Verbose and -Debug

    [CmdletBinding()]
    param(    )

# --------------------------------------------------------------------------

Write-Verbose "Setting Global Variables..."
$InstallDir = "c:\Install\Java"
$Source = "http://javadl.oracle.com/webapps/download/AutoDL?BundleId=211996"
$Destination = "$InstallDir\java.exe"
$Options = "$InstallDir\java_options.txt"


#Create install directory
Write-Verbose "Creating Install Directory"
New-Item -Path $InstallDir -ItemType directory -Force

Write-Verbose "Downloading Software..."
$start_time = Get-Date
(New-Object System.Net.WebClient).DownloadFile($Source, $Destination)
Write-Verbose "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" 

Write-Verbose "Creating Installer Options File..."
$text = 'INSTALL_SILENT=Enable
AUTO_UPDATE=Enable
REBOOT=Disable
SPONSORS=Disable
REMOVEOUTOFDATEJRES=Enable
'

# Create file
$text | Set-Content $Options

#Running the installer
Write-Verbose "Executing Java Install.."
Start-Process -FilePath $Destination -ArgumentList "INSTALLCFG=$Options /s /L $InstallDir\jre-install.log" -Wait -Verbose -PassThru

Monday, April 25, 2016

PowerShell: BATCH Script to Allow PowerShell Scripts to Execute

For some SysAdmins, they will use one or more tools to deploy software to a range of computers on their network. For me, it our monitoring tool that allows are to remote execute scripts to do some sort of task.

Writing in PowerShell is amazing and is Microsoft's best yet command line tool. However by default for security purposes, script execuition is blocked for untrusted or unsigned scripts.

I won't go into Signing scripts just now but using the following command will allow you to run PowerShell Scripts

Set-ExecutionPolicy -RemoteSigned

This line still provides a level of protection while still allwoing you to execute custom scripts. Other switches include -bypass and -unrestricted.

So how can we get a large number of these set?

There are a number of ways:
  1. Group Policy
  2. Batch Script
The reason for me writing this BATCH Script is that it allows me to deploy and run on a computer regardless of the Domain it is joined to.

Copy and paste this code into a text file called SetExecutionPolicy.bat and then Save.

This script also creates entries into the Windows Event Viewer to tell you if and when the command has been executed, or if it failed to run.



@echo off

REM -------------------------------------------------------------
REM       Set-ExecutionPolicy RemoteSigned for Powershell
REM          Date: 10-Spet-2015
REM        Author: Daniel Burrowes
REM -------------------------------------------------------------



Echo Setting PowerShell Execution Policy for RemoteSigned

PowerShell.exe Set-ExecutionPolicy RemoteSigned

IF %ERRORLEVEL% NEQ 0 goto ERROR    

EVENTCREATE /T INFORMATION /L APPLICATION /ID 100 /D "PowerShell Execution Policy has been set to RemoteSigned"
GOTO DONE

:ERROR 

EVENTCREATE /T ERROR /L APPLICATION /ID 100 /D "PowerShell ExecutionPolicy batch script failed to run." 


:DONE

Echo Done!

EXIT

PowerShell: Download and Install Citrix Receiver

I have been doing quite a bit of work lately on PowerShell scripting for work and this is a common one we deploy to our clients that use Citrix.

This script does the following:
  1. Creates the folder C:\Install\CitrixReceiver
  2. Downloads the latest Citrix Receiver software from the Citrix website
  3. Installs the downloaded Receiver silently
The link for the Citrix Receiver is: http://downloadplugins.citrix.com/Windows/CitrixReceiver.exe

Works with PowerShell v2 and higher.
A lot of my scripts are built to work at this level as it means that I can deploy my scripts to older machines without having to first upgrade PowerShell.

Copy and paste the code into a text file called InstallCitrixReceiver.ps1.



# --------------------------------------------------------------------------
#                Citrix Receiver Install Script
#                   Date: 14-Mar-2016
#               Created by: Daniel Burrowes
# --------------------------------------------------------------------------

# -Verbose and -Debug

[CmdletBinding()]
param()

# --------------------------------------------------------------------------
#  Change Log
#
#  14-Mar-2016
# -Added /IncludeSSON to argument list
# --------------------------------------------------------------------------

#Create install directory
Write-Verbose "Creating Install Directory"
$InstallDir = "C:\Install\CitrixReceiver"
New-Item -Path $InstallDir -ItemType directory -Force


Function Download-Citrx {

 $Source = "http://downloadplugins.citrix.com/Windows/CitrixReceiver.exe"
 $destination = "C:\Install\CitrixReceiver\CitrixReceiver.exe"

    Write-Verbose "Downloading Software..."
    (New-Object System.Net.WebClient).DownloadFile($Source, $Destination)
    Write-Verbose "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)" 

}

Write-Verbose "Downloading Latest Citrix Receiver"
Download-Citrx

Write-Verbose "Running silent install of Citrix Receiver"
Start-Process -FilePath "C:\Install\CitrixReceiver\CitrixReceiver.exe" -ArgumentList "/silent /IncludeSSON" -Wait -Verbose -PassThru

Executing Powershell scripts may require you to allow scripts to run.:
#Set-Execution Policy -RemoteSigned