Author Posts

June 16, 2015 at 12:04 pm

Hello Community
I want to automate a script to run every 10 minutes but I'm stuck.
I put the required credentials on the first time I run the script but then it doesn't seem to run (never runs actually).
Furthermore there are logon failure (batch events) in eventlog and this means that every 10 minutes it goes to run the script but something wrong with the credentials happen.

I use the following code:

# Change these three variables to whatever you want
$jobname = "Automate Sth"
$script =  "C:\myScript.ps1"
$repeat = (New-TimeSpan -Minutes 10)

$scriptblock = [scriptblock]::Create($script)
$trigger = New-JobTrigger -Once -At (Get-Date).Date -RepeatIndefinitely -RepetitionInterval $repeat
$msg = "Enter the username and password that will run the task"
$credential = $Host.UI.PromptForCredential("Task username and password",$msg,"$env:userdomain\$env:username",$env:userdomain)
 
$options = New-ScheduledJobOption -RunElevated -ContinueIfGoingOnBattery -StartIfOnBattery
Register-ScheduledJob -Name $jobname -ScriptBlock $scriptblock -Trigger $trigger -ScheduledJobOption $options -Credential $credential

What I'm doing wrong?
Is there any way to put the credentials only at the beginning and then to run the script automatically with no logon failures?
What I have to change in my above "job scheduler" to work fine?

Thank you in advance!!!

June 17, 2015 at 2:20 pm

NOBODY ?????? 🙁 🙁

June 17, 2015 at 10:34 pm

I have no problem with your code at all. It just work.
May be there is a problem with credentials or problem with access under these credentials?

You can save your credentials and use it later (only under the same user who wrote it)

Try to adopt this code:

function New-CredentialObject {
param(
	[string]$UserName,
	[string]$ProtectedPassword
)
	New-Object System.Management.Automation.PSCredential $UserName, ($ProtectedPassword | ConvertTo-SecureString)
}

function Protect-String {
param(
		[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
	[string]$String
)
PROCESS {
	$String | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString
}
}

$protected = 'Pa$$word' | Protect-String
$Credential = New-CredentialObject -User username -ProtectedPassword $protected

June 18, 2015 at 5:22 am

Oh thank for the response!!
Do you mean to put this code (and so the administartor password) inside the script that makes the scheduled job??

June 18, 2015 at 6:03 am

no, script will contain hashed password

you can get in console protected password string
$protected = 'Pa$$word' | Protect-String
$protected

and in script write some like
$Credential = New-CredentialObject -User username -ProtectedPassword '01000000d08c9ddf0115d1118c7a00c04fc297eb010000003f26'

where '01000000d08c9ddf0115d1118c7a00c04fc297eb010000003f26' – is a value of $protected

or if you want more security, use ProtectedData module by Dave Wyatt https://github.com/dlwyatt/ProtectedData

June 18, 2015 at 7:26 am

OK I realize that you mean to pass the password as parameter at my script to do the job.

thank you I will try it and I will write again!!!

June 20, 2015 at 8:16 am

Hello community!!

The problem was that the user that run the scheduled task had not permissions to log on as batch job.

If you deal with this kind of problems you can fix this by doing the following :
Go to
Control Panel -> Administrative Tools -> Local Security Policy -> Local Policies -> User Right Assignment
and look for Log on as Batch Job which user is able to logon as batch job. Add your user to do the job and then look again at task scheduler.