powershell6,7新特性


powershell 6,7的新特性。
1每個特性都注明了版本號,從這個版本開始,才支持這個特性。
2歡迎挑毛病,讓我更完善帖子。
3大都是ps6的新特性。ps7剛剛開始開發,新特性也只有一點點。
 
 
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

在 powershell 6.0 中新增內置變量
$IsCoreCLR                                                                                                             
$IsLinux                                                                                                             
$IsMacOS                                                                                                            
$IsWindows
用於判斷系統。
#假想中的復制文件腳本,由於win,linux目錄路徑,不兼容。
#所以你要在一個腳本中,分別寫2段代碼。
if ($IsWindows)
{
 copy-item c:\xxx d:\yyy
}
if ($IsLinux)
{
 copy-item /home/user1  /home/user2
}
 

powershell 傳教士 分享!2016-12-02
if ($PSEdition -eq 'Desktop') #ps v5.1支持
{
#win
}
if ($PSEdition -eq 'Core')
{
#ps6 in win,ps6 in linux
}
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
powershell6.0即linux版中,New-PSSession新增3個參數,
【-HostName】,
【-UserName】,
【-KeyFilePath】,
 
-SSHTransport 布爾型 強制使用ssh協議,而不是winrm協議
用於linux客戶機,連接linux服務器。
 
命令:
$連接2 = New-PSSession -HostName 127.0.0.1 -UserName  user006 #手動輸入密碼或用-KeyFilePath 選項
Invoke-Command -Session $連接2 -ScriptBlock {new-item ~/ccc.txt}
 
用了-HostName參數后,端口默認22。
用了-computername參數后,端口默認5985。
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
Get-Content $Path -Encoding Byte
變更為
Get-Content $Path -AsByteStream
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
新增語法糖:
'a'..'z'
'z'..'a'
'c'..'g'

'A'..'Z'

支持中文,但若想使用,必須讓區位碼相連的,有意義的字符才有用。
例子:
[char]27721 #返回 漢
[char]27726 #返回 汎
'漢'..'汎' #則返回【漢】的區位碼,到【汎】區位碼之間的字符。漢,汊,汋,汌,汍,汎。好像這樣沒啥用。

'㈠'..'㈩' #返回㈠,㈡,。。㈩,這樣就有用了。
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

import-csv 現在已經支持
`r,`n,`r`n 格式的回車。

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
powershell 6.0:
已經支持linux屏幕顏色代碼。
"`e[38;2;128;0;128;48;2;0;0;0m"
https://docs.microsoft.com/zh-cn/windows/console/console-virtual-terminal-sequences
 
$green="`e[92m"
$none="`e[0m"
$red="`e[91m"
$yellow="`e[93m"
$magenta="`e[95m"
$cyan="`e[96m"
echo  "$green powershell 支持linux顏色 $none 例子"

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
linux中支持。win中不支持。
特殊符號
 "`u{1f44d}"  # evals to  👍
