Unable to access UNC paths in remote Powershell session

I cannot access UNC paths on my servers in a remote Powershell session from my local machine. I can use them directly from the Servers Cmd command line.

Actually, I went to the server and mapped the UNC path as a local drive (say, X :). The option "Connect by connection" is used.

I have a batch file that is on this X drive: I want to run it remotely using the invoke command from my local script. But it fails.

It says: "Cannot find the drive. A drive named X does not exist."

In addition, when I try to map a drive using the net use command in the script block, then it also throws an error - System error 1223 - Native Command Error.

I use administrator credentials to log in to this server.

Can someone help me with this, all I want to do is run a remote script that is on this UNC path?

Also, when I find the UNC path on the server as a local drive, why can't I use it in a remote PS session?

Thanks at Advance. TS

+7
source share
3 answers

You really have 3 different things here.

1 and 3. Disks are displayed only during interactive login. Therefore, when you moved to another computer, connected the drive, and then logged out / disconnected, that connected drive was disconnected. With the exception of user sessions with an interactive graphical interface, you cannot depend on the drive letter you do not create yourself. In scripts or in any remote session, just use UNC paths for everything - it's more reliable.

2 When you try to connect a drive in a remote PS session, you are faced with the so-called double-jump problem. There is a solution for this, but there are additional settings that you must make. See Dual Access Access to copy files without CredSSP

+10
source

Alroc's helpful answer explains the problem well and points out the resources to solve it using preconfiguration.

As for the special solution for accessing a network share in a remote session (PSv3 +) :

  • Pass the credentials for the session through a variable; e.g. -Credential $cred

  • Then use these credentials inside the session — for example, like $using:cred — to install an auxiliary drive in the session area, which displays the network location using New-PSDrive .
    After mapping a drive, the target location becomes available - either by drive name or directly by UNC path.

Note: This is a variation of the approach discovered by the OP him / herself and discussed briefly in a comment on alroc answer, except that [TG43] is used rather than [TG44], which obviates the need for retrieving the password as plain text .

The following code example demonstrates running a script from a network share from a remote session:

 # A sample script to run from a network share on a remote computer. $script = '\\server-001\install\Install-Agent.ps1' # A sample target computer. $computer = 'ws-002' # Obtain the credentials for the remote session and store them in a variable. $cred = Get-Credential $env:USERNAME Invoke-Command -ComputerName $computer -Credential $cred { # Map the target network share as a dummy PS drive using the passed-through # credentials. # You may - but needn't - use this drive; the mere fact of having established # a drive with valid credentials makes the network location accessible in the # session, even with direct use of UNC paths. $null = New-PSDrive -Credential $using:cred dummy -Root (Split-Path -Parent $using:script) -PSProvider FileSystem # Invoke the script via its UNC path. & $using:script } 
+1
source

You can try to open the script by calling WmiMethod:

 $cmd = "CMD.EXE /c *\\\servername\somefolder\yourscript*.cmd" Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName *servername* 

Note that this will run the script on the server. You can do much more with this, for example, installing the package remotely (after copying the package locally on the remote computer).

Hope this help!

-2
source

Source: https://habr.com/ru/post/950090/


All Articles