Powershell script automatically downloads files in fixed format on the FTP server

Source: Internet
Author: User

How can I use a bat script to return the year, month, and day of yesterday? In fact, it is very troublesome to use bat to write a for to get the year, month, and day of the date, and then write a smallProgramTo calculate the year, month, and day of yesterday. In fact, to enable direct execution of. netCodeGood,. Net datetime. Now. adddays (-1) is yesterday

 

In fact, powershell will help you answer this question. You can use the rich functions supported by the. NET class library, such as operating XML, file IO, network, and other features. Of course, you can also write a class library to implement complicated scripts.

 

In this case, open powershell and input [datetime]: Now.

Returns the current date. If you want to see the methods and attributes of now

[Datetime]: Now | get-member

Of course, if you want to calculate how old you are, [datetime]: Now-[datetime] ("yourbirthday") will return your age. I usually use this to calculate my baby for several months.

 

Powershell allows you to define a variable for FTP download. You can first generate the file name you want to download, sort it into a string, and send it to FTP.

The following is a simple example.

$ Yersterday = [datetime]: Now. adddays (-1 );
$ Yyyy = $ yersterday. Year;
$ Mm = $ yersterday. month;
$ Day = $ yersterday. Day;
$ File1 = [String]: Format ("90193698 _ {0} {}_{ 0} {20..zip", $ yyyy, $ mm, $ Day );

$ Cmd = "Open yourftpserver
User Username Password

Binary
Get $ file1"

$ Cmd | FTP-I-n

Save it as a PS1 file. If you want it to run automatically, write a Windows schedule task "powershell fullpathoftheps1"

By default, poweshell has an execution policy that does not run the file that executes the signature. You can change it

Set-executionpolicy remotesigned

Or change the Registry directly.

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ powershell \ 1 \ shellids \ microsoft. powershell

Maybe this is a good example of PS.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.