Forum Discussion
DHIMANS
Aug 01, 2023Copper Contributor
Powershell Script not running through Task Scheduler
I am getting the desired output from the below script manually through PowerShell ISE. But the same script is not running through Task Scheduler. It's showing as completed (in Task Scheduler History)...
F:\ is a local disk? Which account are you running the Task? You could add a Start-Transcript c:\temp\log.txt as the first line and a Stop-Transcript as the last line to see what's going on if it's being run as a Task. (Create c:\temp first, or choose another location)
DHIMANS
Aug 02, 2023Copper Contributor
Yes. F:\ is a local drive. As per my latest observation the task scheduler can't read the encrypted password file (F:\Reports\ImpCred\encrypted_passwd.txt).
************************************************************************************
ConvertTo-SecureString : Key not valid for use in specified state.
At F:\Reports\MapDrive23.ps1:2 char:80
+ ... ts\ImpCred\sencrypted_passwd.txt | ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicExcepti
on
+ FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.Pow
erShell.Commands.ConvertToSecureStringCommand
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the
value of argument "password" is null. Change the value of argument "password" to a non-null
value."
At F:\Reports\MapDrive23.ps1:3 char:10
+ $creds = New-Object -TypeName System.Management.Automation.PSCredenti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewO
bjectCommand
Name Used (GB) Free (GB) Provider Root CurrentLo
cation
---- --------- --------- -------- ---- ---------
U FileSystem \\SERVER1.MICROSOFT.COM....
************************************************************************************
************************************************************************************
ConvertTo-SecureString : Key not valid for use in specified state.
At F:\Reports\MapDrive23.ps1:2 char:80
+ ... ts\ImpCred\sencrypted_passwd.txt | ConvertTo-SecureString
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicExcepti
on
+ FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.Pow
erShell.Commands.ConvertToSecureStringCommand
New-Object : Exception calling ".ctor" with "2" argument(s): "Cannot process argument because the
value of argument "password" is null. Change the value of argument "password" to a non-null
value."
At F:\Reports\MapDrive23.ps1:3 char:10
+ $creds = New-Object -TypeName System.Management.Automation.PSCredenti ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [New-Object], MethodInvocationException
+ FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewO
bjectCommand
Name Used (GB) Free (GB) Provider Root CurrentLo
cation
---- --------- --------- -------- ---- ---------
U FileSystem \\SERVER1.MICROSOFT.COM....
************************************************************************************
- Aug 03, 2023I think you have to encrypt the password with the same user that your Scheduled Task is configured for