PowerShell

更新于 2024-07-11

解析参数

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;
    }
}

输入

$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

IO


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