copy-item With Alternate Credentials

后端 未结 12 2055
长情又很酷
长情又很酷 2020-12-01 05:29

I\'m using the CTP of powershell v2. I have a script written that needs to go out to various network shares in our dmz and copy some files. However, the issue I have is that

相关标签:
12条回答
  • 2020-12-01 05:57

    that evidently powershell's cmdlets such as copy-item, test-path, etc do not support alternate credentials...

    It looks like they do here, copy-item certainly includes a -Credential parameter.

    PS C:\> gcm -syn copy-item
    Copy-Item [-Path] <String[]> [[-Destination] <String>] [-Container] [-Force] [-Filter <String>] [-I
    nclude <String[]>] [-Exclude <String[]>] [-Recurse] [-PassThru] [-Credential <PSCredential>] [...]
    
    0 讨论(0)
  • 2020-12-01 05:57

    You should be able to pass whatever credentials you want to the -Credential parameter. So something like:

    $cred = Get-Credential

    [Enter the credentials]

    Copy-Item -Path $from -Destination $to -Credential $cred

    0 讨论(0)
  • 2020-12-01 05:59

    I have encountered this recently, and in the most recent versions of Powershell there is a new BitsTransfer Module, which allows file transfers using BITS, and supports the use of the -Credential parameter.

    The following sample shows how to use the BitsTransfer module to copy a file from a network share to a local machine, using a specified PSCredential object.

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

    Another way to handle this is using the standard "net use" command. This command, however, does not support a "securestring" password, so after obtaining the credential object, you have to get a decrypted version of the password to pass to the "net use" command.

    $cred = Get-Credential
    $networkCred = $cred.GetNetworkCredential()
    net use \\server\example\ $networkCred.Password /USER:$networkCred.UserName
    Copy-Item \\server\example\file.txt C:\Local\Destination\
    
    0 讨论(0)
  • 2020-12-01 06:00

    I would try to map a drive to the remote system (using 'net use' or WshNetwork.MapNetworkDrive, both methods support credentials) and then use copy-item.

    0 讨论(0)
  • 2020-12-01 06:02

    PowerShell 3.0 now supports for credentials on the FileSystem provider. To use alternate credentials, simply use the Credential parameter on the New-PSDrive cmdlet

    PS > New-PSDrive -Name J -PSProvider FileSystem -Root \\server001\sharename -Credential mydomain\travisj -Persist
    

    After this command you can now access the newly created drive and do other operations including copy or move files like normal drive. here is the full solution:

    $Source = "C:\Downloads\myfile.txt"
    $Dest   = "\\10.149.12.162\c$\skumar"
    $Username = "administrator"
    $Password = ConvertTo-SecureString "Complex_Passw0rd" -AsPlainText -Force
    $mycreds = New-Object System.Management.Automation.PSCredential($Username, $Password)
    
    New-PSDrive -Name J -PSProvider FileSystem -Root $Dest -Credential $mycreds -Persist
    Copy-Item -Path $Source -Destination "J:\myfile.txt"
    
    0 讨论(0)
  • 2020-12-01 06:06

    Here is a post where someone got it to work. It looks like it requires a registry change.

    0 讨论(0)
提交回复
热议问题