Change username/password on scheduled tasks.

As stated in a previous post, I have been working on a script to change passwords on scheduled tasks.

I had hoped to find some time to clean up the code, and write some error handling, but a few people asked for the code, so here it is in alpha alpha edition.

ToDo:
Tidy up code
Add some error handling
Add code to update-scheduledtasks created in 2k8(R2) GUI

Description:

The script is assuming you have  Powershell V2, and Quest AD cmdlets.

Here is a picture of the GUI when you run the script.

The Quest AD cmdlets are used to connect to AD, to get a list of computers to check for scheduled tasks

image

Search Account:  Search for scheduled tasks running under the supplied account, you can use wildcards. If you supply DomainName\* (Default value), then it will search for scheduled tasks running under a domain account, you Search Root: The DN path from where you want to build your computer list

OSName: Default value is *server* it’s the value that is the same for all windows servers… So with this value set, it will only return servers from AD

Filter MachineName: Default value is “*” which returns all servers,  example “Exchan*” would return all servers whose names start with “Exchan”

PassWord: Enter the new password for the scheduled tasks.

The Run scheduled tasks with current credentials is not working, since it is not supported against 2003 servers and below, so I remove the functionality.

 

Here is an example of a result.

image

You can click on one and edit the username if you like, otherwise you can just mark the ones you want to change password on, holding down ctrl and click each one.

image

Press Change PWD

 

As stated above, there is no particular error handling,and it takes a while to run depending on how many servers you have.  (I hope to find some time to update the script, to a more “production” ready state soon)

[ps]
########################################################################
# Code Generated By: SAPIEN Technologies, Inc., PrimalForms 2009 v1.1.11.0
# Generated On: 29-01-2011 23:05
# Generated By: CTN
########################################################################

#———————————————-
#region Application Functions
#———————————————-

function OnApplicationLoad {
#Note: This function runs before the form is created
#Note: To get the script directory in the Packager use: Split-Path $hostinvocation.MyCommand.path
#Note: To get the console output in the Packager (Windows Mode) use: $ConsoleOutput (Type: System.Collections.ArrayList)
#Important: Form controls cannot be accessed in this function
#TODO: Add snapins and custom code to validate the application load

return $true #return true for success or false for failure
}

function OnApplicationExit {
#Note: This function runs after the form is closed
#TODO: Add custom code to clean up and unload snapins when the application exits

$script:ExitCode = 0 #Set the exit code for the Packager
}

#endregion

