Solved

Power shell scripting

Posted on 2014-01-03
10
393 Views
Last Modified: 2014-01-04
Hi ,

I am doing a task manually.

I need a powershell to  zip file from the path
"c:\users\users\test.mdb file and then ftp the file to a linux server.
I have the server name, port , username and password.
Zip file need to have the current date and time for conventions.

Please help

Thanks,
Magento
0
Comment
Question by:magento
  • 5
  • 2
  • 2
  • +1
10 Comments
 
LVL 6

Expert Comment

by:r3nder
ID: 39755620
If you head on over to CodePlex and grab the PowerShell Community Extensions, you can use their write-zip cmdlet. also you can get the project called Alex FTPS Client that will help  you with the FTP portion
0
 
LVL 40

Expert Comment

by:Vasil Michev (MVP)
ID: 39755916
just call the cmdline version of winzip/winrar/whichever utility you are using, you can use get-date to generate the file name
0
 
LVL 5

Author Comment

by:magento
ID: 39755992
Hi ,

I have tried the batch scripting and came so far .

Zip is working fine, but when i use ftp along then it keeps on running until i stop the script via cmd line.

@echo off
set path="C:\Program Files (x86)\WinRAR\";%path%
SET file=c:\2013_.txt
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
SET name=ftp_%mydate%_%mytime%.zip
rar a C:\%name% %file%
ftp -i -s:ftp.txt

Open in new window

***************************************
Content of FTP.txt

open test.com
support@test.com
password
lcd /D "C:\"
cd  var/tmp
binary
put 2013_.txt
bye

Can you kindly advice what is wrong with them?

Thanks
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 6

Expert Comment

by:r3nder
ID: 39756122
have you tried quit or bye after the ftp executes
ftp -i -s:ftp.txt
quit
0
 
LVL 5

Author Comment

by:magento
ID: 39756168
Tried that , but still it keeps on running...

C:\Expert-Exchange\DOS>ftp.bat

RAR 3.71   Copyright (c) 1993-2007 Alexander Roshal   20 Sep 2007
Shareware version         Type RAR -? for help

Evaluation copy. Please register.

Creating archive C:\ftp_2014-01-04_2118.zip

Adding    C:\Expert-Exchange\DOS\2013_.txt                            OK
Done

RAR 3.71   Copyright (c) 1993-2007 Alexander Roshal   20 Sep 2007
Shareware version         Type RAR -? for help

Evaluation copy. Please register.

Updating archive C:\ftp_2014-01-04_2118.zip

Updating  C:\Expert-Exchange\DOS\2013_.txt                            OK
Done

Open in new window

0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39756250
You've called your batch like the application, ftp. Since .cmd and .bat is searched before .exe, you will call your own script instead of FTP. Choose a different name for your batch, or change the call in the batch to ftp.exe.

Also note that you are currently sending the uncompressed file instead of the ZIP. However, the ZIP file name changes, and so you won't be able to use a static ftp script, unless using wildcards and mput.
0
 
LVL 5

Author Comment

by:magento
ID: 39756281
Hi ,

I used the sample file in ftp.txt because i dont know how to pass the value of the zipped file there . Please advice.

I will check ur advice above now.

Thanks
0
 
LVL 5

Author Comment

by:magento
ID: 39756297
Hi Qlemo,

Your advice worked like charm .

Now both zip and ftp working .

But i see 2 issues .

1. I am not able to ftp the zipped file ( Tried put %name%) but its not working.

2. Instead of below, can you give easy dos command to get the datetime (eg: ddmmyyyy_hhmm)

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

Thanks,
Magento
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39756721
There are other ways, but none is "easy", to get a formatted date/time string. However, the second FOR is useless, the third is overwriting the mytime variable, and so the "correct" lines are:
For /f "tokens=2-4 delims=/ " %%a in ("%date%") do set mydate=%%c-%%a-%%b
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do set mytime=%%a%%b

Open in new window

but I prefer a more direct way
set mydate=%date:~-4%-%date:~-10,2%-%date:~-7,2%
set mytime=%time:~0,2%%time:~3,2%

Open in new window

In regard of providing the correct file name to FTP: as said, you can use mput with wildcards (transferring all matching files). Or the FTP script is generated dynamically. But you can't use any variables inside FTP - the MS FTP client just does not support dynamic scripting in any way.
My suggestion is to generate the FTP script:
@echo off
set path=C:\Program Files (x86)\WinRAR\;%path%
set mydate=%date:~-4%-%date:~-10,2%-%date:~-7,2%
set mytime=%time:~0,2%%time:~3,2%
set name=C:\ftp_%mydate%_%mytime%.zip
set file=c:\2013_.txt

rar a %name% %file%
( echo support@test.com
  echo password
  echo cd  var/tmp
  echo binary
  echo put %name%
  echo bye
) | ftp -i test.com

Open in new window

0
 
LVL 5

Author Closing Comment

by:magento
ID: 39756991
This solves both the issue. Thanks again.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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