Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PowerShell pipes and parentheses-PowerShell three minutes (VI)

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

This issue mainly introduces the connection between multiple commands-pipes.

PowerShell connects commands to each other through pipes and transmits the first command as input to the second command Cmdlet to run jointly

The application of pipe characters can greatly improve efficiency, and commands that need to be executed multiple times can be completed in one line.

Take a chestnut.

Now you need to query all domain accounts at the beginning of zhangs and disable

So we can run it like this.

Get-ADUser-Filter {SamAccountName-like "zhangs*"} | Set-ADUser-Enabled $False

Of course, multiple pipes can be used at the same time, assuming that your account exists in a text, you can write like this

Get-Content C:\ 1.txt | Get-ADUser-Filter {Enabled-eq $True} | Set-ADUser-Enabled $False

However, the more pipes, the better. Too many pipes will reduce the execution efficiency. Please pay attention to it here.

As can be seen from the above example, AD users can be queried first through the pipeline, and then AD users can be set up.

But what happens if two unrelated data are executed in this way?

Real-time proof that it cannot be executed successfully

So, how on earth does PowerShell transfer data to the pipeline?

In the following example, we call the first command command A, which produces some results. The second command is command B, which receives the result set generated by command An and then completes its work.

Get-Content C:\ Computers.txt | Get-Service

When you run Get-Content, it puts the computer name in the text file into the pipe. PowerShell then decides how to pass the data to the Get-Service command. However, PowerShell can only use a single parameter at a time to receive incoming data.

That is, PowerShell must decide which parameter of Get-Service will receive the result produced by Get-Content. This decision is called pipe parameter binding.

There are two ways for PowerShell to bind pipeline parameters:

ByValue

When you use ByValue to bind pipe parameters, PowerShell confirms the type of data object generated by command A, and then sees which parameter in command B accepts the type of object passed through the pipe.

You will see that the result object of the Get-Content command is String. Through the query help, you can see that there is indeed a parameter in Get-Service that can receive String type data from the ByValue pipe-Name. You may have found a problem: this is not what we need-- the content in our text file, that is, the String object, refers to the name of the computer, not the name of the service, so it certainly cannot be executed.

ByPropertyName

This scheme also passes the output of command A to the parameters of command B, but ByPropertyName is slightly different from ByValue: multiple parameters of command B can be used at the same time.

The implementation of this function is actually very simple: just find the attribute name that matches the parameter name.

However, it will be more difficult if the attribute output of command An is not aligned with the parameter name of command B. At this time, you need to use custom attributes to solve the problem.

Sometimes, no matter how hard we try, we can't handle the output of the pipeline. Like Get-WmiObject.

Get-Content.\ Computers.txt | Get-WmiObject-Class Win32_BIOS

This parameter does not receive the computer name from the pipe, so how do we pass data from other sources to the command?

At this point, you can use parentheses to give priority to the command in parentheses, and then pass the result as a parameter.

Get-WmiObject-Class Win32_BIOS-ComputerName (Get-Content.\ Computers.txt)

All right, after the introduction of the basic pipes and parentheses, everyone quickly optimize the command and try to complete the execution within one line.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report