(圖片出處: https://sploot.tw/2018/06/powershell-suite-windows-attack-tookit/)
powershell 苦手如我,最近有越來越多的需求要從頭到尾自建 CI/CD 與自動化佈署流程,每次寫都要查一次覺得很煩(越老越金魚腦),決定把常常用到的筆記方便查找
ENV
1 2 3 4 5 6 7 8
| $gci env:* | Sort-Object name
Name Value ---- ----- ALLUSERSPROFILE C:\ProgramData APPDATA C:\Users\Steven Tsai\AppData\Roaming
|
1 2 3 4 5 6
| $env:test="123" $env:test
123
|
Variable
If … Else
1 2 3 4
| $test="123" $if($test -eq "123") {echo "yes"} else {echo "no"}
yes
|
1 2 3 4 5 6
| $test="123" $result=if($test -eq "123") {echo "yes"} else {echo "no"} $result
yes
|
Split
1 2 3 4 5 6
| $test="a,b,c" $test.Split(",")
a b c
|
1 2 3 4
| $test="a,b,c" $test.Split(",")[0]
a
|
Invoke-WebRequest
1 2 3 4 5
| $PASSWORD = ConvertTo-SecureString -String "your_password" -AsPlainText -Force $CREDENTIAL = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList "your_account", $PASSWORD
$Invoke-WebRequest -Method PUT -Uri https://server/file.zip -UseBasicParsing -Credential $CREDENTIAL -Infile ".\file.zip"
|
Remove-Item
1 2
| $Remove-Item .\file.zip -Force
|
New-Item
1 2 3 4 5 6 7 8
| $New-Item -ItemType directory -Path C:\logs
目錄: C:\ Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2020/2/26 上午 12:28 test
|
Copy-Item
1
| $Copy-Item c:\source_folder C:\target_folder\ -Recurse
|
Out-File
1 2 3
| $test="123" $test | Out-File "C:\log.txt" -Encoding utf8 -Force
|
Write file without BOM
1 2
| $test="123" $[System.IO.File]::WriteAllLines("C:\log.txt", $test)
|
Start-Transcript
通常腳本會在長機器的時候自動執行,這時候並不會有人工介入,如果發生錯誤就很需要事後追查 Log ,但一段腳本可能上百上千行,到底錯在哪很難找,這時候就可以透過以下方法把執行的過程跟 output 都儲存下來
1 2 3
| $Start-Transcript -Path "c:\logs.txt" -Append $New-Item -Type Directory C:\TestData2\ -Force $Stop-Transcript
|
執行結束後打開 logs.txt 就可以看到以下輸出
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| ********************** Windows PowerShell 轉譯開始 ********************** 已啟動轉譯,輸出檔為 c:\logs.txt PS C:\Users\Steven Tsai> New-Item -Type Directory C:\TestData2\ -Force
目錄: C:\
Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2020/2/26 上午 12:40 TestData2
PS C:\Users\Steven Tsai> Stop-Transcript ********************** Windows PowerShell 轉譯結束 結束時間: 20200226004058 **********************
|
Set Global Enviroment
這是用在自動化安裝一些套件,例如 : Git , consul 後需要設定全域的環境變數,才能讓之後的指令能夠認得 consul –version 這類指令。
如果手動安裝,就是去把環境變數中 Path 加上 consul.exe 的位置,以下範例是抓出 Path 本來的設定值,並在尾段補上新的路徑設定
1 2 3 4 5
| $oldpath = (Get-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH).path
$newpath = "${oldpath};C:\consul\"
$Set-ItemProperty -Path 'Registry::HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment' -Name PATH -Value $newPath
|
Start-Service
1
| $Start-Service your_service_name
|
register task scheduler
1 2 3 4 5 6 7 8 9 10 11
| $action= (New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy bypass C:\start.ps1")
$ProvisionPrincipal = New-ScheduledTaskPrincipal -UserId "NT AUTHORITY\SYSTEM" -RunLevel Highest -LogonType S4U
$ProvisionTrigger = New-ScheduledTaskTrigger -AtStartup
$Register-ScheduledTask -TaskName "StartConsul" -TaskPath "\" -Action $action -Trigger $ProvisionTrigger -Principal $ProvisionPrincipal
|
Create User
1 2 3 4 5 6 7 8 9 10 11 12 13
| $Secure_String_Pwd=ConvertTo-SecureString "user_password" -AsPlainText -Force
$New-LocalUser "teamuser" -Password $Secure_String_Pwd -FullName "Arch team user" -Description "for arch team" -PasswordNeverExpires:$true
$Add-LocalGroupMember -Group "Administrators" -Member "teamuser"
$New-LocalGroup -Name "docker-users"
$Add-LocalGroupMember -Group "docker-users" -Member "archteamuser"
|
Expand-Archive
1 2 3
| $curl https://source_file.zip -o c:\targe_file.zip
$Expand-Archive c:\targe_file.zip c:\file -force
|