• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1644
  • Last Modified:

powershell or batch command to copy folder using some credentials

Hi Friends,

Please assist me on -->

Powershell or batch script to copy a folder from location A to location B using domain/username & password.

locations are file server & network unc path so i need to provide credentials to be used.
0
Minesh Shah
Asked:
Minesh Shah
  • 4
  • 2
  • 2
  • +2
4 Solutions
 
oBdACommented:
Batch: just create an IPC connection providing user name and password:
net use \\<Servername or IP>\IPC$ "TopSecretPassword" /user:SomeDomain\SomeUser
robocopy "D:\Source" "\\<Servername or IP>\SomeShare\Source" *.* /e ...
net use \\<servername or IP>\IPC$ /delete

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
Assuming you can't run the script as a suitable domain user and therefore need to provide the credentials then at the simplest level you can use a net use command... main problem here is the plain text password.  If this works for you have a look at my document below about ways to hide the password a little and can add that into here for you if wanted.

@echo off
net use x: \\sourceserver\share /user:domain\username password
net use y: \\destserver\share /user:domain\username password
xcopy x:\sourcefolder\*.* Y:\destfolder /e /d /y

or robocopy /MIR to keep two areas in sync (deletes deleted files from dest too)

Steve
0
 
slidingfoxCommented:
Do you want to include the credentials in the script or do you want to be prompted for credentials each time the script run?

If you want to be prompted, try the following:

Copy-Item -Path <SourcePath> -Destination <Destination> -Credential (Get-Credential)

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Steve KnightIT ConsultancyCommented:
Crossed with oBdA there of course.

The hiding passwords I mentioned: http://scripts.dragon-it.co.uk/links/batch-password1

Once batch file has been written then do this from cmd prompt:

echo Password1>YourBatchFile.cmd:password1
echo Password2>YourBatchFile.cmd:password2

Then in the batch file do:

@echo off
setlocal

set /p password1=<%~nx0:password1
set /p password2=<%~nx0:password2

net use x: \\sourceserver\share /user:domain\username %password1%
net use y: \\destserver\share /user:domain\username %password2%
xcopy x:\sourcefolder\*.* Y:\destfolder /e /d /y

endlocal
0
 
chrismerrittCommented:
PowerShell has ways to obfuscate passwords assuming the script is run from the same box each time, however I would certainly consider instead granting the account you are using permissions on the share instead of messing around sending credentials each time, unless you have a good reason not to.
0
 
Steve KnightIT ConsultancyCommented:
Agreed there, though it sounded to me like it was an issue talking from a non-domain account to another domain or similar.
0
 
Minesh ShahSharePoint & all about itAuthor Commented:
worked for me:
net use \\serverpath\IPC$ "password" /user:domainname\username
robocopy "sourcedirectory" "destinationdirectory" /MIR
net use \\serverpath\IPC$ /delete

How can i get an email if this task fails?
I think this will solve all my problems before they come!

Any advise friends..
0
 
oBdACommented:
You can do that with blat.exe: http://sourceforge.net/projects/blat/
@echo off
setlocal
net use \\serverpath\IPC$ "password" /user:domainname\username
if errorlevel 1 (
	set EmailSubject=Error connecting to \\Server
	goto Email
)
robocopy "sourcedirectory" "destinationdirectory" /MIR
if errorlevel 4 (
	net use \\serverpath\IPC$ /delete
	set EmailSubject=Error during robocopy.
	goto Email
) else (
	net use \\serverpath\IPC$ /delete
)
goto :eof

:Email
set EmailServer=smtp.domain.com
set EmailFrom=from@domain.com
set EmailTo=to@domain.com
set EmailSubject=%~nx0: %EmailSubject%
REM add the following if authentication is required before an email can be sent: -u <Logon User> -pw "<Logon Password>"
blat.exe -server %EmailServer% -f %EmailFrom% -t %EmailTo% -s "%EmailSubject%" 

Open in new window

0
 
Steve KnightIT ConsultancyCommented:
OK do consider at least here storing domain password in plain text!

http://scripts.dragon-it.co.uk/links/email-from-batch

My page above shows a simple routine using the free "blat.exe" for sending emails, and also a small VBS script in the attachments there.

The VBS approach is roughly as follows.  Setup a file "sendmail.vbs" alongside the batch file.  Then in the batch file do this.  Have added a few lines so it asks for a password the first time then stores it obscured from general viewer as suggested before.

Steve

@Echo off
set error=
set /p password=<%~nx0:password
if "%password%"=="" set /p password=Please enter password to store: 
(echo !password!)>%~nx0:password

net use \\serverpath\IPC$ "%password%" /user:domainname\username
if errorlevel 1 set error=NET USE %errorlevel%

robocopy "sourcedirectory" "destinationdirectory" /MIR
if errorlevel 1 set error=%error% COPY %errorlevel%

net use \\serverpath\IPC$ /delete

if not "%error%"=="" cscript //nologo sendmail.vbs "ERROR - %error%"

Open in new window



Const MailServer = "12.34.56.78" ' Mail Server to use for SMTP
Const MailServerPort = "25" ' SMTP Port used at Mail server (25 is default)
Const MailTo = "logs@somedomain.com" ' Who should be notified
Const MailFrom = "logs@somedomain.com" 

If WScript.Arguments.Count = 1 then 
   subject =  WScript.Arguments.Item(0)
else
   subject = "No subject passed in"
end if

body = "Message to add in body here"

'Send mail to administrator
SendMail MailFrom , MailTo , subject , body, MailServer , MailServerPort 

Sub SendMail(Sender, Recipient, Subject, Message,Server,Port)
  Set objEmail = CreateObject("CDO.Message")
  objEmail.From = Sender
  objEmail.To = Recipient
  objEmail.Subject = Subject 
  objEmail.Textbody = Message
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = Server
  objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = Port
  objEmail.Configuration.Fields.Update()
  objEmail.Send
End Sub

Open in new window

0
 
Minesh ShahSharePoint & all about itAuthor Commented:
ok
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 4
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now