Solved

Powershell

Posted on 2013-05-20
17
1,233 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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 …
The viewer will learn how to dynamically set the form action using jQuery.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now