Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Power shell scripting

Posted on 2014-01-03
10
Medium Priority
?
413 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 44

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Loops Section Overview

824 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