Monday, April 25, 2011

copy-item With Alternate Credentials...

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 evidently powershell's cmdlets such as copy-item, test-path, etc do not support alternate credentials...

Anyone have a suggestion on how best to accomplish my task..?

From stackoverflow
  • 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>] [...]
    
    Jason : I keep receiving a error message stating -credential is not supported
    Darrell Mozingo : TechNet (http://technet.microsoft.com/en-us/library/dd315340.aspx) says "This parameter is not supported by any providers installed with Windows PowerShell." So they're too lazy to implement it and just expect anyone creating a new provider to do it. :(
  • 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

    Jason : I keep receiving a error message stating -credential is not supported
    EBGreen : I suspect it is an issue with unc pathing then.
    Jason : well, this is the error... Test-Path : Cannot retrieve the dynamic parameters for the cmdlet. The provider does not support the use of credentials. Please perform the operation again without specifying credentials.
    Jason : and the UNC looks like this... \\ip\Logs
  • Here is a post where someone got it to work. It looks like it requires a registry change.

    Jason : That guy is double hopping within the same domain. I need to be able to specify an account as I am accessing files on a different domain altogether...
  • 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.

    halr9000 : This is the best answer because the filesystem provider (and thus copy-item) does not support credentials.
  • This question addresses a very related issue that may help using network shares in powershell.

  • Since PowerShell doesn't support "-Credential" usage via many of the cmdlets (very annoying), and mapping a network drive via WMI proved to be very unreliable in PS, I found pre-caching the user credentials via a net use command to work quite well:

    # cache credentials for our network path
    net use \\server\C$ $password /USER:$username
    

    Any operation that uses \\server\C$ in the path seems to work using the *-item cmdlets.

    You can also delete the share when you're done:

    net use \\server\C$ /delete
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.