I'm trying to automate a process of editing a text file on a remote system every morning.
My script:
$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
Now, this worked flawlessly at first, but a few days later I'm hit with this error:
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
Couple key things that make this tricky:
- The remote system is not windows. (IFS of an AS400)
- The IFS is reachable from the windows machine running the script. (I can browse the IFS with windows explorer by entering the IP.)
- The remote system requires credentials to access. (The credentials for an AS400 user.)
- I cant pull the file off the IFS, edit locally on windows, then put it back.
Is there a better way to do this? If not, how can I correct the multi connections error and prevent it from happening again?
I sure someone will ask 'why dont you edit the file on the 400??' I likely will. There were some issues with that which is why I tried powershell in the first place. This was suppose to a 5 min deal but think I just overcomplicated things instead.