PowerShell – Break vs. Continue Usage

For this example, we create two ArrayList objects and add a couple ints to them to run a loop to see if the value matches between the two and either continues a loop or breaks out of a loop:

$arraylist1 = New-Object -TypeName System.Collections.ArrayList
$arraylist1.Add(1) | Out-Null
$arraylist1.Add(2) | Out-Null
$arraylist1.Add(3) | Out-Null
$arraylist2 = New-Object -TypeName System.Collections.ArrayList
$arraylist2.Add(1) | Out-Null
$arraylist2.Add(2) | Out-Null
$arraylist2.Add(3) | Out-Null
Sample code for ‘continue’:
foreach ($item in $arraylist1)
{
    Write-Output -InputObject "item is: $item"
    foreach ($subitem in $arraylist2)
    {
        Write-Output -InputObject "subitem is: $subitem"
        if ($item -eq $subitem)
        {
            Write-Output -InputObject "$item matches $subitem! Continuing!"
            continue
        }
    }
}

…Which outputs:

item is: 1
subitem is: 1
1 matches 1! Continuing!
subitem is: 2
subitem is: 3
item is: 2
subitem is: 1
subitem is: 2
2 matches 2! Continuing!
subitem is: 3
item is: 3
subitem is: 1
subitem is: 2
subitem is: 3
3 matches 3! Continuing!
Sample code for ‘break’:
foreach ($item in $arraylist1)
{
    Write-Output -InputObject "item is: $item"
    foreach ($subitem in $arraylist2)
    {
        Write-Output -InputObject "subitem is: $subitem"
        if ($item -eq $subitem)
        {
            Write-Output -InputObject "$item matches $subitem! Breaking!"
            break
        }
    }
}

…which outputs:

item is: 1
subitem is: 1
1 matches 1! Breaking!
item is: 2
subitem is: 1
subitem is: 2
2 matches 2! Breaking!
item is: 3
subitem is: 1
subitem is: 2
subitem is: 3
3 matches 3! Breaking!

What can you use this for or when should I use what one? It depends on your use case. 🙂

Advertisements

MDT – Deployment Query Date (VBS)

Useful script to determine when you pushed out an image from MDT…

Save the following as a *.VBS file:

Option Explicit
Const strComputer = "."
Const strNamespace = "\root\cimv2"
Const strClassName = "Microsoft_BDD_Info"

Const wbemCimTypeDatetime = 101
'Const wbemCimTypeString = 8
'Const wbemCimtypeBoolean = 11

Dim objSWbemServices: Set objSWbemServices = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & strNamespace)
Dim colInstances: Set colInstances = objSWbemServices.ExecQuery("SELECT * FROM " & strClassName)
Dim iCount, objInstance, objProperty, colProperties
Dim dateTime : Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
On Error Resume Next
Dim o: o = colInstances.count
On Error Goto 0
If Not IsEmpty(o) Then
    iCount = 0
    For Each objInstance in colInstances
        iCount = iCount + 1
        Set colProperties = objInstance.Properties_
        For Each objProperty in colProperties
            If objProperty.cimType = wbemCimTypeDatetime Then
                datetime.Value = objProperty.Value
                If objProperty.Value <> "" Then WScript.Echo objProperty.Name & ": " & datetime.GetVarDate
            Else
                If objProperty.Value <> "" Then WScript.Echo objProperty.Name & ": " & objProperty.Value
            End If
        Next

    Next
Else
    WScript.Echo strClassName & " not found."
End if

Original author – Michael Klinteberg:
http://social.technet.microsoft.com/Forums/en-US/mdt/thread/e783c5ec-da83-4111-80f2-ea4539d603e2