linnah
linnah

Reputation: 21

Powershell script not running correctly as a scheduled task on Windows 2008

I have this script which connects to a bunch of servers and gets service status, errors from the Application and System logs and disk space. It runs fine as a scheduled task on Windows XP but has some weird problem on Windows 2008. I am using a function to import the credentials from a separate file where they are stored in encrypted form:

function Import-credential($path) {
    $cred = Import-Clixml $path
    $cred.password = $cred.Password | ConvertTo-SecureString
    New-Object system.Management.Automation.PSCredential(
      $cred.username, $cred.password)
}

This works fine when I run it manually and when it runs as a scheduled task on Windows XP but fails on Windows 2008 with the following error:

New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process ar
gument because the value of argument "password" is null. Change the value of ar
gument "password" to a non-null value."
At F:\Tools\MCHS Server Report\monitor.ps1:9 char:15
+     New-Object <<<<  system.Management.Automation.PSCredential(
    + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvoca 
   tionException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power 
   Shell.Commands.NewObjectCommand

This is line 9:

New-Object system.Management.Automation.PSCredential(

In the task scheduler I have the full path to powershell.exe in Program/Script and -command & "F:\Tools\MCHS Server Report\monitor.ps1" in Add Arguments.

Any ideas?

EDIT: I tried changing the account it runs under to my own (I was using a local account specifically created for the scheduled tasks) and it worked alright. I have 0 ideas as to why it won't work with the other user considering we're both Administrators and seem to have the same privs...

Upvotes: 2

Views: 3295

Answers (1)

gpduck
gpduck

Reputation: 381

ConvertTo-SecureString/ConvertFrom-SecureString by default use an encryption key that is bound to the user and computer that the string was originally encrypted on. If you log in as the service account and save the credential it should be able to decrypt it when it is running as a service.

Upvotes: 4

Related Questions