Powershell – How to access a network shared drive inside Invoke-command

powershellpowershell-2.0

I was using below script to copy files from network share to local drive. But,I was not able to access path and getting Path not found error. MY use case is I need to execute this script from Jenkins server and remote into server1 and then have copy files from shared directory (\server2\QlikView) which is already mounted to the server1 as S:\ drive. I was able to access this shared path from powershell If I run the command from server1.But, not inside Invoke-Command script block as shown. Any thoughts on this?

$server = "server1"
$source_dir = "\\server2\QlikView"
$processing_dir = "M:\script_test\processing"
$processed_dir = ""
$user = 'Domain\user1'
$Password = '******'
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $User, $SecurePassword

 Invoke-Command -ComputerName $server -Credential $cred -ScriptBlock {
  param ($server,$source_dir,$processing_dir)
echo "$source_dir"
 Test-path $source_dir
 copy-item -Path $source_dir\* -Destination M:\script_test
  } -ArgumentList $server,$source_dir,$processing_dir

Best Answer

By default PowerShell remoting does not allow credential delegation or "second hop". If you want to connect to a remote machine (in this case a network share) from a remoting session, you need to allow the credentials to be delegated to that machine. To allow it you need to configure CredSSP. Take a look here for details on the problems as well as how to set it up: https://blogs.technet.microsoft.com/heyscriptingguy/2012/11/14/enable-powershell-second-hop-functionality-with-credssp/

Related Topic