Working with XML

Hello again,

Today I want to talk about working with XML in PowerShell. I won’t go into too much detail here as the topic is really well covered in a bunch of places:

http://www.codeproject.com/Articles/61900/PowerShell-and-XML
http://powershell.com/cs/blogs/tobias/archive/2009/02/02/xml-part-2-write-add-and-change-xml-data.aspx
http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/17/use-powershell-to-simplify-working-with-xml-data.aspx

These articles are all helpful for understanding how to work with XML in general. Today, in preperation for my next article I’m going to talk about working with the XML document for the Windows Task scheduler.

The Scheduled tasks XML schema is defined on MSDN here:

http://msdn.microsoft.com/en-us/library/windows/desktop/aa383609(v=vs.85).aspx

We’ll start off working with XML here, grab an XML export of a scheduled task, you can use any old task for the example.
[XML]$task = get-content c:\test.xml
With that you can load up the XML file into memory and overwrite any property you want.

Example:

PS C:\isdps> $task.Task.Triggers.CalendarTrigger

StartBoundary Enabled ScheduleByDay
------------- ------- -------------
2013-01-30T02:50:00 true ScheduleByDay

PS C:\isdps> $task.Task.Triggers.CalendarTrigger.Enabled = "false"
PS C:\isdps> $task.Task.Triggers.CalendarTrigger

StartBoundary Enabled ScheduleByDay
------------- ------- -------------
2013-01-30T02:50:00 false ScheduleByDay

PS C:\isdps> $task.Save('c:\test.xml')

Anyway, once you start playing with it you’ll see a bunch of oportunities for changing the configuration. Remember to review the Task xml schema before you make changes, it’s easy to get an invalid value is you aren’t careful.

As always test before you deploy, I’ve used this to deploy a number of tasks in my production environment automatically. You can use schtasks to import a task based on an xml file.

Thanks for taking the time to read this article, I hope it was useful info, please leave a comment or question if you want anymore info.

Advertisements
This entry was posted in PowerShell 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