Solved

Power shell scripting

Posted on 2014-01-03
10
400 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
[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
  • 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 41

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 70

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 70

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

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!

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
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 …
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 antispam), the admini…

738 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