Solved

Power shell scripting

Posted on 2014-01-03
10
384 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 38

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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 68

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 68

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now