copy with alternate credentials

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<br>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.count100)}
}

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<br>$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$&quot; -credential $cred<br><br> Write-Verbose &quot;-- Starting copy&quot;<br> Copy-Item $files -Destination &quot;$&#40;$server&#41;:&quot;<br><br> Write-Verbose &quot;-- Copy complete for $server; removing drive&quot;<br> Remove-PSDrive -Name $server<br> } else {<br> Write-Verbose &quot;Couldn&#39;t verify connection to $server&quot;<br> }<br>}</code><br><br>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.<br><br>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.</blockquote>by turbomcp at 2012-09-13 07:19:05<blockquote>hey<br>first of all thanks for all your help and ealing with the cluter of my intial script&#058;)<br>i tested this out and changed $source to $files so it matches.<br>but got this error(goot thing you enabled verbose output):<br>&quot;The provider does not support the use of credentials. Perform the operation again without specifying credentials.&quot;<br>turns out this is a known issue, the provider itself it doesnt support cred :)<br><!-- m --><a class="postlink" href="http://jeffwouters.nl/index.php/2011/11/a-gotcha-with-new-psdrive-combined-with-credentials/">http://jeffwouters.nl/index.php/2011/11 ... edentials/</a><!-- m --><br><br>now i am thinking maybe just upgrade to powershell 3.0 i think that now supports both(psdrive and copy-item with cred)</blockquote>by DonJ at 2012-09-13 07:21:34<blockquote>Ah, yeah, sorry - I've been working with v3 for long enough that I've forgotten v2 exists still :).</blockquote>by turbomcp at 2012-09-13 08:02:14<blockquote>i enjoy the learning experience <br>in reality i can right click and use runas<br>but i want to know how to properly do stuff:)<br><br>anyhow i upgraded to 3.0<br>and then tried this:<br><code>$VerbosePreference = &#39;Continue&#39;<br>$files = &#39;c:\test\rollup&#39;<br>$cred = Get-Credential domain\user<br>foreach &#40;$server in &#40;Get-Content c:\test\srvlist.txt&#41;&#41; {<br><br>Write-Verbose &quot;Working on $server&quot;<br>if &#40;test-connection $server -quiet&#41; {<br><br><br>Write-Verbose &quot;Working on $server -- Starting copy&quot; <br>Copy-Item $files -Destination &quot;$&#40;$server&#41;:&quot; -Credential $cred <br><br>Write-Verbose &quot;-- Copy complete for $server&quot; <br><br> } else {<br>Write-Verbose &quot;Couldn&#39;t verify connection to $server&quot; <br> }<br>}</code><br><br>but got this:)<br>(again)<br>The FileSystem provider supports credentials only on the New-PSDrive cmdlet. Perform the operation again <br>without specifying credentials.<br>At C:\test\copyfiles.ps1:11 char:34<br>+ Copy-Item $files -Destination &quot;$($server):&quot; -Credential $cred<br>+ ~~~~~~~<br> + CategoryInfo : NotImplemented: (:) [], PSNotSupportedException<br> + FullyQualifiedErrorId : NotSupported<br><br>so reverting back to psdrive and trying again:)</blockquote>by turbomcp at 2012-09-13 08:08:33<blockquote>ok so that worked fine now on powershell 3.0 :)<br><code>$VerbosePreference = &#39;Continue&#39;<br>$source = &#39;c:\test\rollup\&#39;<br>$cred = Get-Credential domain\user<br>foreach &#40;$server in &#40;Get-Content c:\test\srvlist.txt&#41;&#41; {<br> Write-Verbose &quot;Working on $server&quot;<br> if &#40;test-connection $server -quiet&#41;<br> { Write-Verbose &quot;-- Mapping drive&quot; <br> New-PSDrive -name $server -PsProvider FileSystem -root &quot;\\$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:)