Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell

Posted on 2013-05-20
17
Medium Priority
?
1,473 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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 

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 2000 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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

971 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