将项目复制到网络路径:不正确的用户名或密码

Copy-Item to networkpath: incorrect user name or password

我有一个由 PLC 触发的 PowerShell v1 脚本。它应该将文件从嵌入式 PC 的桌面复制到网络路径。

如果我 运行 手动脚本它工作得很好,但如果脚本是由 PLC 触发的,我会得到以下错误:

    + CategoryInfo : NotSpecified: (:) [复制项目], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
copy-item : 用户名或密码不正确。

任何提示,为什么我会收到此错误,将不胜感激!

感谢@TheIncorrigible1 的帮助,在阅读您的评论后我发现了问题!

问题是,由 plc 启动的脚本与手动启动的脚本不同的另一个用户运行。

因此解决方法是首先使用另一个脚本使用正确的凭据启动 powershell。例如像这样:

$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")

缺点...纯文本密码...