国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PowerShell > 正文

Windows Powershell 別名

2020-10-29 21:04:40
字體:
供稿:網(wǎng)友

簡短描述
在Windows PowerShell中, 別名就是cmdlets或其他命令的替代名稱.

詳細(xì)描述
別名就是cmdlet或者命令(例如: 函數(shù), 腳本, 文件, 可執(zhí)行文件. )的替代名稱或者說是個昵稱. 在使用命令的地方, 你都可以使用別名.

cmdlet 的名稱由一個動詞和一個名詞組成,其功能對用戶來講一目了然。但是對于一個經(jīng)常使用powershell命令的人每天敲那么多命令也很麻煩啊。能不能把命令縮短一點(diǎn)呢?于是“別名”就應(yīng)運(yùn)而生了。Powershell內(nèi)部也實(shí)現(xiàn)了很多常用命令的別名。例如Get-ChildItem,列出當(dāng)前的子文件或目錄。它有兩個別名:ls 和 dir,這兩個別名來源于unix 的shell和windows的cmd。
因此別名有兩個作用:

繼承:繼承unix-shell和windows-cmd。
方便:方便用戶使用。
處理別名:
查詢別名所指的真實(shí)cmdlet命令。

PS C:/PS> Get-Alias -name lsCommandType   Name                        Definition-----------   ----                        ----------Alias      ls                         Get-ChildItemPS C:/PS> Get-Alias -name dirCommandType   Name                        Definition-----------   ----                        ----------Alias      dir                         Get-ChildItemPS C:/PS> Get-Alias -name flCommandType   Name                        Definition-----------   ----                        ----------Alias      fl                         Format-ListPS C:/PS> Get-Alias -name ftCommandType   Name                        Definition-----------   ----                        ----------Alias      ft                         Format-Table

查看可用的別名
查看可用的別名,可以通過” ls alias:” 或者 ”Get-Alias“
如何查看所有以Remove打頭的cmdlet的命令的別名呢?

PS C:/PS> dir alias: | where {$_.Definition.Startswith("Remove")}CommandType   Name                        Definition-----------   ----                        ----------Alias      del                         Remove-ItemAlias      erase                        Remove-ItemAlias      rbp                         Remove-PSBreakpointAlias      rd                         Remove-ItemAlias      rdr                         Remove-PSDriveAlias      ri                         Remove-ItemAlias      rjb                         Remove-JobAlias      rm                         Remove-ItemAlias      rmdir                        Remove-ItemAlias      rmo                         Remove-ModuleAlias      rp                         Remove-ItemPropertyAlias      rsn                         Remove-PSSessionAlias      rsnp                        Remove-PSSnapinAlias      rv                         Remove-VariableAlias      rwmi                        Remove-WMIObject

說明:dir alias:獲取的是別名的數(shù)組,通過where對數(shù)組元素進(jìn)行遍歷,$_代表當(dāng)前元素,alias的Definition為String類型,因?yàn)閜owershell支持.net,.net中的string類有一個方法Startswith。通過where過濾集合在powershell中使用非常廣泛。

有的cmdlet命令可能有2-3個別名,我們可以通過下面的命令查看所有別名和指向cmdlet的別名的個數(shù)。

