copy-item With Alternate Credentials

后端 未结 12 2060
长情又很酷
长情又很酷 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 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"
    

提交回复
热议问题