Forum Discussion
dmarquesgn
Jul 25, 2022Iron Contributor
Running remote script to write in file share
Hi, I'm building a script for checking, downloading and installing updates on Windows Servers, using the PSWindowsUpdate module. My goal is to go to one server a time, check for updates and write a...
farismalaeb
Jul 25, 2022Steel Contributor
Another option if you are in the session by using the Invoke-PSSession, to try a New-PSDrive
https://lazyadmin.nl/powershell/new-psdrive/
https://lazyadmin.nl/powershell/new-psdrive/
- dmarquesgnJul 25, 2022Iron ContributorHi,
Thanks for the notes. Now I understand which is the issue.
But still having some problems to solve it.
Tried the New-PSDrive method, but I have an error. My code is this:
$cred = Get-Credential domain\user (user is domain admin)
$s = New-PSSession -ComputerName servername
Invoke-Command -Session $s -ScriptBlock { New-PSDrive -Name Z -PSProvider FileSystem -Root \\path -Persist }
Got this error:
Access is denied
+ CategoryInfo : InvalidOperation: (Z:PSDriveInfo) [New-PSDrive], Win32Exception
+ FullyQualifiedErrorId : CouldNotMapNetworkDrive,Microsoft.PowerShell.Commands.NewPSDriveCommand
+ PSComputerName : servername
Also, tried this method here, which seemed the easiest one:
https://docs.microsoft.com/en-us/powershell/scripting/learn/remoting/ps-remoting-second-hop?view=powershell-7.2#pass-credentials-inside-an-invoke-command-script-block
But still permissions error.
Do you have any idea of how could I solve this issue? I'm thinking if it would help to create and run a local schedule task, and that way we would avoid a second hop right?
Thanks- farismalaebJul 26, 2022Steel Contributor
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'}
- dmarquesgnJul 26, 2022Iron Contributor
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 : servernameIf 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.