PowerShell Getting Started tutorials the method of calling each other with the PowerShell command _powershell

Source: Internet
Author: User

Using a single script to accomplish a task is ideal, and the reality is often that you have to combine multiple scripts or programs to accomplish a task because of historical reasons, or a team composition. The scope of this article is the combination and invocation of the cmd command and the PowerShell command.

There is no doubt that the call between the cmd command and the PowerShell command has two directions. That is, call the PowerShell command in the cmd command, and invoke the cmd command in the PowerShell script. What needs to be explained here is that the call here is divided into two levels: one, simply execute another command in one of the environments, complete part of the work; second, call another command in one of the execution contexts, and select the following logic based on the return result.

Invoke the PowerShell command in the cmd command

It is very simple to invoke the PowerShell command only in the cmd command without having to record the PowerShell return result. You can execute a similar command directly in the cmd command or script:

Copy Code code as follows:

PowerShell echo "Hello"

When executing the above command, CMD executes the PowerShell as an application.

There are not many possible ways to invoke the PowerShell command in the cmd command and get the return value. This is because the cmd command for the processing of results often requires the help of file read and write, very few opportunities to pass data between the commands, and save the intermediate results. The for command is one of the best methods, and it points to the following methods:

Copy Code code as follows:

For/f%i in (' PowerShell echo ' Hello ') do (set a=%i)

The result of the above command is that%a% is assigned to hello. Of course, if it's just a simple assignment, there's no need for such a big fee. Calling PowerShell's command, it must be fancy for its powerful programming capabilities, such as custom sorting, and so on.

A practical example I have encountered is this: there is a directory on the file server with some names of the directories in the year. Because the directory structure has been identified, and many groups are in use, can not be arbitrarily changed. One of the tasks we have is to regularly go to the latest month and year catalogue to copy builds. The trouble is, first, the folder in accordance with 12.1, 12.2 ... 12.10, 12.11, 12.12, 13.1 ... So named; second, from time, because the latest catalog is not at the end of the month, but one day in the month. With cmd command Limited programming ability, certainly is not good to solve this problem. I finally wrote a PowerShell script file, called in the cmd script, and finally solved the problem.

To invoke the cmd command in the PowerShell command

Because of PowerShell's powerful piping capabilities, it is easy to invoke the cmd command in the PowerShell command or script and get the return result, and whether the results will not affect the way the cmd command is invoked. Take a look at the following example:

Copy Code code as follows:

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

In the preceding example, the result of the 1th row is the same as the execution result of the second line. The difference is that the results of the latter are saved, and the former only prints the results on the screen.

Why do I need to invoke the cmd command in PowerShell (since PowerShell has such a powerful shell and programming features)? In fact, the reason is also very simple. We had a lot of handy cmd commands, tools, and console programs before PowerShell, and if they were completely discarded and rewritten using PowerShell, that would be a waste. You know, it's our goal to be able to use the smallest changes to accomplish tasks efficiently.

Conclusion

A new language, if not compatible with the existing resources and communication, then it is doomed to not be the public choice (perhaps in the minority popular), will eventually be abandoned and forgotten. PowerShell and the existing system of good communication with the obvious, from its relationship with the cmd command can be seen.

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.