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 utility commands (1)

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

Share

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

Powershell has become a skill that windows operators have to master. His strength can only be realized after it has been used. Don't talk much about it. Record the practical commands of Powershell here to help the children's shoes who struggle together in Powershell.

Change the output to the desired object type, for example, I have a decimal, but I only want its integer digits, here we can use the-as operator, followed by as, the type you want to convert can be [int], [string], etc.

1.23-as [int]

This command converts the decimal 1.23 to an integer value, so the final result is 1, which is often used for some disk space or data conversions. the rounding mechanism used in this conversion, that is, if the decimal value is greater than or equal to 0.5, 1 will be added to the integer digits.

Of course, we don't use the as operator, it's easier to add a type such as [int] 1.23 directly before a value or variable, so that the output value is also one.

There are several ways to integrate this. We will also introduce it here. Sometimes we may need to count the decimal places directly as integers, or we may cancel the decimal places directly. In this case, we can perform them through [math]:: Floor () and [math]:: Ceiling (). For example:

PS C:\ Windows\ system32 > $a=2.33PS C:\ Windows\ system32 > [math]:: Ceiling ($a) 3PS C:\ Windows\ system32 > [math]:: Floor ($a) 2

From the above code, we can see that the ceiling function is rounded up, that is, all the decimal places are added to the integer and the function is rounded down, and all the decimal places are ignored, regardless of whether you are greater than 0.5.

There is another way for [int] to convert integers directly to [int] mentioned earlier, that is, [convert]:: Toint32 ($a), you can also get rounded rounding results. Of course, the function of Toint32 is not only to simply convert decimals, but also to convert binary, decimal, hexadecimal, etc., for example

PS C:\ Windows\ system32 > $a = [convert]:: ToString (188 Windows 2) PS C:\ Windows\ system32 > [convert]:: ToInt32 ($a) 10111100PS C:\ Windows\ system32 > [convert]:: ToInt32 ($aMagne2) 188PS C:\ Windows\ system32 > [convert]:: ToInt32 ($aMagne8) 2134592PS C:\ Windows\ system32 > [convert]: ToInt32 ($aMagne16) 269553920PS C:\ Windows\ system32 > [convert]: ToInt32 ($aMagne10) 10111100

First of all, I convert 188 to binary form to assign to the a variable (listen clearly to the form, it's actually a string value, to make sure you can directly use $a.gettype () to see his type), then test to convert directly to an integer, and then convert to binary, octal, hexadecimal and decimal, well, it looks fine, right, buddy.

OK, let's move on to a more practical command to convert the format of the input through a hash expression.

I think when you write a script to export data, you should encounter the name of the field of the exported result that you are not satisfied with. You want to change it to the name you want, no matter why, you just want to change it. What does the saying say? I just like the way you look at me and take me for granted. It's just so capricious.

OK, small case, hash expression to help you, to take a simple example, I want to change the input field English to the Chinese I want, look at the code:

PS C:\ Windows\ system32 > Get-Process | select name-First 3Name-AcroRd32 AcroRd32 ApplicationFrameHost

We use get-process to catch three process names, and we can find that the name field is Name. As a patriotic young man, I have to change this Name into Chinese. Look at my code.

PS C:\ Windows\ system32 > Get-Process | select @ {name=' name'; expression= {$_ .Name}}-First 3 name-AcroRd32 AcroRd32 ApplicationFrameHost

See, Name is gone, instead of Chinese characters, name and expression can be abbreviated to n and e @ {nasty 'desired name'; e = {it can be a code output value or a value}}

By extension, we know that when we use output format commands such as ft or fl, we do not specify the alignment function of the output value. Powershell will align by default, but we will find that there are some left alignment, some right alignment, and whether we can play well or not. why do we have to make two alignments? I only need one alignment, and we can solve it through hash expressions. Just add align='left' or 'right',' to the expression to align the data to the left or right, such as @ {nailed 'name' E = {$_ .name}; align='left'}

That's all for today. If you want to know more, let's hear the next decomposition. The meeting is over!

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