MDT PowerShell: Creating a New Deployment Share

When you perform Actions in MDT you can use the View Script function to replicate the PowerShell commands that you run. This is so you can store them in a custom script to build up your own automation task(s).

Clicking View Script will open notepad and show the code that was ran to perform the task. In this example, to create a new Deployment Share.

This all works great with the exception of the first script you try to automate. The NewDP.Ps1 script only attaches the drive in MDT and populates it with folders and scripts. The remaining stuff is not done in PowerShell. MDT does some sneaky stuff behind the scenes like erm. . . creating a folder and sharing it!!! Meaning you have to script it yourself.

This omission is probably due to the fact that there isn’t a share creation cmdlet built into PowerShell 2.0. Can’t really defend them on creating the folder though. To this end I’ve created my own script to tackle this issue. It’s paramaterized and uses the NET tool built into the Windows command shell. (Yes, I know there are new share scripts and modules on codeplex, don’t email me them please. I want to use the built in software here.)

# \\ Declare Variables
Param (
 [String]$Path="C:\DeploymentShare",
 [String]$PSDriveName="DS001",
 [String]$Description="MDT Deployment Share",
 [String]$ShareName="DeploymentShare$"
 )

# \\ Import MDT Module
Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"

# \\ Create a new Folder
If (!(Test-Path $Path)) {
 New-Item -Path $Path -Type directory -Verbose
 }

# \\ Create a new Deployment Share
New-PSDrive -Name $PSDriveName -PSProvider "MDTProvider" -Root $Path -Description $Description -NetworkPath \\$env:COMPUTERNAME\$ShareName | add-MDTPersistentDrive

# \\ Share Folder
Net Share $ShareName=$Path "/Grant:Everyone,Full" "/Remark:$Description"

Breaking it down, it’s a very straightforward script but the syntax can trip you up if you’re not careful.

These are the Parameters. They have defaults that will create a folder called DeploymentShare in the root of the C: drive, A PS Drive called DS001 , a description and  a hidden  share called DeploymentShare$. You can use -switches on the command line and pass your own values eg -Path D:\DepShare -Description “Scriptimus Share”

# \\ Declare Variables
Param (
 [String]$Path="C:\DeploymentShare",
 [String]$PSDriveName="DS001",
 [String]$Description="MDT Deployment Share",
 [String]$ShareName="DeploymentShare$"
)

This section Imports the new MDT module so we can use the MDT cmdlets later.

# \\ Import MDT Module
Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"

This section, missing from the original MDT script, will create the folder if it does not exist.

# \\ Create a new Folder
If (!(Test-Path $Path)) {
 New-Item -Path $Path -Type directory -Verbose
 }

This line will create the MDT Deployment Share by filling it with the MDT Scripts and default folders so it can be opened in the deployment workbench.

# \\ Create a new Deployment Share
New-PSDrive -Name $PSDriveName -PSProvider "MDTProvider" -Root $Path -Description $Description -NetworkPath \\$env:COMPUTERNAME\$ShareName | add-MDTPersistentDrive

This line, missing from the original MDT script, will share the folder with full control. Full control is needed so you can save your captured images to your Deployment Share and store log files etc.

# \\ Share Folder
Net Share $ShareName=$Path "/Grant:Everyone,Full" "/Remark:$Description"

It has default values and is parameterized so it can be used to create custom deployment shares.

The full script below is self documenting so you can type get-help .\New-DeploymentShare.ps1 to get full usage information.


<#
.SYNOPSIS
Create a new Microsoft Deployment Toolkit Deployment Share.

.DESCRIPTION
The New-DeploymentShare.ps1 script will import the MDT module, Create a folder then create a new Deployment Share based on the parameters supplied in the variables section or passed to the command line.

.PARAMETER Path
Specifies the path to the Deployment Share.

.PARAMETER PSDriveName
Specifies the label for the the PSSDrive created eg. DS001

.PARAMETER Description
A friendly description for the Deployment Share. Useful in a production environment with multiple DeploymentShares.

.PARAMETER ShareName
A new Deployment Share needs to have a network share for remote access. eg: \\Servername\DeploymentShare$

.EXAMPLE
New-DeploymentShare.ps1
This will create a New Deployment Share in the root of C:\ with the default settings

.EXAMPLE
New-DeploymentShare.ps1 -Path C:\MyShare -PSDriveName DS001 -Description "Deployment Share on Scriptimus01" -ShareName DeployShare$
This will create a New Deployment Share with the custom parameters provided

.NOTES
Author: Andrew Barnes
Date: 4 June 2012

.LINK
Online version: http://scriptimus.wordpress.com

#>

# \\ Declare Variables
Param (
 [String]$Path="C:\DeploymentShare",
 [String]$PSDriveName="DS001",
 [String]$Description="MDT Deployment Share",
 [String]$ShareName="DeploymentShare$"
 )

# \\ Import MDT Module
Import-Module "C:\Program Files\Microsoft Deployment Toolkit\bin\MicrosoftDeploymentToolkit.psd1"

# \\ Create a new Folder
If (!(Test-Path $Path)) {
 New-Item -Path $Path -Type directory -Verbose
 }

# \\ Create a new Deployment Share
New-PSDrive -Name $PSDriveName -PSProvider "MDTProvider" -Root $Path -Description $Description -NetworkPath \\$env:COMPUTERNAME\$ShareName | add-MDTPersistentDrive

# \\ Share Folder
Net Share $ShareName=$Path "/Grant:Everyone,Full" "/Remark:$Description"

When you  save the file, call it New-DeploymentShare.ps1

About these ads

About Andrew Barnes

A Scipting and Deployment Specialist.
This entry was posted in MDT 2012, PowerShell, Scripting and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s