PS C:/PS> ls alias: | Group-Object definition | sort -Descending CountCount Name           Group----- ----           -----  6 Remove-Item        {del, erase, rd, ri...}  3 Set-Location       {cd, chdir, sl}  3 Get-History        {ghy, h, history}  3 Get-ChildItem       {dir, gci, ls}  3 Get-Content        {cat, gc, type}  3 Move-Item         {mi, move, mv}  3 Copy-Item         {copy, cp, cpi}  2 Start-Process       {saps, start}  2 Set-Variable       {set, sv}  2 Write-Output       {echo, write}  2 Get-Process        {gps, ps}  2 Invoke-History      {ihy, r}  2 New-PSDrive        {mount, ndr}  2 Stop-Process       {kill, spps}  2 Rename-Item        {ren, rni}  2 Get-Location       {gl, pwd}  2 Compare-Object      {compare, diff}  2 Where-Object       {?, where}  2 ForEach-Object      {%, foreach}  2 Clear-Host        {clear, cls}  1 Out-Host         {oh}  1 New-PSSession       {nsn}  1 New-Variable       {nv}  1 Out-GridView       {ogv}  1 Pop-Location       {popd}  1 Tee-Object        {tee}  1 Remove-PSBreakpoint    {rbp}  1 Receive-Job        {rcjb}  1 Push-Location       {pushd}  1 mkdir           {md}  1 Measure-Object      {measure}  1 help           {man}  1 Remove-PSSnapin      {rsnp}  1 Out-Printer        {lp}  1 New-Item         {ni}  1 New-Module        {nmo}  1 New-Alias         {nal}  1 Move-ItemProperty     {mp}  1 Wait-Job         {wjb}  1 Remove-PSDrive      {rdr}  1 Start-Service       {sasv}  1 Set-PSBreakpoint     {sbp}  1 Set-ItemProperty     {sp}  1 Start-Job         {sajb}  1 Set-Alias         {sal}  1 Start-Sleep        {sleep}  1 Set-Item         {si}  1 Select-Object       {select}  1 Set-Content        {sc}  1 Sort-Object        {sort}  1 Remove-WMIObject     {rwmi}  1 Remove-Module       {rmo}  1 Rename-ItemProperty    {rnp}  1 Stop-Service       {spsv}  1 Set-WMIInstance      {swmi}  1 Remove-Job        {rjb}  1 Remove-Variable      {rv}  1 Resolve-Path       {rvpa}  1 Stop-Job         {spjb}  1 Remove-ItemProperty    {rp}  1 Remove-PSSession     {rsn}  1 Exit-PSSession      {exsn}  1 Format-Custom       {fc}  1 Enter-PSSession      {etsn}  1 Export-Csv        {epcsv}  1 Export-PSSession     {epsn}  1 Format-List        {fl}  1 Get-PSBreakpoint     {gbp}  1 Get-Command        {gcm}  1 Get-Alias         {gal}  1 Format-Table       {ft}  1 Format-Wide        {fw}  1 Export-Alias       {epal}  1 Clear-History       {clhy}  1 Clear-Item        {cli}  1 Clear-Content       {clc}  1 Add-Content        {ac}  1 Add-PSSnapIn       {asnp}  1 Clear-ItemProperty    {clp}  1 Disable-PSBreakpoint   {dbp}  1 Enable-PSBreakpoint    {ebp}  1 Convert-Path       {cvpa}  1 Clear-Variable      {clv}  1 Copy-ItemProperty     {cpp}  1 Invoke-Expression     {iex}  1 Invoke-Item        {ii}  1 Invoke-Command      {icm}  1 Get-Variable       {gv}  1 Get-WmiObject       {gwmi}  1 Import-Alias       {ipal}  1 powershell_ise.exe    {ise}  1 Invoke-WMIMethod     {iwmi}  1 Import-PSSession     {ipsn}  1 Import-Csv        {ipcsv}  1 Import-Module       {ipmo}  1 Get-Unique        {gu}  1 Get-Job          {gjb}  1 Get-Member        {gm}  1 Get-Item         {gi}  1 Get-PSCallStack      {gcs}  1 Get-PSDrive        {gdr}  1 Get-Module        {gmo}  1 Get-PSSnapIn       {gsnp}  1 Get-Service        {gsv}  1 Get-PSSession       {gsn}  1 Get-ItemProperty     {gp}  1 Group-Object       {group}

創(chuàng)建自己的別名
給記事本創(chuàng)建一個別名,并查看該別名;

PS C:/PS> Set-Alias -Name Edit -Value notepadPS C:/PS> EditPS C:/PS> $alias:Editnotepad

刪除自己的別名
別名不用刪除,自定義的別名在powershell退出時(shí)會自動清除。但是請放心,powershell內(nèi)置別名(諸如ls,dir,fl等)不會清除。如果你非得手工刪除別名。請使用

