PowerShell Script Fails When Run via Task Job

David Marino 136 Reputation points
2022-04-06T18:16:30.04+00:00

I setup a PS script some time ago to login to a remote server and map a drive to a folder in the remote server. This script is currently being used, so I know it works in my environment.
I now need to monitor a folder on the remote server, so I took the appropriate code snippet from my original PS script to use in my folder monitoring script. I can run this new file monitoring script manually without any problems, but when I attempt to schedule it to run from the task scheduler I get the error:
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."

I can manually execute each line of code leading up to and including $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password
without any errors. So I'm not quite sure why I can't run the script via the task scheduler, which is how all the other scripts using my connection code are being executed successfully.
When I attempt to display the $password variable after it's populated with password info ($password = Get-Content -Path $psScript\"cpswd.txt" | ConvertTo-SecureString) I get System.Security.SecureString, which I believe is correct.

I have uploaded my script, any guidance is greatly appreciated as I'm stumped at the moment.

190627-monitorpscript.txt

Windows for business | Windows Server | User experience | PowerShell
Windows for business | Windows Server | User experience | Other
{count} votes

3 answers

Sort by: Most helpful
  1. David Marino 136 Reputation points
    2022-04-06T20:33:12.497+00:00

    @Rich Matheisen
    I'm attaching the log file in which I record the execution of the cmdlets manually and via the task scheduler; each is noted in the file.

    The Task Scheduler version errors out right after the New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password execution.
    The error in the email is 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.".

    That is all the information that is provided.
    I also did a if statement "if ($password -eq $null) { "write out to file $password is null"}". This is executed via the Task scheduler, but not when I kick off the script manually.
    At this point I don't know if anything has been updated on the server by Admin that would cause this issue190617-04062022-logfile.txt. This is a test server. I could try running it on the prod server since it's not a data changing script.

    0 comments No comments

  2. Rich Matheisen 47,901 Reputation points
    2022-04-06T21:41:57.687+00:00

    It seems you're opposed to adding a transcript to you script. Is there a reason for that?

    I don't see how what you're doing with that password ever works!

    $pw = "01000000d08c9ddf0115d1118c7a00c04fc297eb0100000030da7b3fadcf6740a49b4f31f1dda6b10000000002000000000003660000c00000001000000005780f7baac79ee44295a9bd2aa31e370000000004800000a0000000100000000718ad40bb416d7d72426294c88d278718000000a8e7e564efd8902cdc166efd59f1b752f6466050077a4ea814000000b3e496f27b8eb4e7967f89116e53b5946dfd7c00"
    $password = $pw | ConvertTo-SecureString
    $username = "melvin"
    $credential = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password
    

    That code throws TWO errors:

    ConvertTo-SecureString : Key not valid for use in specified state.
    At C:\Junk\Untitled-22.ps1:2 char:19
    + $password = $pw | ConvertTo-SecureString
    +                   ~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [ConvertTo-SecureString], CryptographicException
        + FullyQualifiedErrorId : ImportSecureString_InvalidArgument_CryptographicError,Microsoft.PowerShell.Commands.ConvertToSecureStrin  
       gCommand
    
    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 C:\Junk\Untitled-22.ps1:4 char:15
    + ... redential = New-Object System.Management.Automation.PSCredential -Arg ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodInvocationException
        + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
    

    How did you get the 300+ characters in that file? Did you copy it from another machine? If you did, that not going to work because the encrypted string is created using the certificate on the machine that did the encryption.


  3. Rich Matheisen 47,901 Reputation points
    2022-04-07T01:36:32.823+00:00

    The System.Management.Automation.PSCredential class constructor that takes two arguments requires them to be a System.String object containing the user name, and a System.Security.SecureString object containing the encrypted password. the object type of the second argument is, in your case, System.String (a plain text string of characters).

    I think a much better way to do this is to create the credential object and export it:

    $credential = Get-Credential
    $credential | Export-CliXml -Path 'C:\My\Path\cred.xml'
    

    Then, in the script that needs the credential, import that saved file:

    $credential = Import-CliXml -Path 'C:\My\Path\cred.xml'
    

    No need to mess around with strings, secure strings, and creating credentials using .Net class constructors. It's a lot more readable, too.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.