Reputation: 135
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
Upvotes: 0
Views: 13092
Reputation: 164
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: Enable PowerShell "Second-Hop" Functionality with CredSSP
Upvotes: 1
Reputation: 27423
You can do it with a scheduled task. The system user can access active directory shares.
icm comp001 {
$action = New-ScheduledTaskAction -Execute 'cmd' -argument '/c \\sccmserver\sources\applications\install.bat > c:\users\admin\install.log 2>&1'
Register-ScheduledTask -action $action -taskname install -user system -force > $null
Start-ScheduledTask -TaskName install
while ((Get-ScheduledTask -TaskName install).State -ne 'Ready') {
Write-Verbose -Message 'Waiting on scheduled task...' }
Get-ScheduledTask install | Get-ScheduledTaskInfo | select LastTaskResult
}
Upvotes: 0