Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Powershell

Posted on 2013-05-20
17
Medium Priority
?
1,427 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Python: Series & Data Frames With Pandas

Learn the basics of Python’s pandas library of series & data frames and how we can use these tools for data manipulation.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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