3

I am a fresh beginner to PowerShell.

I have username and password to reach a shared folder in a remote location.

I need to copy the file foo.txt from the current location to \\Bar.foo.myCOmpany.com\logs within a PS1 script that is written for Powershell v3.0.

How can I achieve this?

serenesat
  • 4,524
  • 10
  • 32
  • 51
pencilCake
  • 45,443
  • 73
  • 211
  • 346

3 Answers3

4

I'd make use of BITS. Background Intelligent Transfer Service.

If BitsTransfer module not implemented for your session:

Import-Module BitsTransfer

Sample of using it to transfer a file using a credentials:

$cred = Get-Credential()
$sourcePath = \\server\example\file.txt
$destPath = C:\Local\Destination\
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred

Caveat: If you are executing script within a RemotePS session, then BITS is NOT supported.

Get-Help for Start-BitsTransfer:

SYNTAX

 Start-BitsTransfer [-Source] <string[]> [[-Destination] <string[]>] [-Asynchronous] [-Authentication <string>] [-Credential <PS
Credential>] [-Description <string>] [-DisplayName <string>] [-Priority <string>] [-ProxyAuthentication <string>] [-ProxyBypass
 <string[]>] [-ProxyCredential <PSCredential>] [-ProxyList <Uri[]>] [-ProxyUsage <string>] [-RetryInterval <int>] [-RetryTimeou
t <int>] [-Suspended] [-TransferType <string>] [-Confirm] [-WhatIf] [<CommonParameters>]

More help...

Here is script to create $cred object so you aren't prompted for username/passwod:

    #create active credential object
    $Username = "user"
    $Password = ConvertTo-SecureString ‘pswd’ -AsPlainText -Force
    $cred = New-Object System.Management.Automation.PSCredential $Username, $Password
4

Copy-Item doesn't support the -credential parameter, This does parameter appear but it is not supported in any Windows PowerShell core cmdlets or providers"

You can try the below function to map a network drive and invoke copy

Function Copy-FooItem {

param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
        [string]$username,
        [Parameter(Mandatory=$true,ValueFromPipeline=$True)]
        [string]$password
        )

$net = New-Object -com WScript.Network
$drive = "F:"
$path = "\\Bar.foo.myCOmpany.com\logs"
if (test-path $drive) { $net.RemoveNetworkDrive($drive) }
$net.mapnetworkdrive($drive, $path, $true, $username, $password)
copy-item -path ".\foo.txt"  -destination "\\Bar.foo.myCOmpany.com\logs"
$net.RemoveNetworkDrive($drive)

}

Here's how you can run the function just change the parameters for username and password

Copy-FooItem -username "powershell-enth\vinith" -password "^5^868ashG"
PowerShell
  • 1,821
  • 7
  • 31
  • 53
  • Do I need to close connections before and/or after to target server? I came across that it is necessery when I was browsing the net? – pencilCake Nov 06 '12 at 11:08
  • Anyway, I am having an error when I try this: rrorMessage: The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again without specifying credentials..Exception.Message At line:1 char:1 – pencilCake Nov 06 '12 at 11:19
  • hi try it now Copy-Item doesn't support the -credential parameter, This does parameter appear but it is not supported in any Windows PowerShell core cmdlets or providers" we can map the network drive and make it work, just try and let me know – PowerShell Nov 06 '12 at 12:12
1

You can try with:

copy-item -path .\foo.txt  -destination \remoteservername\logs -credential (get-credential)
CB.
  • 53,323
  • 8
  • 138
  • 146
  • But where do I pass the user name and password? – pencilCake Nov 05 '12 at 20:45
  • `get-credential` open a windows for input-in userid & password. Do you need to read them from file? – CB. Nov 05 '12 at 20:47
  • read this SO answer: http://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password – CB. Nov 05 '12 at 20:51
  • 1
    this just gives an error 'The FileSystem provider supports credentials only on the New-PSDrive cmdlet' for me – Sam Holder Nov 07 '14 at 17:22