PS C:/PS> del alias:Edit保存自己的別名
可以使用Export-Alias將別名導(dǎo)出到文件,需要時(shí)再通過Import-Alias導(dǎo)入。但是導(dǎo)入時(shí)可能會有異常,提示別名已經(jīng)存在無法導(dǎo)入:

PS C:/PS> Import-Alias alias.ps1Import-Alias : Alias not allowed because an alias with the name 'ac' already exists.At line:1 char:13+ Import-Alias <<<< alias.ps1  + CategoryInfo     : ResourceExists: (ac:String) [Import-Alias], SessionStateException  + FullyQualifiedErrorId : AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand

這時(shí)可以使用Force強(qiáng)制導(dǎo)入。

PS C:/PS> Export-Alias alias.ps1PS C:/PS> Import-Alias -Force alias.ps1

 
例如, 如果你為Get-AuthenticodeSignature設(shè)置了別名"gas", 你可以直接輸入:

gas c:/scripts/sqlscript.ps1

 
而不必輸入:
 

get-authenticodesignature c:/scripts/sqlscript.ps1

如果你為微軟的Word設(shè)置了別名"word", 你可以直接輸入:
 
word
 
而不必輸入:
 
"c:/program files/microsoft office/office11/winword.exe"
 

預(yù)定義的別名

Windows PowerShell已經(jīng)預(yù)定義了一部分別名, 例如: "cd"和"chdir"都是Set-Location的別名, "ls" 和"dir"是Get-Childitem的別名.
 
查找系統(tǒng)中的所有別名(包括預(yù)定別名), 輸入如下命令:
 
get-alias

別名相關(guān)的CMDLETS

Windows PowerShell包含了幾個cmdlets用于操作別名.
 
?         Get-Alias: 取得當(dāng)前會話(session)中的別名. 
?         New-Alias: 創(chuàng)建一個新的別名.
?         Set-Alias: 創(chuàng)建或修改一個別名.
?         Export-Alias:  導(dǎo)出一個或多個別名到文件中.
?         Import-Alias:  導(dǎo)入一個別文件到Windows PowerShell.

 

需要cmdlets的詳細(xì)信息, 輸入:
 
get-help <cmdlet-name> -detailed
 
例如:
 
get-help export-alias -detailed
 
創(chuàng)建別名
創(chuàng)建一個新的別名, 可以使用New-Alias cmdlet. 例如, 要為Get-Help創(chuàng)建一個"gh"別名, 輸入,
 
new-alias -name gh -value get-help
 
你可以在命令中就好像你使用的完整的cmdlet名稱和各種參數(shù)一樣, 來使用這個別名.
 
例如, 取得Get-WmiObject cmdlet的詳細(xì)信息, 你只要輸入:
 
get-help get-wmiobject -detailed
 
或者
 
gh get-wmiobject -detailed
 
保存別名
你創(chuàng)建的別名只在當(dāng)前的會話(session)有效. 要在不同的會話中使用別名, 你必須把別名的定義寫入你的Windows PowerShell配置文件, 或者使用Export-Alias將別名存儲到文件里.
 
查找別名
要在當(dāng)前控制臺上顯示所有別名, 包括Windows PowerShell預(yù)定義的別名, 你的Windows PowerShell配置文件中定義的別名, 你在當(dāng)前會話創(chuàng)建的別名, 只要輸入:
 
get-alias
 
如果需要特定的別名, 通過為Get-Alias指定Name參數(shù)即可. 例如, 要取得"p"開頭的別名, 輸入
 
get-alias -name p*
 
要查找特定cmdlet的所有別名, 可以輸入:
 
get-alias | where-object {$_.Definition -eq "<cmdlet-name>"}
 
例如:
 
get-alias | where-object {$_.Definition -eq "Remove-Item"}
 

為帶有參數(shù)的命令創(chuàng)建別名

你可以為cmdlet, 腳本, 函數(shù), 或者可執(zhí)行文件賦予別名. 但是你不能為帶有參數(shù)的命令設(shè)置別名. 例如, 你能夠?yàn)?Get-Eventlog"設(shè)置別名, 但是你不能為"Get-Eventlog -logname security"設(shè)置別名.
 
