Script software deployment in PowerShell

This is an example in PowerShell of how you can schedule the installation a software package by Altiris DS.
Instead of using the API, the command-line tool axsched is being called to schedule the software package.

function DeploySoftware
{
	param( [String] $pcname,  [String] $package,  [String] $nexttime,  [String] $folder)
	#Write-Host "Deploy $pcname '$package' /t $nexttime /f '$folder'"
	#\\ALTIRIS\express\axsched.exe "$pcname" "$package" / t "$nexttime" / f "$folder"

	$StartInfo = New-Object System.Diagnostics.ProcessStartInfo
	$StartInfo.CreateNoWindow = $true
	$StartInfo.UseShellExecute = $false
	$StartInfo.FileName = "\\ALTIRIS\express\axsched.exe"
	$StartInfo.WorkingDirectory = "C:\Temp"
	$StartInfo.Arguments = "`"$pcname`" `"$package`" /t `"$nexttime`" /f `"$folder`""
	$p =  [System.Diagnostics.Process] ::Start($StartInfo)
	$p.WaitForExit()
	if ($p.ExitCode -eq 0) { $TRUE } else { $FALSE }
}
#This function adds a number of minutes to the given date time and outputs the addition in the right format for AxSched.
function Get-ScheduleTime
{
	param( [System.DateTime]  $starttime,  [int] $minutes)
	#$minutes = 12
	$tspan = New-Object System.TimeSpan(0, 0, $minutes, 0)
	$addtime =  [System.DateTime] ::op_Addition($starttime, $tspan)
	#"2008-10-20 15:00"
	$y = $addtime.Year
	$mo = $addtime.Month
	$d = $addtime.Day
	$h = $addtime.Hour
	$mi = $addtime.Minute
	$schedtime = "$y"
	$schedtime += "-"
	$schedtime += TwoDecimal("$mo")
	$schedtime += "-"
	$schedtime += TwoDecimal("$d")
	$schedtime += " "
	$schedtime += TwoDecimal("$h")
	$schedtime += ":"
	$schedtime += TwoDecimal("$mi")
	$schedtime
}
#Simple function to convert a single decimal to a two decimal, eg 7 becomes 07
function TwoDecimal
{
	param( [String] $Number)
	while ($number.Length -lt 2) { $number = "0$number" }
	$number
}
$now =  [System.DateTime] ::Now
$pcname = "COMP007"
$jobname = "Install Outlook 2007"
$jobfolder = "Software"
#The installation of the package needs to be started after 5 minutes.
$nexttime = Get-ScheduleTime $now 5
DeploySoftware $pcname $jobname $nexttime $jobfolder
 

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.