最後更新: 2022-05-04
目錄
- Windows Management Framework
-
Output
- Write-Output
- | Select-Object "From"
- .From
- | Format-List
Windows Management Framework(WMF)
現在(20130712)的 Server03 及 XP 上已經不用獨立安裝 PowerShell 了,
因為它已經整合到 WMF 上.
P.S.
WMF 在 03 server 上的是 .Net 2.0
組件:
- Windows PowerShell 2.0
- WinRM 2.0 (WS 管理通訊協定,一種標準 Simple Object Access Protocol (SOAP) 型)
- BITS 4.0 (BITS 是一種服務,用戶端和伺服器之間傳輸檔案。)
PowerShell 的位置
S03, Win10
C:\WINDOWS\system32\WindowsPowerShell\v1.0
打開 PowerShell
Win+R(執行) -> PowerShell
Check Version
$PSversionTable
Name Value ---- ----- PSVersion 5.1.19041.1023 PSEdition Desktop PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...} BuildVersion 10.0.19041.1023 CLRVersion 4.0.30319.42000 WSManStackVersion 3.0 PSRemotingProtocolVersion 2.3 SerializationVersion 1.1.0.1
Script
幅檔名: .PS1
執行 script:
Mothod 1:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe
PS C:\script>.\script.ps1
Mothod 2:
powershell - File <scriptfile> <args>
-noexit, an optional parameter that tells the PowerShell console to remain open after the script finishes.
ExecutionPolicy
PS C:\script> Get-ExecutionPolicy
Restricted
Remark
- Restricted 執行原則不會允許任何指令碼執行
- AllSigned 和 RemoteSigned 執行原則禁止 Windows PowerShell 執行沒有數位簽章的指令碼
PS C:\script> Get-Help About_Signing
PS C:\script> Set-ExecutionPolicy Unrestricted
======
Profile
======
test-path $profile
new-item -path $profile -itemtype file -force
notepad $profile
======
Snapin
======
add-pssnapin quest.activeroles.admanagement
PS C:\script>Get-PSSnapin
============
remote sessions
============
Windows Management Instrumentation and Windows Remote Management (WS-Management) and
make sure they are both started and set to run automatically.
Enable-psRemoting
Enter-psSession MachineName
Exit-psSession
Remove Module
Get-Module -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Binary 2.0.2.140 AzureAD {Add-AzureADApplicationOwner, Get-AzureADApplication, Get-... ...
Remove-Module -Name NAME
Output
Write-Output
Module: Microsoft.PowerShell.Utility
Writes the specified objects to the pipeline.
If Write-Output is the last command in the pipeline, the objects are displayed in the console.
i.e.
Write-Output "test1 test2 test3"
Measure-Object
Write-Output "test1 test2 test3" | Measure-Object
Count : 1 Average : Sum : Maximum : Minimum : Property :
Format-List
# Formats the output as a list of properties in which each property appears on a new line.
| Format-List
Filter
Filter 的種類
- Server-side filtering: Get-Mailbox -Filter { Alias -like '*link' }
- Client-side filtering: Get-Mailbox | ? {$_.Alias -like '*link' }
Select-Object
# Selects objects or object properties.
Get-TransportRule -Identity Whitelist | Select-Object "From"
(Get-TransportRule -Identity Whitelist).From
Sort
...
Where
Get-Mailbox | where {$_.OrganizationalUnit -like "*remove_user*"}
$_
This is the variable for the current value in the pipe line / loop,
which is called $PSItem in Powershell 3 and newer.
$_.SomeProperty
?
A question mark("?") as an alias for the Where-Object cmdlet
Get-Mailbox | ? {$_.OrganizationalUnit -like "*remove_user*"}
ComparisonOperator
- -eq for equals
- -like for string comparison (必須要前後 "*")
- -notlike
可以用來找 OU 的 fields
- OrganizationalUnit: DOMAIN/OU
- Identity: DOMAIN/OU/cs
- DistinguishedName: CN=USERNAME,CN=OU,DC=DOMAIN,DC=DOMAIN