PowerShell教程 - 编程结构(Program Struct)- 第五部分
2022/8/22 5:24:10
本文主要是介绍PowerShell教程 - 编程结构(Program Struct)- 第五部分,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
更新记录
转载请注明出处。
2022年8月21日 发布。
2022年8月18日 从笔记迁移到博客。
枚举类型
定义枚举类型(Defining an enumeration)
简单的定义
enum MyEnum { Zero One }
设置明确的值
enum Direction { Up = 1 Down = 2 Left = 3 Right = 4 }
还可以单个值对应多个名称
enum MyEnum { One = 1 First = 1 Two = 2 Second = 2 }
还可以自定义底层的类型
enum MyEnum : UInt64 { First = 0 Last = 18446744073709551615 }
获得枚举的类型
[Direction].GetEnumUnderlyingType()
还可以转为底层的类型
[Int][MyEnum]::First
Flag特性(flags attribute)
就是C#中Flag特性
定义枚举
[Flags()] enum MyEnum { First = 1 # 001 Second = 2 # 010 Third = 4 # 100 }
使用枚举
[Int][MyEnum]'First,Second' [MyEnum]6 # Second, Third
类类型
创建类类型(Creating a class)
class MyClass { [String]$Property = 'Value' }
实例化类类型(Create Instance)
[MyClass]: :new()
定义属性成员(Property)
class MyClass { [String]$Property = 'Value' }
定义类的构造函数(Constructors)
class MyClass { [String]$Property MyClass() { $this.Property = 'Hello world' } MyClass([String]$greeting) { $this.Property = $greeting } }
定义方法成员(Methods)
class MyClass { [String]$Property MyClass() { $this.Property = 'Hello world' } [String] ToString() { return $this.Property } }
方法还可以进行重载
class MyClass { [String]$Property = 'Hello world' [String] ToString() { return '{0} on {1}' -f $this.Property,(GetDate).ToShortDateString() } [String] ToString($format) { return '{0} on {1}' -f $this.Property,(Get-Date).ToString($format) } }
类间的继承(Inheritance)
class MyBaseClass { [String]$BaseProperty = 'baseValue' } class MyClass : MyBaseClass { [String]$Property = 'Value' }
还可以覆盖子类的方法
注意:不需要override修饰
class MyBaseClass { [String] ToString() { return 'default' } } class MyClass : MyBaseClass { [String] ToString() { return 'new' } }
构造函数继承(Constructor inheritance)
#父类 class MyBaseClass { [String]$BaseProperty MyBaseClass() { Write-Host 'Executing base constructor' $this.BaseProperty = 'baseValue' } } #子类 class MyClass : MyBaseClass { [String]$Property MyClass() : base() { Write-Host 'Executing child constructor' $this.Property = 'value' } }
链式构造函数调用(Chaining constructors)
将通用的初始化代码放入到一个公用的方法中即可
class MyClass { [String]$FirstProperty [String]$SecondProperty MyClass() { $this.Initialize() } MyClass([String]$First) { $this.Initialize($First) } MyClass([String]$First, [String]$Second) { $this.Initialize($First,$Second) } [Void] Initialize() { $this.Initialize('DefaultFirst','DefaultSecond') } [Void] Initialize([String]$First) { $this.Initialize($First,'DefaultSecond') } [Void] Initialize([String]$First, [String]$Second) { $this.FirstProperty = $First $this.SecondProperty = $Second } }
静态修饰符(Static modifier)
class MyClass { static [String] $Property = 'Property value' static [String] Method() { return 'Method return' } }
正则表达式(Regular Expressions)
正则表达式规则基础(Regex basics)
字面量字符(Literal characters)
就是普通的字符串
实例:
'The first rule of regex club' -match 'regex' '9*8' -match '\*' # * is reserved '1+5' -match '\+' # + is reserved
任意字符(Any character (.))
实例:
'abcdef' -match '......' 'abcdefghijkl' -match '......'
字符重复(Repetition with * and +)
实例:
'aaabc' -match 'a*' # Returns true, matches 'aaa' 'bcd' -match 'a*' # Returns true, matches nothing 'aaabc' -match 'a+'# Returns true, matches 'aaa' 'bcd' -match 'a+' # Returns false 'Anything' -match '.*' # 0 or more. Returns true '' -match '.*' # 0 or more. Returns true 'Anything' -match '.+' # 1 or more. Returns true
转义字符(The escape character ())
实例:
'domain\user' -match 'domain\\user' 'domain\user' -match '.*\\.*' '1 * 3' -match '\*' 'domain\user' -replace 'domain\\', 'newdomain\' 'domain\user' -match 'domain\.+' 'domain.user' -match 'domain\.+' 'Domain\User' -replace '.+\\' # Everything up to and including \ 'Domain\User' -replace '\\.+' # Everything including and after \
可选字符(Optional characters)
实例:
'Panda' -match 'P?'
不可见字符(Non-printable characters)
Tab \t Line feed \n Carriage return \r
锚点(Anchors)
实例:
$env:PATH -split ';' | Where-Object { $_ -match '^C' } #retrieving everything that starts with the C drive 'The first day is Monday' -replace '\bday\b', 'night' 'Monday is the first day' -replace '\bday\b', 'night'
重复(Repetition)
实例:
字符多选一(Character classes)
格式:
[]
可以在[]中放入以下内容:
-: Used to define a range \: Escape character ^: Negates the character class
实例:
'get' -match 'g[aeiou]t' 'got' -match 'g[aeiou]' '1st place' -match '[0-9]+' # $matches[0] is "1" '23rd place' -match '[0-9]+' # $matches[0] is "23" '"#' -match '[!-9]+'; '0xAF9B7' -match '0x[0-9a-f]+'; $matches[0] (ipconfig) -match 'IPv4 地址.+: *[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' 'one-two_three,four' -split '[-_,]' 'one-two_three,four' -split '[_,-]' 'one-two_three,four' -split '[_\-,]' 'Ba%by8 a12315tthe1231 k#.,154eyboard' -replace '[^a-z ]'
速记字符类(Shorthand character classes)
二选一(Alternation)
使用|即可
实例:
'one', 'two', 'three' | Where-Object { $_ -match 'one|three' } 'one', 'one hundred', 'three', 'eighty three' | Where-Object { $_ -match '^one$|^three$' }
分组(Grouping)
分组的作用:
表示重复(不止一个字符)
将替换限制为正则表达式的一部分
捕捉值
语法:
使用()即可
实例:
替换重复的部分
[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+
可以替换为
([0-9]+\.){3}[0-9]+
捕获值
'first second third' -match '(first) (second) (third)'
表示前面的值
'first second third' -replace '(first) ((second) (third))', '$1, $4, $2'
给分组命名(Named capture groups)
语法:
(?<GroupName>Expression) 'first second third' -match '(?<One>first) (?<Two>second) (?<Three>third)'
跳过分组的捕获(Non-capturing groups)
语法:
使用?:就可以了
'first second third' -match '(?<One>first) (?<Two>second) (?:third)'
获得匹配结果
$matches数组
常用正则表达式调试工具和网站
https://www.debuggex.com/
http://regexhero.net/tester/
http://www.regexplanet.com/advanced/dotnet/index.html#
http://www.regular-expressions.info/
注释(Note)
# 单行注释 <##> 多行注释
实例:
<# note note #>
异步编程(Asynchronous Processing)
Working with jobs
The job commands provide a means of executing code asynchronously by creating a new PowerShell process for each job
As each job executes within a new process, data cannot be shared between jobs
开启Job
Start-Job
When Start-Job executed, System.Management.Automation.PSRemotingJob is created
实例:
开启Job并指定为代码块
Start-Job -ScriptBlock { Start-Sleep -Seconds 1 }
设置使用32位版本的PowerShell来执行
Start-Job -ScriptBlock { Get-ChildItem } -RunAs32
获得Job
Get-Job
实例:
移除Job
Remove-Job
实例:
接收Job
Receive-Job
常用于接收Job的结果
实例:
接收指定Id的Job的返回内容
Receive-Job -Id 9
与事件交互(Reacting to events)
实例:
获得进程对象的事件成员
Get-Process | Get-Member -MemberType Event
获得FileSystemWatcher对象的事件成员
$fileSystemWatcher = New-Object System.IO.FileSystemWatcher; $fileSystemWatcher | Get-Member -MemberType Event
管道(Pipelines)
管道说明
The pipeline is one of the most prominent features of PowerShell
PowerShell通过管道(pipeline)把命令互相连接起来
管道通过传输一个命令,把其输出作为另外一个Cmdlet的输入
使得第二个命令可以通过第一个的结果作为输入并联合起来运行
The pipeline is used to send output from one command (standard out or Stdout) into another command(standard in or Stdin)
标准输出(Standard output)
Each of the streams in PowerShell has a number associated with it
These are shown in the following table
非标准输出(Non-standard output)
In PowerShell there are other output streams,each of these has a stream of its own
error
Write-Error
information (introduced in PowerShell 5)
Write-Information
warning
Write-Warning
verbose
Write-Verbose
In PowerShell 5 and later, Write-Host command sends output to the information stream
Write-Host
管道重定向
重定向到文件
"Panda666.com" > "D:/test.txt"
警告输出流重定向到文件
function Test-Redirect{ Write-Warning "Warning $i" } Test-Redirect 3> 'warnings.txt' # Overwrite
重定向所有流
$verbosePreference = 'continue' function Test-Redirect { 'This is standard out' Write-Information 'This is information' Write-Host 'This is information as well' Write-Error 'This is an error' Write-Verbose 'This is verbose' Write-Warning 'This is a warning' } Test-Redirect *> 'alloutput.txt'
重定向流到标准输出(Redirecting streams to standard output)
使用N>&1的语法即可
使用*>&1的语法将所有流重定向到标准输出
实例:
将Error流和Warning流重定向到标准输出
function Test-Redirect { 'This is standard out' Write-Error 'This is an error' Write-Warning 'This is a warning' } $stdOut = Test-Redirect 2>&1 3>&1
将所有流重定向到标准输出
$verbosePreference = 'continue' function Test-Redirect { 'This is standard out' Write-Information 'This is information' Write-Host 'This is information as well' Write-Error 'This is an error' Write-Verbose 'This is verbose' Write-Warning 'This is a warning' } $stdOut = Test-Redirect *>&1
重定向到Null(Redirection to null)
Get-Process > $null Get-Process > '' .\somecommand.exe 2> $null 3> $null .\somecommand.exe *> $null
管道追加
标准输出追加到文件
"Panda666.com" >> "D:/test.txt"
警告输出追加道文件
function Test-Redirect{ Write-Warning "Warning $i" } Test-Redirect 3>> 'warnings.txt' # Append
与.NET集成工作(Working with .NET)
说明
PowerShell is written in and built on the .NET Framework
Much of the .NET Framework can be used directly
程序集(Assemblies)
.NET Framework程序集默认存放位置
%SystemRoot%\Assembly
查看当前已经加载的程序集
[System.AppDomain]: :CurrentDomain.GetAssemblies()
注意:Once an assembly, and the types it contains, has been loaded into a session, it can't be unloaded without completely restarting the session
获得指定程序集的信息
注意:程序集必须已经加载了
[System.Management.Automation.PowerShell].Assembly [System.Management.Automation.PSCredential].Assembly [System.Management.Automation.PSObject].Assembly
命名空间(Namespaces)
In PowerShell, the system namespace is implicit
类型(Types)
In PowerShell, types are written between square brackets
比如:
[System.AppDomain] [System.Management.Automation.PowerShell] [System.Text.StringBuilder]
类(Classes)
非公开类型(Non-public classes)
Private and internal (non-public) classes are not directly accessible
.NET classes come with a number of access modifiers
Instances of a public class may be created using New-Object
Private and internal (non-public) classes are not directly accessible
using keyword
The using keyword does a number of different things
It can import and declare the following:
Assemblies
Modules
Namespaces
引入命名空间(Using Namespaces)
引入命名空间
using namespace System.Net.NetworkInformation
定义命名空间别名
using namespace NetInfo = System.Net.NetworkInformation
注意:在使用命名空间前记得引入程序集
引入程序集引入命名空间并实例化
# Load the Windows Presentation Framework using assembly PresentationFramework # Use the System.Windows namespace using namespace System.Windows $window = [Window]@{ Height = 100 Width = 150 } # Create a System.Windows.Controls.Button object $button = [Controls.Button]@{ Content = 'Close' } $button.Add_Click( { $window.Close() } ) $window.Content = $button $window.ShowDialog()
引入程序集(Using assemblies)
直接引入
using assembly System.Windows.Forms
引入明确版本的程序集
using assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral,PublicKeyToken=b77a5c561934e089'
使用Add-Type也可以实现引入命名空间
Add-Type -AssemblyName System.Windows.Forms
加载GAC中的程序集
Get-GacAssembly System.Windows.Forms
将模块安装到GAC中 Install-Module Gac -Scope CurrentUser
引入指定程序集文件
using assembly 'C:\SomeDir\someAssembly.dll'
构造函数(Constructors)
显示类的构造函数信息
[System.Text.StringBuilder]: :new
调用构造函数(Calling constructors)
首先推荐的使用new方法,这是主推创建对象的方式
$stringBuilder = [System.Text.StringBuilder]::new()
也可以使用下面这种语法
$stringBuilder = New-Object System.Text.StringBuilder
或者在命令中显式定义参数
New-Object -TypeName System.Text.StringBuilder -ArgumentList 10
或者使用C#类似的语法
$stringBuilder = New-Object System.Text.StringBuilder('Hello world')
或者将参数抽出来作为数组(Arguments as an array)
$params = @{ TypeName = 'System.Text.StringBuilder' ArgumentList = 'Initial value', 50 } $stringBuilder = New-Object @params $stringBuilder = New-Object System.Text.StringBuilder($argumentList)
属性和方法(Properties and methods)
获得类型的静态属性
[System.Text.Encoding] | Get-Member -MemberType Property -Static
使用静态属性
[<TypeName>]: :<PropertyName
实例:
直接使用
[System.Text.Encoding]: :ASCII
通过变量做中转
$type = [System.Text.Encoding] $propertyName = 'ASCII' $type::$propertyName
使用静态方法
[<TypeName>]::<MethodName>(<ArgumentList>)
实例:
获得所有网口
[System.Net.NetworkInformation.NetworkInterface]: :GetAllNetworkInterfaces()
通过变量做中转
$type = [System.Net.NetworkInformation.NetworkInterface] $methodName = 'GetAllNetworkInterfaces' $type::$methodName()
还可以带参数
[System.IO.Path]: :ChangeExtension("C:\none.exe", "bak""
参数列表同样可以抽出来
$argumentList = "C:\none.exe", "bak" [System.IO.Path]::ChangeExtension($argumentList)
通过Invoke调用
$argumentList = "C:\none.exe", "bak" [System.IO.Path]: :ChangeExtension.Invoke($argumentList)
DotNet中的类型使用
获得DotNet类型
[类型名称]
实例:
[System.DateTime] [System.Text.StringBuilder]
正则表达式(Regular Expression)
Powershell正则表达式规则
^ Matches the beginning of the line. $ Matches the end of the line. . Matches any single character except newline. Using m option allows it to match the newline as well. [...] Matches any single character in brackets. [^...] Matches any single character not in brackets. \A Beginning of the entire string. \z End of the entire string. \Z End of the entire string except allowable final line terminator. re* Matches 0 or more occurrences of the preceding expression. re+ Matches 1 or more of the previous thing. re? Matches 0 or 1 occurrence of the preceding expression. re{ n} Matches exactly n number of occurrences of the preceding expression. re{ n,} Matches n or more occurrences of the preceding expression. re{ n, m} Matches at least n and at most m occurrences of the preceding expression. a| b Matches either a or b. (re) Groups regular expressions and remembers the matched text. (?: re) Groups regular expressions without remembering the matched text. (?> re) Matches the independent pattern without backtracking. \w Matches the word characters. \W Matches the nonword characters. \s Matches the whitespace. Equivalent to [\t\n\r\f]. \S Matches the nonwhitespace. \d Matches the digits. Equivalent to [0-9]. \D Matches the nondigits. \A Matches the beginning of the string. \Z Matches the end of the string. If a newline exists, it matches just before newline. \z Matches the end of the string. \G Matches the point where the last match finished. \n Back-reference to capture group number "n". \b Matches the word boundaries when outside the brackets. Matches the backspace (0x08) when inside the brackets. \B Matches the nonword boundaries. \n, \t, etc. Matches newlines, carriage returns, tabs, etc. \Q Escape (quote) all characters up to \E. \E Ends quoting begun with \Q.
正则匹配语法
使用-match运算符
实例:
"book" -match "oo" "copy" -match "c..y" "big" -match "b[iou]g" "and" -match "[a-e]nd" "baggy" -match "g*" "baggy" -match "g?" "abcd defg" -match "\w+" #(this matches abcd) "abc" -match "\w*" "abc" -match "\w{2,}" "abc" -match "\w{2,3}"
这篇关于PowerShell教程 - 编程结构(Program Struct)- 第五部分的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享