1

我正在尝试自动化每天早上在远程系统上编辑文本文件的过程。

我的脚本:

$User = "USER"
$File = "C:\secureString.txt"
$c = New-Object -TypeName System.Management.Automation.PSCredential `
 -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)

New-PSDrive -Name "X" -PSProvider FileSystem -Root "\\192.168.1.69\ifs\home" -Credential $c
(Get-Content X:\textFile.txt).Replace(","," ") | Set-Content X:\textFile.TXT

现在,这起初完美无缺,但几天后我遇到了这个错误:

New-PSDrive : Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again

几个使这变得棘手的关键因素:

  • 远程系统不是windows。(AS400 的 IFS)
  • IFS 可从运行脚本的 Windows 机器访问。(我可以通过输入IP使用Windows资源管理器浏览IFS。)
  • 远程系统需要凭据才能访问。(AS400 用户的凭据。)
  • 我无法从 IFS 中提取文件,在 Windows 上进行本地编辑,然后将其放回原处。

有一个更好的方法吗?如果没有,我该如何纠正多连接错误并防止它再次发生?

我肯定有人会问“你为什么不在 400 上编辑文件??” 我很可能会。有一些问题,这就是我首先尝试 powershell 的原因。这本来是一个5 分钟的交易,但我认为我只是把事情复杂化了。

4

0 回答 0