Share via


I need to copy a file using Copy-Item to mapped path

Question

Monday, August 21, 2017 9:00 PM

Hello,

     

$credential = New-Object System.Management.Automation.PSCredential($username,$password)

New-PSDrive -Name P -PSProvider FileSystem -Root \10.128.192.60\QDLS\CLEAR_CK\COMPPEXT -Credential $credential

Copy-Item $LocalIntesa P

I get an error that P already exists? How should I reference the mapped path?
Regards,

Bryan

All replies (6)

Tuesday, August 22, 2017 2:00 PM ✅Answered

The error for your use of New-PSDrive should be self-explanatory. Throw a Get-PSDrive and see if there is a PSDrive with name "P" already. If you're going to use New-PSDrive in a function or use it frequently, you should remove the drive that you created within the same script instance so it doesn't cause issues in the future.

Here is one way you could accomplish this:

New-PSDrive -Name source -PSProvider FileSystem -Root "\\source\path\" | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root "\\target\path\" | Out-Null

Copy-Item -Path "source:\filename.ext" -Destination "target:"

Remove-PSDrive source
Remove-PSDrive target

Alternatively, you could just call the FileSystem provider explicitly from the Copy-Item line...

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\path\to\source\filename.ext" -Destination "Microsoft.PowerShell.Core\FileSystem::\\path\to\target\"

As an aside, the Rename-Item error is not reflected in the code you posted above so I can't comment on that, but that error should be pretty easy to suss out from the error text.

Please read the whole thread to see what stage the Q&A is at before proposing an answer that does not follow the thread.

\(ツ)_/


Monday, August 21, 2017 8:41 PM

Hello,

      I'm new to powershell, but I googled how to map a path and the only example I got was this:

$cred ``=`` ``Get``-``Credential
New``-``PSDrive`` ``-``NAME Z``:`` ``-``PSprovider`` ``Filesystem`` ``-``root \\``Computer``\Exchange `` ``-``credentials $cred

How do I pass a password to the credential?

Regards,

Bryan


Monday, August 21, 2017 8:52 PM

I answered my own question with more googling.

$password = "password" | ConvertTo-SecureString -asPlainText -Force

$username = "10.128.192.60\user"

$credential = New-Object System.Management.Automation.PSCredential($username,$password)


Monday, August 21, 2017 9:05 PM

Copy-Item $LocalIntesa  \10.128.192.60\QDLS\CLEAR_CK\COMPPEXT  -Credential $credential

Try checking the help for you CmdLets before asking questions.  The help will save you a lot of time.

\(ツ)_/


Tuesday, August 22, 2017 1:37 PM

Hi, I tried that, I'm still getting an error, the file doesn't exist on the drive yet I'm getting an error that it does?

$username="10.128.192.60\coronany"

$credential

=New-ObjectSystem.Management.Automation.PSCredential($username,$password)

New-PSDrive

-NameP-PSProviderFileSystem-Root\10.128.192.60\QDLS\CLEAR_CK\COMPPEXT-Credential$credential

Copy-Item

$LocalIntesa  \10.128.192.60\QDLS\CLEAR_CK\COMPPEXT  -Credential$credential

Rename-Item : Cannot create a file when that file already exists. At D:\Test1\VSoft_POD.ps1:19 char:1 + Rename-Item $LocalVsoft $LocalIntesa + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (D:\test1\POD_EXTRACT_20170821.csv:String) [Rename-Item], IOException + FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

New-PSDrive : A drive with the name 'P' already exists. At D:\Test1\VSoft_POD.ps1:23 char:1 + New-PSDrive -Name P -PSProvider FileSystem -Root \10.128.192.60\QDLS\CLEAR_CK\C ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceExists: (P:String) [New-PSDrive], SessionStateException + FullyQualifiedErrorId : DriveAlreadyExists,Microsoft.PowerShell.Commands.NewPSDriveCommand

The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials. At D:\Test1\VSoft_POD.ps1:24 char:1 + Copy-Item $LocalIntesa \10.128.192.60\QDLS\CLEAR_CK\COMPPEXT -Credential $cre ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+ CategoryInfo : NotImplemented: (:) [], PSNotSupportedException + FullyQualifiedErrorId : NotSupported


Tuesday, August 22, 2017 1:39 PM

Please do not post colorized code. 

Please carefully read the error as it tells you the exact problem.

The error does not match your code.

\(ツ)_/