$a = "`u{1f00e}"  # 麻將8萬。
$a
${`u{1f00e}} = 'a' # 麻將8萬。
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
新增Markdown模塊
Invoke-RestMethod https://raw.githubusercontent.com/kubernetes/kubernetes/master/README.md  -outfile /tmp/r.md

$a = ConvertFrom-Markdown -LiteralPath /tmp/r.md  #把r.md源碼渲染成html。
$a.html
$b = ConvertFrom-Markdown -LiteralPath /tmp/r.md   -AsVT100EncodedString #把r.md源碼渲染成 字符界面+彩色 。
$b.AsVT100EncodedString
ps傳教士 原創整理

Get-MarkdownOption
Set-MarkdownOption
查看-設置顏色

Show-Markdown 基本沒用

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
添加ThreadJob模塊,中有一個命令:
Start-ThreadJob
   
SYNTAX
    Start-ThreadJob [-ScriptBlock] <scriptblock> [-Name <string>] [-InitializationScript <scriptblock>] [-InputObject <psobject>] [-ArgumentList
    <Object[]>] [-ThrottleLimit <int>] [<CommonParameters>]
   
    Start-ThreadJob [-FilePath] <string> [-Name <string>] [-InitializationScript <scriptblock>] [-InputObject <psobject>] [-ArgumentList <Object[
    ]>] [-ThrottleLimit <int>] [<CommonParameters>]
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

Invoke-RestMethod,Invoke-WebRequest
添加了重試的功能
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
PSCustomobject
添加ForEach和Where方法
添加Count和Length屬性
 
 
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

-replace  支持代碼塊
原來:
"字符串" -replace "查找", {字符串}
現在:
"字符串" -replace "查找", {return "a" * 7} #加入非字符串,則被視為代碼塊。

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.1██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
Select-Object  新增 -SkipIndex 參數

1..8 | Select-Object -SkipIndex 0,2,3
1,2,3,4,5,6,7,8中,去掉第134個,返回2 5 6 7 8
也就是說,【大數組】中,跳過所有【小數組中包含】的元素。

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.2██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

從6.2 preview3開始:
PS /root> 1..3 | Join-String -OutputPrefix "A " -OutputSuffix " B" -Separator "," -SingleQuote
A '1','2','3' B
# 數組插入,分隔符,引號,頭尾。
Join-String  # 數組 |Join-String
-Property
-Separator 'x' #分隔符x
-OutputPrefix <string>]  #最前面加 字符串
[-OutputSuffix <string>  #后加
-SingleQuote
#powershell傳教士 整理 分享
-DoubleQuote
-FormatString <string>
123 | Join-String -FormatString '{0:N2}'
123.00

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
以Sort-Object,添加參數-Top,-Bottom,對頂部/底部篩選。
並在6.2 pr4中添加-stable參數
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.2 pr4████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
enum枚舉新增,支持從類中繼承
enum MyEnum : Int64 {
    a = [int64]::MaxValue
}

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.2 pr4████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
format-hex原來只支持字符串,數組。
現新增支持枚舉。
[System.Net.HttpStatusCode]::OK | format-hex

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.2██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
======
從ps 5.0開始:
模塊名:Microsoft.PowerShell.Core
Enter-PSHostProcess 和 Exit-PSHostProcess  讓您調試另一個powershell進程。
運行 Enter-PSHostProcess 附加到一個特定的進程 ID,然后再運行得到運行空間返回運行在進程內的活動空間。
運行 Exit-PSHostProcess,當您完成調試進程內的腳本從進程中分離。
 
======
從ps6.2 rc1開始:
Enter-PSHostProcess 新增-CustomPipeName參數,這樣就【不需要總變化的進程id】了。
powershell進程1:
pwsh -custompipename mypipe
powershell進程2:
Enter-PSHostProcess -CustomPipeName mypipe
2019-04-05 PowerShell v6.2正式版將發布!
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.2██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
Enable-ExperimentalFeature -Name PSTempDrive
后,將增加【temp:】盤符
Enable-ExperimentalFeature -Name PSUseAbbreviationExpansion
后,將增加長命令的駝峰縮寫
i-arsavsf等於Import-AzRecoveryServicesAsrVaultSettingsFile
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V7.0pr1 ████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

將QuoteFields參數添加到ConvertTo-Csv和Export-Csv。這允許顯式指向要在輸出中引用的字段。
將新參數添加-UseQuotes到Export-Csv和ConvertTo-Csv cmdlet:
Never - 不要引用任何東西。
Always - 引用所有內容(當前和默認行為)。
AsNeeded - 僅引用需要它的字段(它們包含分隔符)。

----------------------------------------------------------------

┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V7.0pr1 ████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
在腳本中,管道符可以放在行首,原來只能在行尾。
原來:
xxx1 |
xxx2 |
xxx3

現在:
xxx1
| xxx2
| xxx3
 

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V7.0pr1 ████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
powershell 新增線程方法重載?
$r1 = [powershell]::Create().AddScript('1..10 | % { [console]::WriteLine("#1 running $_"); sleep 1 }').InvokeAsync()
$r2 = [powershell]::Create().AddScript('1..15 | % { [console]::WriteLine("#2 running $_"); sleep 1 }').InvokeAsync()
$r3 = [powershell]::Create().AddScript('1..13 | % { [console]::WriteLine("#3 running $_"); sleep 1 }').beginInvoke()
[system.threading.tasks.task]::waitall($r1, $r2, $r3); write-host "All tasks done"
 
----------------------------------------------------------------

┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V7.0pr2 ████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
-split 支持負數
PS /root> 'a b c d' -split ' ',-2
a b c
d
PS /root> 'a b c d' -split ' ',-3
a b
c
d
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview3 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

新增並發參數
| ForEach-Object -Parallel
例子:
1
get-content ip.txt |foreach-object -Parallel{ ping.exe $_  }
2
Get-Content /powershell/ip.txt |ForEach-Object -Parallel {if (Test-Connection -Count 1 -TargetName $_ -Quiet) {$_} } 6>$null
3
$c = 123
$a = 1..100
$a | ForEach-Object -ThrottleLimit 5  -Parallel {
 Write-Host $_
 Start-Sleep -Seconds 3
#這里調用函數,必須在這里定義。不能在外部定義。因為這是一個單獨的線程。不支持執行代碼塊。支持調用腳本。
 $c1 = $using:c #變量要先克隆到線程內,再計算
}

參數:
-Parallel
-ThrottleLimit
-TimeoutSeconds
-AsJob
 
手冊:
https://docs.microsoft.com/zh-cn/powershell/module/Microsoft.PowerShell.Core/ForEach-Object?view=powershell-7
 
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview3 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
Select-String -raw
默認會輸出行號,
加上這個參數,只輸出匹配。
 
----------------------------------------------------------------
 
還有些細小的特性,我可能忘記添加在這里,歡迎提醒我補充,謝謝觀看。
 
此貼不定期更新,更新新特性。
 
 
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview4 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
新增三目運算符

$變量名 = 表達式 ? 判斷為真時運行的語句 : 判斷為假時運行的語句
 
$message = (Test-Path $path) ? "Path exists" : "Path not found"
 
return $blackList -contains $target ? (Block-Target $target) : (Register-Target $target)
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview4 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
-ErrorAction
-WarningAction
$DebugPreference,
$ErrorActionPreference,
$InformationPreference,
$ProgressPreference,
$VerbosePreference,
新增 'break' 枚舉值,用於立即進入調試器。

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview4 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
Invoke-DSCResource 支持繞過LCM(本地配置管理器),
從這個版本開始,linux下支持:
ps1----》mof
 
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview5 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
'前面的指令成功' && '則運行后面的指令';
1/0  && '因為前面的指令失敗則后面的不運行'

'前面的指令失敗' || '則運行后面的指令';
1/0  || '因為前面的指令失敗則后面的運行'
 
1/0 && '成功' || '失敗'
#類似於 if (1/0) {'成功'}else {'失敗'} ,但不完全一樣。
RuntimeException: Attempted to divide by zero.
失敗

【??】的本質是一種if跳轉。
左側是null,則執行右側。
左側非null,則執行左側。
 
$a ??= 1
左側的變量值不是$null時,才為變量賦值。
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview5 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
Format-Hex 更新
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview5 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
Select-String 默認以反色突出顯示。
若想要舊的不反色顯示,加上參數-NoEmphasis

----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview5 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
$ErrorView 從NormalView變成ConciseView。
報錯信息省略了,錯誤腳本行號。
$ErrorView = 'NormalView' 
新增命令:Get-Error
以formatlist的形式,顯示最近的報錯信息列表。
 
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview6 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
Invoke-WebRequest和Invoke-RestMethod
新增-SkipHttpErrorCheck參數,
新增-ResponseStatusVariable參數
可以不同時使用。
 
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview6 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
get-counter監控命令,
Out-Printer
Update-List
回歸
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview6 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
linux新增Get-Clipboard和Set-Clipboard
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃██V7.0 preview6 ██┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛
 
新增幺蛾子運算符
空條件運算符?.和?[]   (#10960)

?.方法名()  不會報錯
'abc'?.toupper()?.tulower()  #我測試不靈
 
${xxx}.?[2] 不會報錯
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM