Currently Being Moderated

*****************************************

All Intel-provided code snippets in or attached to this blog are provided under the Intel Software Community License unless otherwise specified.

 

Any user submitted code or materials posted on this blog is supplied under license from the submitter, and should be used or downloaded in accordance with any license terms specified. Intel is not responsible for user submitted code nor warrants that it will work correctly.  If no license is provided, you should contact the submitter.

*****************************************


I was showing off setting an Intel vPro Alarmclock to a colleague. He thought it was easy…because it is!

 

PS C:\Users\cdpiper> Set-AMTAlarmClock 192.168.1.101 -Credential $itprocredential -AlarmTime 2012-08-21T11:22:33

 

This tells the platform to power itself on at 11:22:33 on August 21, 2012.

“BUT…” he starts to say. Great – there is always more!

“But what?” I reply.

 

“But what happens if I want to power on the machine at a slightly different time?”

 

Easy! So I quickly wrote a small cmdlet to input the Set-AMTAlarmClock string and a random minute range.

 

Feel free to use this:

[CmdletBinding()]

Param (

                [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, position=1, HelpMessage="Alarm Time in YYYY-MM-DDTHH:MM:SS format")][ValidatePattern("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")] [string] $AlarmTime,

                [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, position=2, HelpMessage="Random minute range to add")][Int] $RandomMinuteRange

)

PROCESS {

                $RandObj = New-Object System.Random

                $TimeObj = [System.DateTime]::Parse($AlarmTime)

                $TimeObj = $TimeObj.AddMinutes($RandObj.next(-$RandomMinuteRange,($RandomMinuteRange+1)))

                write-Output $TimeObj.ToString("yyyy-MM-ddThh:mm:ss")

}

Here is an example of using this with a random 180 minute (three hour) range before or after the specified alarm time:

setting single system.PNG

 

“Awesome” I think to myself as I show him.

 

“Looks good for running on a single client. BUT…” he starts to say. Great – what else!?!

 

“…but what about if I want to set a randomized time to lots and lots of systems at once?”

 

Again, this is where the power of Microsoft’s PowerShell really shines. We discuss the different options. You could write another script that randomizes the $date and calls set-AMTAlarmClock for each $item in a list. You could query each system to get the time and then add or subtract x minutes. You could modify the Set-AMTAlarmClock cmdlet to take a random minute range as an input parameter.

 

He opted for the last option since the AMT cmdlets already can take in a list of clients. I agreed to blog about it – so here we go!

I modified the original Set-AMTAlarmClock cmdlet located in the C:\Program Files\Intel Corporation\PowerShell\Modules\IntelvPro directory to include the logic from above. Of course to run this cmdlet after modification your execution policy must not be set to All-Signed.  

using modified Set-AMTAlarmClockRandom.PNG

 

Here is the modified cmdlet:

[CmdletBinding()]

Param (

                [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true, position=0, HelpMessage="Hostname, FQDN, or IP Address")] [String[]] $ComputerName,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Valid Ports are 16992 (non-TLS) or 16993 (TLS)")][ValidateSet("16992", "16993")] [String] $Port,

                [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, position=1, HelpMessage="Alarm Time in YYYY-MM-DDTHH:MM:SS format")][ValidatePattern("[0-9][0-9][0-9][0-9]-[0

-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]")] [string] $AlarmTime,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Periodic Interval in DD-HH:MM:SS format")][ValidatePattern("[0-9][0-9]-[0-9][0-9]:[0-9][0-9]:[0

-9][0-9]")] [string] $Interval, 

[Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Use TLS (Port 16993)")] [switch] $TLS,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Digest of Kerberos User")] [string] $Username,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Digest of Kerberos Password")] [string] $Password,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, position=2, HelpMessage="PS Credential")] [System.Management.Automation.PSCredential] $Credential,

                [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Random minute range to add")][Int] $RandomMinuteRange

)

PROCESS {

$Results = @()

$RandObj = New-Object System.Random

#create a connection object

$Connection = New-Object Intel.Management.Wsman.WsmanConnection

if ($credential.username.Length -gt 0)

{

$Connection.SetCredentials($credential.Username, $credential.Password) 

}

elseif ($username.length -gt 0)

{

if ($password.length -gt 0)

{

$Connection.SetCredentials($username, $password) 

}

else

{

$cred = Get-Credential $username

$Connection.SetCredentials($cred.Username, $cred.Password)

}

}

if ($Port.Length -lt 1) {

if ($TLS.IsPresent) {

$Port = 16993;

}

else {

$Port = 16992;

}

}

Foreach ($comp in $ComputerName)

{

$Connection.SetHost($comp, $port)

if($Port -ne "16992")

{

        $GMTError = $false

}

else

{

        $http = [System.Net.WebRequest]::Create("http://"+$Comp+":"+$port+"/logon.htm")

        $httpResponse = $http.GetResponse()

if($httpResponse.StatusCode -eq [System.Net.HttpStatusCode]::OK)

        {

            $amtTime = [System.DateTime]::Parse($httpResponse.GetResponseHeader("Date"))

            $systemTime = [System.DateTime]::Now

            if($amtTime.Hour -ne $systemTime.Hour)

            {

                # AMT Time was not set to UTC

                $GMTError = $true

            }

        }

        $httpResponse.Close()

}

$Obj = new-object psobject

$Obj | Add-Member -MemberType noteproperty -Name ComputerName -value $Comp

$Obj | Add-Member -MemberType noteproperty -Name Port -value $port

$AlarmClockService_EPR = $Connection.NewReference("http://intel.com/wbem/wscim/1/amt-schema/1/AMT_AlarmClockService")

$AlarmClockService_EPR.AddSelector("SystemName","ManagedSystem")

try

{

        $AlarmClockService = $AlarmClockService_EPR.Get()    

}

catch

{

        $Obj | Add-Member -MemberType noteproperty -Name "Status" -value "Failed"

            $Obj | Add-Member -MemberType noteproperty -Name "NextAlarmTime" -value "[Error]"

            $Obj | Add-Member -MemberType noteproperty -Name "PeriodicInterval" -value "[Error]"

        $Results += $Obj

        continue    

}

if($AlarmTime -ne "")

{

        if ($AlarmTime.EndsWith(":00") -eq $false)

        {

            $replace = $AlarmTime.SubString($AlarmTime.Length-3, 3)

            $NewAlarmTime = $AlarmTime.TrimEnd($replace);

            $NewAlarmTime+= ":00"

            $AlarmTime = $NewAlarmTime

        }

        $SetAlarmTime = $Connection.NewInstance("Datetime")

        try

        {

            $TimeObj = [System.DateTime]::Parse($AlarmTime)

                    $TimeObj = $TimeObj.AddMinutes($RandObj.next(-$RandomMinuteRange,($RandomMinuteRange+1)))

               

        }

        catch

        {

            $Obj | Add-Member -MemberType noteproperty -Name "NextAlarmTime" -value "[Parse Error]"

            $Results += $Obj

            continue

        }

       

        if($GMTError)

        {

            # Local Time

            $SetAlarmTime.Text = $TimeObj.ToString("s")+"Z"

        }

        else

        {

            # Universal Time

            $SetAlarmTime.Text = $TimeObj.ToUniversalTime().ToString("s")+"Z"

        }

        $Obj | Add-Member -MemberType noteproperty -Name "NextAlarmTime" -value $TimeObj.ToString("F")

$AlarmClockService.SetProperty("NextAMTAlarmTime",$SetAlarmTime)

       

}

if($Interval -ne "")

{

        if($Interval.Contains("-"))

        {

            $temp = $Interval.Split("-")

            $Day = $temp[0]

            $TimeInterval = $temp[1]

        }

        else

        {

            $Day = "0"

            $TimeInterval = $Interval

        }

        $temp = $TimeInterval.Split(":")

        $hours = $temp[0]

        $minutes = $temp[1]

        $seconds = $temp[2]

       

        $intervalstring = "P0Y0M" + $Day + "DT" + $hours + "H" + $minutes + "M"

        $SetIntervalTime = $Connection.NewInstance("Interval")

        try

        {

            $TimeObj = [System.DateTime]::Parse($AlarmTime)

        }

        catch

        {

            $Obj | Add-Member -MemberType noteproperty -Name "PeriodicInterval" -value "[Error]"

            $Results += $Obj

            continue

        }

        $SetIntervalTime.Text = $intervalstring

        $Obj | Add-Member -MemberType noteproperty -Name "PeriodicInterval" -value $intervalstring

$AlarmClockService.SetProperty("AMTAlarmClockInterval",$SetIntervalTime)

       

       

}

try

{

        $RetVal = $AlarmClockService_EPR.Put($AlarmClockService)

        $Obj | Add-Member -MemberType noteproperty -Name "Status" -value "Successful"

}

catch

{

        $Obj | Add-Member -MemberType noteproperty -Name "Status" -value "Failed"

}

$Results += $Obj

}

Write-Output $Results

}

 

 

The differences?

Added Line 11:        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$false, HelpMessage="Random minute range to add")][Int] $RandomMinuteRange

Added Line 104:      $TimeObj = $TimeObj.AddMinutes($RandObj.next(-$RandomMinuteRange,i($RandomMinuteRange+1)))

 

 

That’s all!

 

Hope he doesn’t ask for more!

Comments

Filter Blog

By author:
By date:
By tag: