Solved

Powershell

Posted on 2013-05-20
17
1,287 Views
Last Modified: 2013-05-30
Hi Guys,

i have quite a few servers on my DMZ network, and i need to copy a file to all the servers.

They all have the same username and password.

Is there a way I can do this in powershell?

thank you in advance,
K
0
Comment
Question by:chgl
  • 7
  • 6
  • 4
17 Comments
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39180707
If this is something you will do often then I would make a .cmd file that copies the file to the other named servers, it would take the filename as the first parameter.

Something like:

; copyfile.cmd
; Usage copyfile <filename>
@echo off
cls

if '%1' == '' goto error

echo Copying File %1 to all other servers from DMZ00
echo.
pause

echo ---------------------------------------------
echo Copying %1 to DMZ01
copy \\DMZ00\D$\Files\%1 \\DMZ01\D$\Files\%1
Echo File Copied to DMZ01

echo ---------------------------------------------
echo Copying %1 to DMZ02
copy \\DMZ00\D$\Files\%1 \\DMZ02\D$\Files\%1
Echo File Copied to DMZ02

echo ---------------------------------------------
pause
goto end

:error
echo ERROR:- No input.  A filename is required on the command line to enable copying.
goto end

:end

Open in new window

0
 

Author Comment

by:chgl
ID: 39181034
Burundi this is a batch file script, I need the powershell equivalent
0
 
LVL 2

Expert Comment

by:Chris_Ryan81
ID: 39181171
This should work... it will read a text file of your servers (C:\servers.txt) you just need to edit the $file variable with the location of the file you want to copy and the line copy item with the path you want to file to end up in... so to copy the file to c:\users you would edit the line to :       copy-item $file \\$server\c$\users\


#Text list of servers
$servers = get-content c:\servers.txt
#Path to the file you would like to copy
$file = \\server\share\file.txt

foreach($Server in $Servers)
{
	copy-item $file \\$server\<path>
}

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:chgl
ID: 39181235
Hi Chris,

Your script makes sense, however the servers require authentication, it is not part of our domain, so how do i enter its credentials?

Also i want to create a variable called dmxserver, therefore $dmxservers, however each time I close powershell it dissappears, how can I make it appear each time i run powershell?

thanks you very much,
k
0
 
LVL 2

Expert Comment

by:Chris_Ryan81
ID: 39181877
For a permanent variable you would need to create a profile file.  More info for that can be found here. - http://technet.microsoft.com/en-us/library/ee692764.aspx

As for credentials, give me a little bit and I should be able to throw you something that works and won't require you to hard code a password into the script.
0
 
LVL 2

Expert Comment

by:Chris_Ryan81
ID: 39181913
Give this a shot - it should prompt a box to enter your credentials

#Text list of servers
$servers = get-content c:\servers.txt
#Path to the file you would like to copy
$file = "c:\test.txt"
Get-Credential
foreach($Server in $Servers)
{
	copy-item -path $file -Destination \\$server\c$
}

Open in new window


Edit: Changed script, tested and this works for me.
0
 

Author Comment

by:chgl
ID: 39183699
Thank you very much guys.
0
 

Author Comment

by:chgl
ID: 39184426
this script does not work, it gives me this error:

Copy-Item : The path is not of a legal form.
At line:3 char:14
+     copy-item <<<<  -path "$file" -Destination "\\$server\c$\"
    + CategoryInfo          : InvalidArgument: (\\10.100.0.06\c$\:String) [Copy-Item], ArgumentException
    + FullyQualifiedErrorId : CreateDirectoryArgumentError,Microsoft.PowerShell.Commands.CopyItemCommand
0
 
LVL 6

Accepted Solution

by:
BurundiLapp earned 500 total points
ID: 39184653
Does this do it?

#Text list of servers
$servers = get-content c:\servers.txt
#Path to the file you would like to copy
$file = "test.txt"
$fileFldr = "c:\"
Get-Credential
foreach($Server in $Servers)
{
	copy-item -path $fileFldr$ & file -Destination "\\" & $server & "\c$\" & $file
}

Open in new window

0
 

Author Comment

by:chgl
ID: 39184713
instead of a network path defined in $file, i defined a local filed, therefore $file="c:\testing123.txt"

however when i try to copy the file it gives me an username and password error:

Copy-Item : Logon failure: unknown user name or bad password.
At line:3 char:14
+     copy-item <<<<  -path $file -Destination \\$server\c$
    + CategoryInfo          : NotSpecified: (:) [Copy-Item], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.CopyItemCommand

I am 100% certain that the username and password is correct

however if i click on start > run \\10.100.5.120 > then enter the username and password and then run the script then it works,

please can someone send me a solution on how to fix this problem. I have quite a few files to copy to quite a few servers.

thanks
0
 
LVL 2

Expert Comment

by:Chris_Ryan81
ID: 39184769
Sort of a hack, but can you run the script on one DMZ server to copy the files to all the rest of the servers?

Admittedly, I do not have much experience with passing alternate credentials as all my servers are in one of two domains.
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39184882
0
 

Author Comment

by:chgl
ID: 39186976
the bits transfer doesnt work either, i have tried:

Import-Module bitstransfer
$cred = Get-Credential
$sourcePath = "c:\dsquery.txt"
$destPath = "\\10.100.0.06\c$"
Start-BitsTransfer -Source $sourcePath -Destination $destPath -Credential $cred

i then get this error message:Start-BitsTransfer : Cannot process argument because the value of argument "path" is null. Change the value of argume
 "path" to a non-null value.
At line:1 char:19
+ Start-BitsTransfer <<<<  -Source $sourcePath -Destination $destPath -Credential $cred
    + CategoryInfo          : InvalidArgument: (:) [Start-BitsTransfer], PSArgumentNullException
    + FullyQualifiedErrorId : ArgumentNull,Microsoft.BackgroundIntelligentTransfer.Management.NewBitsTransferCommand
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39187626
I've just tested the script above (had to do it using a domain machine as we only have ftp access to our DMZ servers) and it worked fine, no errors.

I'm using Powershell in Windows 7 which is being report as v2.0, if you run get-host which version are you running?  Maybe it's a Powershell version problem.
0
 

Author Comment

by:chgl
ID: 39188015
i am running version 2.0 on windows xp.
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39188040
Do you have a windows 7 machine you could test the script on to see if it is an issue with your overall environment or just that PC?
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39188050
Sorry - posted to the wrong thread.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question