powershell定义文本,用户交互,正则表达式

定义文本

PS C:\Users\Administrator> $site="yuan"
PS C:\Users\Administrator> $text="$site $(get-date) $env:windir"
PS C:\Users\Administrator> $text
yuan 09/16/2022 14:12:26 C:\Windows

在这里插入图片描述

#使用单引号闭合字符串输出双引号
'The site of my blog is"www.mossfly.com"'
#使用转义字符输出双引号
“My blog site is`"www.mossfly.com`""
#在字符串中输出换行符
“The site of my blog is `"www.mossfly.com`",`n飞苔博客"

powershell定义文本,用户交互,正则表达式_第1张图片

定义多行文本

@"
这首诗用来评价陶渊明的诗歌再好不过了

一语天然万古新,豪华落尽见真淳。
南窗白日羲皇上,未害渊明是晋人。
"@

用户交互

如果要提示用户输入可以使用read-host

$name=Read-Host "请输入您的用户名"
请输入您的用户名: Mosser Lee

Read-Host存储的字符串使用的单引号闭合,也就是说不会自动解析变量,不过可以通过ExpandString方法解析,例如:

PS E:> $inputPath=Read-Host "请输入文件路径"
请输入文件路径: $env:windir
PS E:> $inputPath
$env:windir
PS E:> $ExecutionContext.InvokeCommand.ExpandString($inputPath)
C:windows

但是如果想通过Read-Host接受敏感数据,比如密码,可以使用**-asSecureString选项**,不过这样读取到的数据为SecureString,及为加过密后的数据,当然你可以将密码转换成普通文本.

PS E:> $pwd=Read-Host -AsSecureString "请输入密码"
请输入密码: ******
PS E:> $pwd
System.Security.SecureString
PS E:> [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd))
abc123

询问用户名和密码Get-credential

如果你想授权一个用户需要提供用户凭据,可以使用Get-Credential命令,该命令会弹出一个安全对话框,一旦用户输入完毕,就会返回一个Credential对象包含用户名和密码

PS E:> $cre=Get-Credential yuan
PS E:> $cre
UserName                        Password
--------                        --------
yuan System.Security.SecureString

正则

powershell定义文本,用户交互,正则表达式_第2张图片

powershell定义文本,用户交互,正则表达式_第3张图片
powershell定义文本,用户交互,正则表达式_第4张图片
powershell定义文本,用户交互,正则表达式_第5张图片

  1. 匹配IP地址
$parttern="\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"
               #"\b \d{1,3} \. \d{1,3} \. \d{1,3} \. \d{1,3} \b"
"192.168.10.3" -match $parttern
"a.168.10.3" -match $parttern
"1000.168.10.3" -match $parttern
<#
 #输出
 #True
 #False
 #False
#>
  1. 验证Email格式
    如果你想验证用户提供的E-Mail地址是不是一个合法电子邮件格式,可以使用下面的正则表达式:
#$parttern = "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
#"\b[A-Z0-9 ._%+-]+  @[A-Z0-9.-]+  \. [A-Z]{2,4}\b"
$parttern = "^[A-Z0-9 ._%+-]+ @[A-Z0-9.-]+ \. [A-Z]{2,4}$"
"[email protected]" -match $parttern
".@ ." -match $parttern
<#
 #输出:
 #True
 #False
#>
  1. 模糊匹配 u?

具体到上面的例子,“u?”就确保了字符“u”在模式中不是必需的。常用的其它限定符,还有“*”(出现0次后者多次)和“+”(至少出现一次)。

"color" -match "colo u? r"
True
"colour" -match "colou?r"
True

匹配多个字符()

"Nov" -match "\b Nov (ember)? \b"
True
"November" -match "\bNov(ember)?\b"
True

如果你想使用多个可选的搜索词语,可以使用“或”操作符“|”:

"Bob and Ted" -match "Alice|Bob"
True

搜索一段话

# 搜索 "and Bob":
"Peter and Bob" -match "and (Bob|Willy)"
True
# 没有搜索到 "and Bob":
"Bob and Peter" -match "and (Bob|Willy)"
False

大小写敏感

# -match 大小写不敏感:
"hello" -match "heLLO"
True
# -cmatch 大小写敏感:
"hello" -cmatch "heLLO"
False

在文本中搜索信息

# 源文本中有两个电子邮件地址,-match只能匹配到一个
$rawtext = "[email protected] sent an e-mail that was forwarded to [email protected]."
$rawtext -match "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b"
True
$matches
Name                           Value
----                           -----
0                              [email protected]

深入使用子表达式

"www.pstips.net" -match "\b [^>]* >  (.*?) "
$matches[1]
#True
#www.pstips.net

你可能感兴趣的:(#,运维,powershell,正则表达式,windows,linux)