copy with alternate credentials

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 3 months ago.

  • Author
    Posts
  • #5102

    by turbomcp at 2012-09-12 13:07:06

    Hi there
    i have to admit i am starting to like powershell(scary:))
    so i want to do more stuff with it ,even though i dont know how to do some stuff i like the learning experience
    anyway,

    i am trying to copy a file to multiple exchange servers
    seems pretty easy something like:
    $servers = Get-Content "D:\Scripting\Srvlist.txt"
    $file=c:\test\
    foreach ($server in $servers)
    {
    if (test-connection $server -quiet)
    {$copy1={Copy-Item $file -Destination \\$server\r$\ -force}}
    For($i = 1; $i -le $copy1.count; $i++){Write-Progress -Activity "copying file to $server" -status "copying $File to $server" -percentComplete ($i / $copy1.count*100)}
    }

    only thing is
    my user on my workstation doesnt have permissions(regular user) so i somehow need to add the get-credentials into it and im not sure how:)

    by DonJ at 2012-09-12 14:03:02

    None of the -Item commands support alternate credentials; they're designed to work with PSDrives.

    So you would first use New-PSDrive, with the -Credential parameter, to map a drive to the remote server's file system. Just like mapping a network drive. Then, Copy-Item will be able to make the copy. You could then remove the PSDrive if you were done with it.

    Keep in mind that PSDrives don't have drive letters, they have drive names. So you don't have to pick M: or Q: or something; you could put REMOTE1: as the drive name, or even use the server's name as the drive name. Whatever you want.

    If you need to use the same credential for all of the servers:


    $cred = Get-Credential DOMAIN\Username

    And then pass $cred to the -Credential parameter of New-PSDrive. That way you only get prompted once for the password.

    by turbomcp at 2012-09-12 15:42:55

    so something like this should work:)
    $servers = Get-Content "D:\Scripting\Srvlist.txt"
    $files=c:\test\
    $cred = Get-Credential DOMAIN\Username
    $destination=new-Psdrive -name $server -PsProvider FileSystem -root \\$server\r$ -Credential $cred
    foreach ($server in $servers)
    {
    if (test-connection $server -quiet)
    {$copy1={Copy-Item $files -Destination $destination -force}}
    For($i = 1; $i -le $copy1.count; $i++){Write-Progress -Activity "copying file to $server" -status "copying $File to $server" -percentComplete ($i / $copy1.count*100)}
    Remove-PSDrive $server

    }

    by DonJ at 2012-09-12 15:52:40

    Uh, no. Several problems.

    1. You're only creating ONE PSDrive. When you do so, $server isn't defined. So that won't work. You probably want that INSIDE your ForEach loop. I'd think you'd want to map the PSDrive after you test the connection, to help ensure that the drive mapping won't fail, no?

    2. You can't pass $destination, which is a PSDrive, to -Destination, I don't think. -Destination wants a path. Maybe -Destination "$server] would work, provided you've mapped the PSDrive by then.

    3. I'm not sure if your trick of putting the copy command into a scriptblock in $copy1 will work. Have you tried this already and found it to function correctly?

    Part of my uncertainty here is that your formatting is pretty tough to follow – you're not consistent with your use of the curly brackets and indentation, so it's making the code a bit difficult to unravel in my head :(. Sorry about that.

    by turbomcp at 2012-09-12 16:09:34

    [quote="DonJ"]Uh, no. Several problems.

    1. You're only creating ONE PSDrive. When you do so, $server isn't defined. So that won't work. You probably want that INSIDE your ForEach loop. I'd think you'd want to map the PSDrive after you test the connection, to help ensure that the drive mapping won't fail, no?

    2. You can't pass $destination, which is a PSDrive, to -Destination, I don't think. -Destination wants a path. Maybe -Destination "$server] would work, provided you've mapped the PSDrive by then.

    3. I'm not sure if your trick of putting the copy command into a scriptblock in $copy1 will work. Have you tried this already and found it to function correctly?

    Part of my uncertainty here is that your formatting is pretty tough to follow – you're not consistent with your use of the curly brackets and indentation, so it's making the code a bit difficult to unravel in my head :(. Sorry about that.[/quote]

    thanks
    sorry for my amature experience:(
    thats why its all over the place
    ill continue working on it tomorow and test by i have made modification according to what you said( and errors i got:))
    so right now im here:
    $servers = Get-Content "c:\test\Srvlist.txt"
    $files="c:\test\rollup"
    $cred = Get-Credential DOMAIN\Username
    foreach ($server in $servers)
    {
    if (test-connection $server -quiet)
    {$destination=new-Psdrive -name $server -PsProvider FileSystem -root \\$server\r$ -Credential $cred -force}
    {$copy1={Copy-Item $files -Destination $server: -force}
    For($i = 1; $i -le $copy1.count; $i++){Write-Progress -Activity "copying file to $server" -status "copying $Files to $server" -percentComplete ($i / $copy1.count*100)}
    Remove-PSDrive $server

    }

    by DonJ at 2012-09-12 16:31:41

    Ok. So, look, I want to be helpful to you here, but I don't want to see you heading in a bad direction. Let me just show you how I would do this. This writing-the-whole-script thing isn't something I can often do, but this is fairly concise.

    $VerbosePreference = 'Continue'
    $source = 'c:\test\rollup'
    $cred = Get-Credential DOMAIN\Username

    foreach ($server in (Get-Content c:\test\srvlist.txt)) {

    Write-Verbose "Working on $server"
    if (test-connection $server -quiet) {
    Write-Verbose "-- Mapping drive"
    New-PSDrive -name $server -PsProvider FileSystem -root "\\$server\r`$" -credential $cred

    Write-Verbose "-- Starting copy"
    Copy-Item $files -Destination "$($server):"

    Write-Verbose "-- Copy complete for $server; removing drive"
    Remove-PSDrive -Name $server
    } else {
    Write-Verbose "Couldn't verify connection to $server"
    }
    }

    You'll notice I left out the progress bar. I appreciate that you're after that, but it looks to me like your copy command is actually having to execute twice to do what you think that's doing. If you get the basic copy working, you can look at adding the progress bar back in. For more, the verbose output is more useful than the progress bar.

    Note that I haven't tested this. I'm just using what you already wrote, and correcting the things I see that I know are syntactically wrong.

    by turbomcp at 2012-09-13 07:19:05

    hey
    first of all thanks for all your help and ealing with the cluter of my intial script:)
    i tested this out and changed $source to $files so it matches.
    but got this error(goot thing you enabled verbose output):
    "The provider does not support the use of credentials. Perform the operation again without specifying credentials."
    turns out this is a known issue, the provider itself it doesnt support cred 🙂
    http://jeffwouters.nl/index.php/2011/11 ... edentials/

    now i am thinking maybe just upgrade to powershell 3.0 i think that now supports both(psdrive and copy-item with cred)

    by DonJ at 2012-09-13 07:21:34

    Ah, yeah, sorry – I've been working with v3 for long enough that I've forgotten v2 exists still :).

    by turbomcp at 2012-09-13 08:02:14

    i enjoy the learning experience
    in reality i can right click and use runas
    but i want to know how to properly do stuff:)

    anyhow i upgraded to 3.0
    and then tried this:
    $VerbosePreference = 'Continue'
    $files = 'c:\test\rollup'
    $cred = Get-Credential domain\user
    foreach ($server in (Get-Content c:\test\srvlist.txt)) {

    Write-Verbose "Working on $server"
    if (test-connection $server -quiet) {

    Write-Verbose "Working on $server -- Starting copy"
    Copy-Item $files -Destination "$($server):" -Credential $cred

    Write-Verbose "-- Copy complete for $server"

    } else {
    Write-Verbose "Couldn't verify connection to $server"
    }
    }

    but got this:)
    (again)
    The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again
    without specifying credentials.
    At C:\test\copyfiles.ps1:11 char:34
    + Copy-Item $files -Destination "$($server):" -Credential $cred
    + ~~~~~~~
    + CategoryInfo : NotImplemented: (:) [], PSNotSupportedException
    + FullyQualifiedErrorId : NotSupported

    so reverting back to psdrive and trying again:)

    by turbomcp at 2012-09-13 08:08:33

    ok so that worked fine now on powershell 3.0 🙂
    $VerbosePreference = 'Continue'
    $source = 'c:\test\rollup\'
    $cred = Get-Credential domain\user
    foreach ($server in (Get-Content c:\test\srvlist.txt)) {
    Write-Verbose "Working on $server"
    if (test-connection $server -quiet)
    { Write-Verbose "-- Mapping drive"
    New-PSDrive -name $server -PsProvider FileSystem -root "\\$server\r`$" -credential $cred
    Write-Verbose "-- Starting copy"
    Copy-Item $source\* -Destination "$($server):"
    Write-Verbose "-- Copy complete for $server; removing drive"
    Remove-PSDrive -Name $server
    } else { Write-Verbose "Couldn't verify connection to $server"
    }
    }

    so now just for fun i want to return that progress bar:)

You must be logged in to reply to this topic.