#———————————————-
# Generated Form Function
#———————————————-
function GenerateForm {

#———————————————-
#region Import Assemblies
#———————————————-
[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
#endregion

#———————————————-
#region Generated Form Objects
#———————————————-
[System.Windows.Forms.Application]::EnableVisualStyles()
$form1 = New-Object System.Windows.Forms.Form
$progressbar1 = New-Object System.Windows.Forms.ProgressBar
$label9 = New-Object System.Windows.Forms.Label
$SchTaskPWD = New-Object System.Windows.Forms.TextBox
$label8 = New-Object System.Windows.Forms.Label
$button4 = New-Object System.Windows.Forms.Button
$label7 = New-Object System.Windows.Forms.Label
$SchTaskUsr = New-Object System.Windows.Forms.TextBox
$button3 = New-Object System.Windows.Forms.Button
$label6 = New-Object System.Windows.Forms.Label
$srchMachineNameTXT = New-Object System.Windows.Forms.TextBox
$label5 = New-Object System.Windows.Forms.Label
$SrchOSNameTXT = New-Object System.Windows.Forms.TextBox
$label4 = New-Object System.Windows.Forms.Label
$SrchRootTXT = New-Object System.Windows.Forms.TextBox
$label2 = New-Object System.Windows.Forms.Label
$SrchAccountTxt = New-Object System.Windows.Forms.TextBox
$label1 = New-Object System.Windows.Forms.Label
$textbox1 = New-Object System.Windows.Forms.TextBox
$button2 = New-Object System.Windows.Forms.Button
$datagridview1 = New-Object System.Windows.Forms.DataGridView
$button1 = New-Object System.Windows.Forms.Button
$tooltip1 = New-Object System.Windows.Forms.ToolTip
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#———————————————-
# User Generated Script
#———————————————-

$FormEvent_Load={
#TODO: Initialize Form Controls here
$erroractionpreference = "SilentlyContinue"
Add-PSSnapin *quest*
$domainname = (Get-QADRootDSE).Domain.ToString()
$SrchAccountTxt.Text = "$domainname" + "*"

$SrchRootTXT.Text = (Get-QADRootDSE).DefaultNamingContextDN.ToString()
$SrchOSNameTXT.Text = "*Server*"
$srchMachineNameTXT.Text = "*"
$SchTaskUsr.text = $env:userdomain + "\" + $env:username
$array= new-object System.Collections.ArrayList
$Tasks = @()

}

$handler_button1_Click={
#TODO: Place custom script here

$Servers = get-QADComputer -SearchRoot $SrchRootTXT.Text -OSName $SrchOSNameTXT.Text -Name $srchMachineNameTXT.Text # | where {$_.name -like $($srchMachineNameTXT.Text)}
$progressbar1.Maximum = $Servers.Length

Foreach ($server in $Servers ) {
$serverFQDN = $server.dnsname

$progressbar1.value += 1

$schtask = Schtasks.exe /query /s $serverFQDN /V /FO CSV | ConvertFrom-Csv
#Write-Host "****************** $($server.name) Scheduled Tasks ******************"
if ($schtask) {

Foreach ($sch in $schtask) {
#$Sch."Run As User"
if ($Sch."Run As User" -like "$($domainname)*") {
#Write-Host $serverFQDN ($Sch.TaskName).replace("\","") $Sch.’Run As User’

$SchedTasks = New-Object System.Object
$SchedTasks | Add-Member -type NoteProperty -name ServerName -value $serverFQDN
$SchedTasks | Add-Member -type NoteProperty -name "Scheduled Task" -value ($Sch.TaskName).replace("\","")
$SchedTasks | Add-Member -type NoteProperty -name NextRun -value $Sch.’Next Run Time’
$SchedTasks | Add-Member -type NoteProperty -name Username -value $sch.’Run As User’
$Tasks += $SchedTasks
$SchedTasks

}

}

}

}

$array.AddRange($tasks)
$datagridview1.DataSource = $array
$datagridview1.AutoResizeColumns()

}
$handler_button2_Click={
foreach ($row in $datagridview1.SelectedRows) {
#[System.Windows.Forms.MessageBox]::Show($row.Cells[0].Value)
$StrCmd = schtasks.exe /change /s "$($row.Cells[0].Value)" /RU "$($row.Cells[3].Value)" /RP "$($textbox1.text)" /TN ""$($row.Cells[1].Value)""
#Invoke-Expression $strCmd #|Out-File c:\temp\userlog.txt
Write-Host $LastExitCode
Write-Host $StrCmd
Clear-Variable StrCmd -ErrorAction SilentlyContinue

}

}

$handler_label1_Click={
#TODO: Place custom script here

}

$handler_label2_Click={
#TODO: Place custom script here

}

$handler_button3_Click={
#TODO: Place custom script here
if ($textbox1.PasswordChar -notlike "0" ) {$textbox1.PasswordChar = $null ;$textBox1.Text = $textBox1.Text}
else {$textbox1.PasswordChar = "*";$textBox1.Text = $textBox1.Text}

}

$handler_label7_Click={
#TODO: Place custom script here

}

$handler_tooltip1_Popup=[System.Windows.Forms.PopupEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.PopupEventArgs]
#TODO: Place custom script here

}

$ShowHelp={
#TODO: Place custom script here
#display popup help
Switch ($this.name) {
"SrchAccountTxt" {$tip="Examples:
n Domainname\AdminGuy Search for a specific account.n DomainName\admin* For all usersnames in the domain that starts with admin n Domainname\* All Domain users"}
"SrchRootTXT" {$tip="Enter DN for the OU, that you want to be the base of the AD search"}
"SrchOSNameTXT" {$tip="Enter a complete or partial machinename that you want to search for. Wildcards are permitted n Exhange* will find all servers whos name starts with 'Exchange'"}
default {$tip="No help found for $($this.name)"}
}
$tooltip1.SetToolTip($this,$tip)

}

$handler_button4_Click={
#TODO: Place custom script here
if ($SchTaskPWD.PasswordChar -notlike "
0" ) {$SchTaskPWD.PasswordChar = $null ;$SchTaskPWD.Text = $SchTaskPWD.Text}
else {$SchTaskPWD.PasswordChar = "*";$SchTaskPWD.Text = $SchTaskPWD.Text}
}

#———————————————-
# Generated Events
#———————————————-

$Form_StateCorrection_Load=
{
#Correct the initial state of the form to prevent the .Net maximized form issue
$form1.WindowState = $InitialFormWindowState
}

#———————————————-
#region Generated Form Code
#———————————————-
#
# form1
#
$form1.Controls.Add($progressbar1)
$form1.Controls.Add($label9)
$form1.Controls.Add($SchTaskPWD)
$form1.Controls.Add($label8)
$form1.Controls.Add($button4)
$form1.Controls.Add($label7)
$form1.Controls.Add($SchTaskUsr)
$form1.Controls.Add($button3)
$form1.Controls.Add($label6)
$form1.Controls.Add($srchMachineNameTXT)
$form1.Controls.Add($label5)
$form1.Controls.Add($SrchOSNameTXT)
$form1.Controls.Add($label4)
$form1.Controls.Add($SrchRootTXT)
$form1.Controls.Add($label2)
$form1.Controls.Add($SrchAccountTxt)
$form1.Controls.Add($label1)
$form1.Controls.Add($textbox1)
$form1.Controls.Add($button2)
$form1.Controls.Add($datagridview1)
$form1.Controls.Add($button1)
$form1.ClientSize = New-Object System.Drawing.Size(897,567)
$form1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$form1.Name = "form1"
$form1.Text = "Change Scheduled Task Passwords v. 0.0.1"
$form1.add_Load($FormEvent_Load)
#
# progressbar1
#
$progressbar1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$progressbar1.Location = New-Object System.Drawing.Point(34,528)
$progressbar1.Name = "progressbar1"
$progressbar1.Size = New-Object System.Drawing.Size(829,23)
$progressbar1.TabIndex = 23
#
# label9
#
$label9.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label9.Location = New-Object System.Drawing.Point(505,98)
$label9.Name = "label9"
$label9.Size = New-Object System.Drawing.Size(108,23)
$label9.TabIndex = 22
$label9.Text = "Schtasks Password"
#
# SchTaskPWD
#
$SchTaskPWD.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$SchTaskPWD.Location = New-Object System.Drawing.Point(619,95)
$SchTaskPWD.Name = "SchTaskPWD"
$SchTaskPWD.PasswordChar = ‘*’
$SchTaskPWD.Size = New-Object System.Drawing.Size(161,20)
$SchTaskPWD.TabIndex = 21
#
# label8
#
$label8.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label8.Location = New-Object System.Drawing.Point(505,64)
$label8.Name = "label8"
$label8.Size = New-Object System.Drawing.Size(108,23)
$label8.TabIndex = 20
$label8.Text = "Schtasks username"
#
# button4
#
$button4.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$button4.Location = New-Object System.Drawing.Point(786,93)
$button4.Name = "button4"
$button4.Size = New-Object System.Drawing.Size(81,23)
$button4.TabIndex = 19
$button4.Text = "Toggle PWD"
$button4.UseVisualStyleBackColor = $True
$button4.add_Click($handler_button4_Click)
#
# label7
#
$label7.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label7.Location = New-Object System.Drawing.Point(506,36)
$label7.Name = "label7"
$label7.Size = New-Object System.Drawing.Size(218,23)
$label7.TabIndex = 18
$label7.Text = "Run Schtasks with following credentials"
#
# SchTaskUsr
#
$SchTaskUsr.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$SchTaskUsr.Location = New-Object System.Drawing.Point(619,63)
$SchTaskUsr.Name = "SchTaskUsr"
$SchTaskUsr.Size = New-Object System.Drawing.Size(161,20)
$SchTaskUsr.TabIndex = 17
#
# button3
#
$button3.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$button3.Location = New-Object System.Drawing.Point(439,123)
$button3.Name = "button3"
$button3.Size = New-Object System.Drawing.Size(79,23)
$button3.TabIndex = 16
$button3.Text = "Toggle PWD"
$button3.UseVisualStyleBackColor = $True
$button3.add_Click($handler_button3_Click)
#
# label6
#
$label6.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label6.Location = New-Object System.Drawing.Point(169,92)
$label6.Name = "label6"
$label6.Size = New-Object System.Drawing.Size(106,23)
$label6.TabIndex = 15
$label6.Text = "Filter MachineName"
#
# srchMachineNameTXT
#
$srchMachineNameTXT.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$srchMachineNameTXT.Location = New-Object System.Drawing.Point(284,92)
$srchMachineNameTXT.Name = "srchMachineNameTXT"
$srchMachineNameTXT.Size = New-Object System.Drawing.Size(148,20)
$srchMachineNameTXT.TabIndex = 14
#
# label5
#
$label5.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label5.Location = New-Object System.Drawing.Point(172,66)
$label5.Name = "label5"
$label5.Size = New-Object System.Drawing.Size(101,23)
$label5.TabIndex = 13
$label5.Text = "OS Name"
#
# SrchOSNameTXT
#
$SrchOSNameTXT.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$SrchOSNameTXT.Location = New-Object System.Drawing.Point(284,66)
$SrchOSNameTXT.Name = "SrchOSNameTXT"
$SrchOSNameTXT.Size = New-Object System.Drawing.Size(148,20)
$SrchOSNameTXT.TabIndex = 12
#
# label4
#
$label4.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label4.Location = New-Object System.Drawing.Point(172,41)
$label4.Name = "label4"
$label4.Size = New-Object System.Drawing.Size(100,23)
$label4.TabIndex = 11
$label4.Text = "SearchRoot "
#
# SrchRootTXT
#
$SrchRootTXT.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$SrchRootTXT.Location = New-Object System.Drawing.Point(284,39)
$SrchRootTXT.Name = "SrchRootTXT"
$SrchRootTXT.Size = New-Object System.Drawing.Size(148,20)
$SrchRootTXT.TabIndex = 10
#
# label2
#
$label2.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label2.Location = New-Object System.Drawing.Point(172,14)
$label2.Name = "label2"
$label2.Size = New-Object System.Drawing.Size(100,23)
$label2.TabIndex = 8
$label2.Text = "Search Accounts"
#
# SrchAccountTxt
#
$SrchAccountTxt.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$SrchAccountTxt.Location = New-Object System.Drawing.Point(284,12)
$SrchAccountTxt.Name = "SrchAccountTxt"
$SrchAccountTxt.Size = New-Object System.Drawing.Size(148,20)
$SrchAccountTxt.TabIndex = 7
#
# label1
#
$label1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$label1.Location = New-Object System.Drawing.Point(172,124)
$label1.Name = "label1"
$label1.Size = New-Object System.Drawing.Size(100,23)
$label1.TabIndex = 6
$label1.Text = "New Password"
#
# textbox1
#
$textbox1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$textbox1.Location = New-Object System.Drawing.Point(284,124)
$textbox1.Name = "textbox1"
$textbox1.PasswordChar = ‘*’
$textbox1.Size = New-Object System.Drawing.Size(148,20)
$textbox1.TabIndex = 5
#
# button2
#
$button2.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$button2.Location = New-Object System.Drawing.Point(71,42)
$button2.Name = "button2"
$button2.Size = New-Object System.Drawing.Size(89,23)
$button2.TabIndex = 4
$button2.Text = "Change PWD"
$button2.UseVisualStyleBackColor = $True
$button2.add_Click($handler_button2_Click)
#
# datagridview1
#
$System_Windows_Forms_DataGridViewCellStyle_16 = New-Object System.Windows.Forms.DataGridViewCellStyle
$System_Windows_Forms_DataGridViewCellStyle_16.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$System_Windows_Forms_DataGridViewCellStyle_16.BackColor = [System.Drawing.Color]::FromArgb(255,240,240,240)
$System_Windows_Forms_DataGridViewCellStyle_16.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1)
$System_Windows_Forms_DataGridViewCellStyle_16.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$System_Windows_Forms_DataGridViewCellStyle_16.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,51,153,255)
$System_Windows_Forms_DataGridViewCellStyle_16.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$System_Windows_Forms_DataGridViewCellStyle_16.WrapMode = [System.Windows.Forms.DataGridViewTriState]::True
$datagridview1.ColumnHeadersDefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_16
$datagridview1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$System_Windows_Forms_DataGridViewCellStyle_17 = New-Object System.Windows.Forms.DataGridViewCellStyle
$System_Windows_Forms_DataGridViewCellStyle_17.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$System_Windows_Forms_DataGridViewCellStyle_17.BackColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$System_Windows_Forms_DataGridViewCellStyle_17.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1)
$System_Windows_Forms_DataGridViewCellStyle_17.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$System_Windows_Forms_DataGridViewCellStyle_17.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,51,153,255)
$System_Windows_Forms_DataGridViewCellStyle_17.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$System_Windows_Forms_DataGridViewCellStyle_17.WrapMode = [System.Windows.Forms.DataGridViewTriState]::False
$datagridview1.DefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_17
$datagridview1.Location = New-Object System.Drawing.Point(34,165)
$datagridview1.Name = "datagridview1"
$System_Windows_Forms_DataGridViewCellStyle_18 = New-Object System.Windows.Forms.DataGridViewCellStyle
$System_Windows_Forms_DataGridViewCellStyle_18.Alignment = [System.Windows.Forms.DataGridViewContentAlignment]::MiddleLeft
$System_Windows_Forms_DataGridViewCellStyle_18.BackColor = [System.Drawing.Color]::FromArgb(255,240,240,240)
$System_Windows_Forms_DataGridViewCellStyle_18.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",8.25,0,3,1)
$System_Windows_Forms_DataGridViewCellStyle_18.ForeColor = [System.Drawing.Color]::FromArgb(255,0,0,0)
$System_Windows_Forms_DataGridViewCellStyle_18.SelectionBackColor = [System.Drawing.Color]::FromArgb(255,51,153,255)
$System_Windows_Forms_DataGridViewCellStyle_18.SelectionForeColor = [System.Drawing.Color]::FromArgb(255,255,255,255)
$System_Windows_Forms_DataGridViewCellStyle_18.WrapMode = [System.Windows.Forms.DataGridViewTriState]::True
$datagridview1.RowHeadersDefaultCellStyle = $System_Windows_Forms_DataGridViewCellStyle_18
$datagridview1.Size = New-Object System.Drawing.Size(829,356)
$datagridview1.TabIndex = 2
#
# button1
#
$button1.DataBindings.DefaultDataSourceUpdateMode = [System.Windows.Forms.DataSourceUpdateMode]::OnValidation
$button1.Location = New-Object System.Drawing.Point(71,12)
$button1.Name = "button1"
$button1.Size = New-Object System.Drawing.Size(89,23)
$button1.TabIndex = 1
$button1.Text = "Get Servers"
$button1.UseVisualStyleBackColor = $True
$button1.add_Click($handler_button1_Click)
#
# tooltip1
#
#endregion Generated Form Code

#———————————————-

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($Form_StateCorrection_Load)
#Show the Form
return $form1.ShowDialog()

} #End Function

#Call OnApplicationLoad to initialize
if(OnApplicationLoad -eq $true)
{
#Create the form
GenerateForm | Out-Null
#Perform cleanup
OnApplicationExit
}

[/ps]