更新于 2024-08-08
$match = [System.Text.RegularExpressions.Regex]::Match($line, "^(\?|!)(?:\s+)(.+)$")
if(!$match.Success){
continue;
}
function ParseArguments($arguments){
$options = @{};
$switches = @();
$raws = @();
for($i=0; $i -lt $arguments.Length; $i++){
$arg = $arguments[$i]
if($arg -like "--*"){
if($i -eq ($arguments.Length - 1)) {
$switches += $arg;
continue;
}
$nextValue = $arguments[$i + 1];
if($nextValue -like "--*"){
$switches += $arg;
continue;
}
$options[$arg] = $nextValue
$i++;
continue;
}
if($arg -like "-*"){
$switches += $arg;
continue;
}
$raws += $arg;
}
return @{
Options=$options
Switches=$switches
Raws=$raws
};
}
class Arguments {
hidden [hashtable]$options = @{};
hidden [array]$switches = @();
hidden [array]$raws = @();
Arguments ([array]$arguments) {
for($i=0; $i -lt $arguments.Length; $i++){
$arg = $arguments[$i]
if($arg -like "--*"){
$arg = $arg.substring(2)
if($i -eq ($arguments.Length - 1)) {
$this.switches += $arg;
continue;
}
$nextValue = $arguments[$i + 1];
if($nextValue -like "--*"){
$this.switches += $arg;
continue;
}
$this.options[$arg] = $nextValue
$i++;
continue;
}
if($arg -like "-*"){
$arg = $arg.substring(1)
$this.switches += $arg;
continue;
}
$this.raws += $arg;
}
}
[string]GetOption([string]$key, [string] $defaultValue){
return $this.options[$key] ?? $defaultValue;
}
[string]GetRaw([int]$index){
return $this.raws[$index];
}
[boolean]HasOption([string]$key){
return $this.options.Keys -contains $key;
}
[boolean]NotHasOption([string]$key){
return $this.options.Keys -notcontains $key;
}
[boolean]HasSwitch([string]$key){
return $this.switches -contains $key;
}
[boolean]HasSwitchCi([string]$key){
return $this.switches -ccontains $key;
}
[boolean]NotHasSwitch([string]$key){
return $this.switches -notcontains $key;
}
[boolean]NotHasSwitchCi([string]$key){
return $this.switches -cnotcontains $key;
}
}
-and
-or
-xor
-not
!
-eq、-ieq、-ceq - 等于
-ne、-ine、-cne - 不等于
-gt、-igt、-cgt - 大于
-ge、-ige、-cge - 大于或等于
-lt、-ilt、-clt - 小于
-le、-ile、-cle - 小于或等于
-like、-ilike、-clike - 字符串匹配通配符模式
-notlike、-inotlike、-cnotlike - 字符串与通配符模式不匹配
-match、-imatch、-cmatch - 字符串匹配正则表达式模式
-notmatch、-inotmatch、-cnotmatch - 字符串与正则表达式模式不匹配
-replace,-ireplace,-creplace - 替换与正则表达式模式匹配的字符串
-contains、-icontains、-ccontains - 集合包含值
-notcontains、-inotcontains、-cnotcontains - 集合不包含值
-in - 值位于集合中
-notin - 值不在集合中
-is - 这两个对象的类型相同
-isnot - 对象类型不相同
$password = Read-Host "Please input password" -MaskInput
$text = Read-Host "Please input username"
$array = @(1, 2, 3)
$text = $array -join ' '
$array -contains 1
$array -notcontains 1
$array = @("lilith", "malier")
$array -ccontains "lilith"
$array -ccontains "Lilith"
$string = "hello world"
$string.IndexOf(' ')
$string.Split(' ')
$string.ToLower()
$string.ToUpper()
$string.Trim()
$string.TrimStart()
$string.TrimEnd()
$start = (Get-Date);
$end = (Get-Date);
$timeUsed = "{0:0.00}" -f ($end - $start).TotalSeconds
Set-Content -Path "xxxx" -Value "Hello, World!"
Add-Content -Path "xxxx" -Value "Hello again!"
"Hello, World!" | Out-File -FilePath "xxxx" -Encoding UTF8
$stream = [System.IO.StreamWriter] "xxxx"
$stream.WriteLine("Hello, World!")
$stream.Close()
#将文本文件数据读取到数组中
Get-Content -Path $PROFILE
Get-Location
Set-Location -Path C:\Windows
Set-Location -Path C:\Windows -PassThru
Push-Location -Path "Local Settings"
Pop-Location
Get-ChildItem -Path C:\ -Force -Recurse
Copy-Item -Path $PROFILE -Destination $($PROFILE -replace 'ps1$', 'bak') -Force
Copy-Item C:\temp\test1 -Recurse C:\temp\DeleteMe
Copy-Item -Filter *.txt -Path c:\data -Recurse -Destination C:\temp\text
New-Item -Path 'C:\temp\New Folder' -ItemType Directory
Remove-Item -Path C:\temp\DeleteMe -Recurse