Forum Discussion
Running remote script to write in file share
Hi
I assume that the user have the correct permission to perform the required action.
Try the following code
$Cred=Get-Credential
Invoke-Command -ComputerName Server1 -ScriptBlock {New-PSDrive -Root '\\FileServer.FQDN.local\MyShare' -PSProvider FileSystem -Name X -Credential $Using:Cred -Persist;Set-Content -Path 'X:\myfile.txt' -Value 'www2w111'}
Hi,
Thanks. I have the permissions, as I'm using a domain admin for the testing.
I tried your code and it works, but I have some issues. I replaced a part of the code to acomplish my objective. It's like this right now:
$Cred=Get-Credential
Invoke-Command -ComputerName servername -ScriptBlock {New-PSDrive -Root '\\share_ip\WindowsUpdateLogs' -PSProvider FileSystem -Name X -Credential $Using:Cred -Persist; (Get-WUList).GetEnumerator() | Export-Csv -Path "X:\$(hostname)-$(get-date -f dd-MM-yyyy)-WindowsUpdate.log" }
But this works once and then if I try to run it a next time on the same server or a different server I get this error message:
A specified logon session does not exist. It may already have been terminated
+ CategoryInfo : InvalidOperation: (X:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
+ PSComputerName : servername
If I leave it for 1h and back for testing, it works again once. So there's something with the expiration of the session which I'm not understanding.
Also, I need to insert this into a cycle to fetch the updates on each server. But if this is running just once, I can't insert it on a cycle.
Thanks in advance.
- farismalaebJul 27, 2022Steel Contributor
I try it on my end, and Yes, the same issue I got
But I notice the following: my file server is a failover cluster, but if I set the write destination to a server node or a single server, the script you provide works fine.
I need to go deeper into this to see what is causing this; for now I think its related to the double-hope issue.
Can you try to write to other node or destination and let me know.
- dmarquesgnJul 27, 2022Iron Contributor
Hi,
My server is not a cluster, it's a regular VM. Also, I've tried with another share on another server and the issue persists, with same error message.
It's quite odd, as it seems that the session is terminated just after has been established.
Thanks
- farismalaebJul 27, 2022Steel Contributor
I found 2 resource that might help you in your issue
https://github.com/PowerShell/PowerShell/issues/11333
and also check this one
https://support.imanami.com/knowledgebase/article/KA-01005/en-us#:~:text=Go%20to%20Security%20Settings%20%3E%20Local,Click%20Apply%20and%20then%20OK.