Contents

Script Dumpster: Copy your MDT Task Sequence


Contents

Ever have a Task Sequence in MDT which you customized and configured to use a specific driver profile, then having to create a second one just for another model and you have to copy everything over?

Well, I did and didn’t like it.

So according to Johan Arwidmark [if you’re into MDT and don’t already know this guy, get to know him!], this is possible in various ways.

Now while I used his approach, I didn’t like the fact that it wasn’t easily re-usable and I still had to provide various data myself. And I’m lazy, so I don’t want that!

As mentioned in my previous MDT related post, do note that I have 2 important variables [MdtDrive & MdtRoot] configured within my PowerShell profile on my MDT server, so please make sure to also predefine these to use the script.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#requires -Modules MicrosoftDeploymentToolkit

[cmdletbinding()]
param (
    [Parameter(Position=0,Mandatory=$true)]
    [string]$NewTaskName,
    [Parameter(Position=1,Mandatory=$true)]
    [string]$NewTaskID
)

begin {
    #region Declaring variables
    $TaskPath = "$($MdtDrive):\Task Sequences"
    $ControlPath = "$MDtroot\Control"
    $OSPath = "$($MdtDrive):\Operating Systems"
    #endregion

    #region Verbose output of declared variables and parameters
    Write-Verbose "New Task Name is '$NewTaskName'"
    Write-Verbose "New Task ID is '$NewTaskID'"
    Write-Verbose "Task Sequence Path is '$TaskPath'"
    Write-Verbose "Task Sequence xml files can be found under '$ControlPath'"
    Write-Verbose "Operating System path is '$OSPath'"
    #endregion
}

process {
    Write-Verbose 'Finding available OS Images'
    $OS = Get-ChildItem -Path $OSPath | Out-GridView -PassThru -Title "Select required OperatingSystem"

    Write-Verbose "Creating new Task Sequence with OS '$($OS.Name)'"
    try {
        Import-MDTTaskSequence -Template client.xml -name $NewTaskName -ID $NewTaskID -OperatingSystemPath "$OSPath\$($OS.Name)" -Path $TaskPath -Version 1.0 -ErrorAction Stop

        Write-Verbose 'Finding available Task Sequences to copy'
        $OldTask = Get-ChildItem -Path $ControlPath -Directory | Sort-Object Name | Select-Object Name,FullName | Out-GridView -PassThru -Title "Select Old TaskSequence"

        Write-Verbose "Copying contents from task sequence '$($OldTask.Name)' to '$NewTaskName'"
        $null = Copy-Item -Path "$ControlPath\$($OldTask.Name)\ts.xml" -Destination "$ControlPath\$NewTaskID" -Force

        if (Test-Path -Path "$ControlPath\$NewTaskID\ts.xml"){
            Write-Output "Task Sequence details copied from '$($OldTask.Name)' to '$NewTaskName'"
        } else {
            Write-Warning "Unable to copy Task Sequence from '$($OldTask.Name)' . $($_.Exception.Message)"
        }
    } catch {
        Write-Warning "Unable to create new Task Sequence. $($_.Exception.Message)"
    }
}

Happy Scripting! 🙂