Copy-Item to networkpath: incorrect user name or password

5.1k views Asked by At

I have a PowerShell v1 script, that is triggerd by a PLC. It should copy a file from the desktop of the embedded PC to a network path.

If I run the script manually it works just fine, but if the script is triggered by the PLC I will get the following error:

    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
copy-item : The user name or password is incorrect.

Any tips, why I get this error, would be very much appreciated!

1

There are 1 answers

0
hen-ling On BEST ANSWER

Thanks for your help @TheIncorrigible1 after reading your comment I found the problem!

The problem was, that the script started by the plc runs with another user than the manually started script.

So the workaround is to first start powershell with the correct credentials with another script. For example like so:

$usr = 'XXX'

$paswrd = 'XXX'

$securePassword = ConvertTo-SecureString $paswrd -AsPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential $usr, $securePassword

$args = "/path to your script"

Start-Process powershell.exe -Credential $credential -ArgumentList ("-file $args")

downside... password in plain text...