你只能通過創(chuàng)建一個包含該命令的函數(shù)來解決這個問題. 例如, 如下命令創(chuàng)建一個叫做”seclog"的函數(shù), 此函數(shù)可以表示"get-eventlog -logname security”命令.
 
function seclog {get-eventlog -logname security}
 
現(xiàn)在你可以輸入用名字"seclog"來簡化之前的命令, 你還可以為函數(shù)"seclog"創(chuàng)建別名.
 
關(guān)于函數(shù)的信息, 輸入:
 
get-help about_function
 

別名對象

Windows PowerShell別名實(shí)際是類System.Management.Automation.AliasInfo的實(shí)例對象. 關(guān)于對象類型信息, 參見MSDN 中"AliasInfo Class"的主題.
 
要查看別名對象上的屬性和方法, 首先取得別名對象, 并且通過管道傳遞給Get-Member cmdlet. 例如,
 
get-alias | get-member
 
要查看特定別名的屬性值, 例如別名"dir", 取得該別名并通過管道傳遞給Format-List cmdlet. 例如, 如下代碼首先取得別名"dir"對象, 通過管道傳遞給Format-List cmdlet, 通過對Format-List的參數(shù)Property賦值為所有 (*), 來顯示別名"dir"的所有屬性.
 
get-alias -name dir | format-list -property *
 
WINDOWS POWERSHELL別名PROVIDER

Windows PowerShell別名provider(譯者注: 一個Provider就類似于用戶使用的文件系統(tǒng)目錄結(jié)構(gòu), 微軟開發(fā)人員通過MVC這種設(shè)計(jì)思想, 將變量, 注冊表, 別名等資源的管理, 抽象為文件系統(tǒng)的管理. 這樣用戶可以使用統(tǒng)一的語法對各種資源進(jìn)行訪問. PowerShell開發(fā)人員, 也能為PowerShell擴(kuò)展其他的Provider.) , 使得在Windows PowerShell中, 查看別名就像瀏覽文件系統(tǒng)驅(qū)動器一樣.
 
別名provider提供了"Alias:"驅(qū)動器(譯者注:虛擬驅(qū)動器, 只有在PowerShell中有效). 要進(jìn)入Alias: 驅(qū)動器, 輸入:
 
set-location alias:
 
要查看該驅(qū)動器的內(nèi)容, 輸入:
 
get-childitem
 
在Windows PowerShell其他的驅(qū)動器時(shí), 如果想查看別名驅(qū)動器, 在目錄前要協(xié)商驅(qū)動器名稱, 緊跟著一個冒號(:). 例如,
 
get-childitem -path alias:
 
要取得特定別名的信息, 輸入驅(qū)動器名稱和別名名稱, 或名稱的模式(pattern. 筆者注: 一般使用的就是通配符. ). 例如, 要取得所有以"p"開頭別名的列表, 輸入:
 
get-childitem -path alias:p*
 
需要更多關(guān)于Windows PowerShell別名provider的信息, 輸入:
 
get-help alias-psprovider
 
 您還可以參考
要列出關(guān)于別名的cmdlets, 輸入:
 
get-help *-Alias
 
關(guān)于函數(shù)的信息, 輸入:
 
get-help about_function

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 拉孜县| 岳阳市| 刚察县| 黑山县| 神木县| 通化县| 高州市| 乌拉特后旗| 巴林右旗| 乌兰浩特市| 宣武区| 平塘县| 崇信县| 房山区| 日喀则市| 会泽县| 富锦市| 沭阳县| 藁城市| 岳阳市| 邢台县| 临沭县| 汽车| 邛崃市| 临泉县| 渭源县| 龙口市| 曲沃县| 韶关市| 秀山| 土默特右旗| 尼玛县| 凌云县| 剑阁县| 汝城县| 商城县| 奉贤区| 贡嘎县| 遵化市| 靖宇县| 云霄县|