PowerShell entry (5): Interaction Between Cmd and PowerShell commands

Source: Internet
Author: User

It is ideal to use a script to complete a task. The reality is that, due to historical reasons or team composition, You have to combine multiple scripts or programs, to complete a task. The scope of this article is the combination and call between the Cmd command and the PowerShell command.

Undoubtedly, there are two ways to call between the Cmd command and the PowerShell command. That is, you can call the PowerShell command in the Cmd command and the Cmd command in the PowerShell script. It should be noted that the calls here are divided into two levels: 1. simply execute another command in one of the environments to complete some work; 2. Call another command in the execution context of one of them and select the following logic based on the returned result.

Call the PowerShell command in the Cmd command

It is very simple to call the PowerShell command in the Cmd command without recording the return results of PowerShell. You can directly execute a command similar to the following in the Cmd command or script:

powershell echo "Hello"

When executing the preceding command, Cmd runs PowerShell as an application.

There are not many feasible methods to call the PowerShell command in the Cmd command and obtain the returned value. This is because the Cmd command often needs to read and write the results by means of files. It rarely has the opportunity to pass data between commands and save the intermediate results. The For command is one of the best methods. Its method is as follows:

for /f  %i in ('powershell echo "Hello"') do (set a=%i)

The execution result of the preceding command is % a %, which is assigned as Hello. Of course, if it's just a simple assignment, there's no need for such a huge discount. To call PowerShell commands, you must have taken a fancy to its powerful programming capabilities, such as custom sorting.

An actual example is as follows: the file server has a directory named by year and month. This is because the directory structure has been determined and many groups are in use and cannot be changed at will. One task is to copy the build in the latest year and month directory on a regular basis every day. The trouble is that, first, the folders are as follows: 12.1, 12.2... 12.10, 12.11, 12.12, 13.1... This is the name. Second, it cannot be found from the time, because the latest directory is generated not at the end of the month, but on a certain day of the month. With the limited programming capability of Cmd commands, it is certainly difficult to solve this problem. I finally wrote a PowerShell script file and called it in the Cmd script to finally solve this problem.

Call the Cmd command in PowerShell

Due to the powerful Pipeline Function of PowerShell, it is easy to call the Cmd command in PowerShell commands or scripts and obtain the returned results. Whether to obtain the results does not affect the way to call the Cmd command. See the following example:

cmd /c dir$a = (cmd /c dir)$a

In the preceding example, the execution result of row 1st is the same as that of the last two rows. The difference is that the execution results of the latter are saved, and the former only prints the results on the screen.

Why do we need to call the Cmd command in PowerShell? Since PowerShell has such powerful Shell and programming features )? In fact, the reason is also very simple. Before PowerShell, we had a lot of useful Cmd commands, tools, and console programs. It would be a waste if they were completely discarded and rewritten using PowerShell. We need to know that it is our goal to efficiently complete tasks with minimal changes.

Conclusion

If a new language cannot be compatible with and communicate with existing resources, it is doomed that it cannot become a public choice and may become popular among the masses.) It will eventually be abandoned and forgotten. Good communication between PowerShell and the existing system is obvious, as can be seen from its relationship with Cmd commands.

Related Article

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.