powershell 6, 7 的新特性。

2019-07-02 14:23:14 +08:00
 ps1aniuge
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
}



我原创 分享! 2016-12-02
if ($PSEdition -eq 'Desktop')
{
#win
}

if ($PSEdition -eq 'Core')
{
#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
3361 次点击
所在节点    PowerShell
3 条回复
ps1aniuge
2019-07-02 14:27:38 +08:00
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃P┃█┃
┃┃███████┃O┃█┃
┃┃███秘███┃W┃█┃
┣┫███████┃E┃█┃
┃┃███████┃R┃█┃
┃┃███████┃S┃█┃
┃┃███████┃H┃█┃
┣┫███████┃E┃█┃
┃┃███籍███┃L┃█┃
┃┃███████┃L┃█┃
┃┃███████┗━┛█┃
┣┫███████████┃
┃┃███V6.0██████┃
┃┃███████████┃
┗┻━━━━━━━━━━━┛

新增语法糖:
'a'..'z'
'z'..'a'
'c'..'g'


'A'..'Z'


支持中文,但若想使用,必须让区位码相连的,有意义的字符才有用。

例子:

[char]27721 #返回 汉
[char]27726 #返回 汎
'汉'..'汎' #则返回 [汉] 的区位码,到 [汎] 区位码之间的字符。汉,汊,汋,汌,汍,汎。好像这样没啥用。


'㈠'..'㈩' #返回㈠,㈡,。。㈩,这样就有用了。
ps1aniuge
2019-07-02 14:28:29 +08:00
----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃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



----------------------------------------------------------------
┏┳━━━━━━━━━━━┓
┃┃███████████┃
┃┃███████████┃
┣┫███████┏━┓█┃
┃┃███████┃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
ps1aniuge
2019-07-02 14:29:43 +08:00
不能自定义字体,看起来很不爽,唉~

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/579290

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX