We upgraded our ETL box from WS 2003 to 2008. Win Server 2008's scheduled task system is completely revamped (meaning you can't just drag tasks over).
I wrote this little script to:
- Read all the tasks into a CSV
- Loop thru each task, replace a executable path (due to install differences) and output to the new XML format for WS 2008.
- *Optional - actually import the tasks using the 'CREATE' command of schtasks
#### Upgrade WS2003 scheduled tasks to WS2008 format # Vars $remote_server = "yourserver.domain.com" $xml_destination = "c:\" $nl = [Environment]::Newline # Get data from schtasks query $task_data = schtasks /Query /S $remote_server /FO csv # Parse CSV $task_data_csv = ConvertFrom-Csv $task_data # Loop thru CSV, building string and concatenating to output Foreach($entry in $task_data_csv) { #Trim leading \ from output $task_name = $entry.TaskName.TrimStart("\") #build export path $exported_task_path = "" + $xml_destination + $task_name + ".xml" try { #Run command; stuff returned data into variable $xmldata = schtasks /query /s $remote_server /tn "$task_name" /xml ### OPTIONAL XML REGEX STRING REPLACE ### # I needed to remap a bunch of strings to match our new server environment - you may not need this $OFS="`r" $old_status = "(?<=\.*?)(\ true\ )" $new_status = "false " $xmldata = [regex]::Replace($xmldata, $old_status, $new_status, "Multiline") $old_path = "c:\oldinstallpath\bin" $new_path = "c:\newinstallpath\c10_64\bin" $xmldata = [regex]::Replace($xmldata, [regex]::Escape($old_path), $new_path, "Multiline") $old_user = "OldComp\username" $new_user = "NewComp\username" $xmldata = [regex]::Replace($xmldata, [regex]::Escape($old_user), $new_user, "Multiline") $old_LogonElement = "InteractiveTokenOrPassword " $new_LogonElement = "InteractiveToken " $xmldata = [regex]::Replace($xmldata, [regex]::Escape($old_LogonElement), $new_LogonElement, "Multiline") $xmldata = $xmldata.split("`r") ### END OPTIONAL XML REGEX STRING REPLACE ### $xmldata | out-file $exported_task_path Try{ schtasks /Create /XML "$exported_task_path" /TN "TASKS_FOLDERNAME_HERE\$task_name" /F } Catch{ Write-host "[ERROR] ** Task: " + $task_name + "Could not be imported" Write-host "[ERROR] ** Error message was: " + $_.Exception.ToString() } } catch [exception] { Write-host "[ERROR] ** The following command errored during the run: " + $output_text Write-host "[ERROR] ** Error message was: " + $_.Exception.ToString() } }
No comments:
Post a Comment