Solved

Powershell

Posted on 2013-05-20
17
1,382 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

623 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