MDT: Running a single PowerShell Cmdlet from a Task Sequence Step

Sometimes I want to perform small tasks that don’t really require a script creation. In those cases I use a Run Command Line task sequence step. It was always easy for cmd.exe and it’s just as easy for PowerShell.exe(once you have the syntax)

This example will create a folder called Files using the New-Item Cmdlet.

PowerShell.exe -Command “& {New-Item -type Directory -Path C:\Files}”

image

The obvious drawback in using this method is that there’s no logging features. The step will return a 0 (success) even if the cmdlet fails. Hmm. so perhaps not so good an idea then. Although, technically the script block {} can handle a pipe line and multiple commands including any error handling(a Try. . Catch for example) that you could construct.

Disclaimer – Use this method only if you know what you’re doing.

MDT team, please add a new task sequence step that runs a single PowerShell cmdlet and handles errors and captures logs etc.

Advertisements

About Andrew Barnes

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

10 Responses to MDT: Running a single PowerShell Cmdlet from a Task Sequence Step

  1. Clyde says:

    Hi Andrew,
    Love your blog… found some really great tricks with MDT.
    I was looking at this post because I wanted to run a powershell command from a batchfile (not through MDT) and wanted to point out something I found. Alias’s do not play well in a batch file. The one in particular that I was using was ‘%’ for ‘foreach-object’. Using this, I kept getting the error ‘Expressions are only allowed as the first element of a pipeline.’ Once I replaced the ‘%’ with ‘ForEach-Object’ everything is working great.

    Thanks for the blog and keep up the great work.

    Like

  2. Harry says:

    Thanks for a great blog. Can you recommend any powershell coding software for the Ipad?

    Like

  3. Jonatas says:

    Very usefully! Help me a lot! Thanks

    Like

  4. Andy says:

    Hi Andrew,
    This is very useful for performing simple tasks but I wonder if this method can be used to execute multiple scripts located in the same location. I’ve been trying to get the following to work as a task in MDT, but the path variable in the get-childitem doesn’t seem to work when using the method above and I’m wondering why?

    This works from the PowerShell command prompt after ztiutility module imported:
    foreach ($i in Get-ChildItem $tsenv:deployroot\scripts -filter *.ps1) { if (($i.FullName).Contains(‘Report-‘)) {powershell -command $i.FullName} }

    Yet this does not work when invoked using your powershell -command method:
    PowerShell -command “} }”

    The command basically executes all the ps1 scripts in the SCRIPT folder if the script file name contains ‘Report-‘, however when executed using your method, the deployroot path variable isn’t passed inside the script block.

    Thank you in advance!
    -Andy

    Like

    • I see what you’re trying to do there but advise against it. I say 1 task per step. That way if a step fails then it’s easy to isolate and troubleshoot.

      Like

      • Andy says:

        Thanks Andrew! In theory it should have worked, but I get what you’re saying. As a workaround i just wrote a simple powershell script to execute each reporting script. Hard to explain why I’m doing it this way, but i have about eight custom reports I generate on the fly that then need to be sent to various people and systems, so having one task to execute each just made sense in my brain.

        Thanks again for your help!

        Like

  5. Josh R. says:

    Hey Andrew, I know this is a older post but I have an idea I want to accomplish but need some help. I’m using MDT 2012 and I am wanting to send a email message to the technician that is installing the image. I’ve thought about creating a new UDI Wizard but can not get it to work. Do you have any way to resolve this?

    I know I can send a email from a scheduled task to a mass email distro but I’m wanting to send an email from the PC when it is complete but to the technician that enter’s their credentials for network file share access.

    Thanks for the help!

    Like

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