changing password for multiple schedule tasks on multiple servers using powershell

I am trying to change passwords for schedule tasks using a powershell script as shown below. The script works fine but every time we change the passwords we will have to go edit the script to add the new password for all these 5 tasks manually I am trying to find if there is a way to do supply the password only once at a prompt and then it will go in and change the password for all tasks on all servers without me entering the passwords manually in the script as is the case now also don't want to store the password in the script.Appreciate your help!

$serverList=get-content D:\LOC1\LOC\server.txt
foreach ($server in $serverList)
{
Write-Host $server
schtasks /Change /s $Server /RU username /RP 'New_password' /TN "Task1"
schtasks /Change /s $Server /RU username /RP 'New_password' /TN "Task2"
schtasks /Change /s $Server /RU username /RP 'New_password' /TN "Task3"
schtasks /Change /s $Server /RU username /RP 'New_password' /TN "Task4"
schtasks /Change /s $Server /RU username /RP 'New_password' /TN "Task5"
}

Parents
  • Ask for the creds once (ie, AsSecureString), store the password in a variable, user the variable in the script where needed.
    Maybe, something like...

        $TaskUserName = Read-Host -Prompt 'Enter the task username'
        $TaskPassword = Read-Host -Prompt 'Enter a password'-AsSecureString
        $serverList = Get-Content 'D:\LOC1\LOC\server.txt'

        # Loop through the server list for processing
        foreach ($server in $serverList)
        {
            "Processing the task(s) on $server"
            # Execute the task(s) on the servers
            'Task1','Task2','Task3','Task4','Task5' | % {
            "Executing task: $_"
            schtasks /Change /s $Server /RU $TaskUserName /RP $TaskPassword /TN $_
            }
        }

    Note: Note tested, just quickly put together.

Reply Children