I am trying to copy file from my local machine to a server destination
My Script: Copy-Item –Path D:\Test.txt –Destination '\\10.10.XX\c$'
Error:
Copy-Item : The network path was not found
At D:\PS_Test_script.ps1:1 char:2
+ Copy-Item –Path D:\Test.txt –Destination '\\10.10.X.28X\c$'
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Copy-Item], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand
The server has credentials, I am guessing that, I have to invoke something to use the credentials.
To keep it somewhat simple, to copy to a folder share which requires different credentials to access you can use New-PSDrive
to map a drive using those credentials
$desiredMappedDrive = 'J'
$desiredMappedDrivePath = '\\10.10.X.X\c$' # Map to administrative C: drive share requires administrator credentials)
$source = 'D:\Test.txt'
$destination = "${desiredMappedDrive}:\temp"
# Get-Credential cmdlet will request that you enter a username and password that has access to the share
New-PSDrive -Name $desiredMappedDrive -PSProvider FileSystem -Root $desiredMappedDrivePath -Credential (Get-Credential)
# after drive is mapped copy file over
Copy-Item -Path $source -Destination $destination -Verbose
Here is a whole different approach using PSSession
no need to map any drives:
$targetComputerName = "127.0.0.1"
$Session = New-PSSession $targetComputerName -Credential 'username'
$DestinationPath = "C:\temp"
$source = 'D:\Test.txt'
Copy-Item -Path $source -ToSession $Session -Destination $DestinationPath
$Session | Remove-PSSession
If you want to execute it as a script you would have to create a [SecureString]
and build a credential object.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.