Windows PowerShell命令曆史用法詳解

來源:互聯網
上載者:User

標籤:windows powershell   get-history   

(一)擷取所有包含“history”關鍵字的cmdlet

使用PS C:\< Get-Command -Name *history* -CommandType cmdlet,結果。

650) this.width=650;" title="clipboard" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard" src="http://s3.51cto.com/wyfs02/M01/5A/33/wKioL1T5pZHDfKMxAAC3v38aiP8741.jpg" height="123" />

(二)通過尋找到history相關的命令後,我們可以再使用get-history來擷取命令的記錄,。

650) this.width=650;" title="clipboard[1]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[1]" src="http://s3.51cto.com/wyfs02/M02/5A/33/wKioL1T5pZPxWlvBAACVplncRN0541.jpg" height="187" />

(三)可以通過使用Get-Alias -Definition Get-History的方式來擷取該cmdlet的別名,。

650) this.width=650;" title="clipboard[2]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[2]" src="http://s3.51cto.com/wyfs02/M00/5A/33/wKioL1T5pZWAqrS0AAB23X2flfw651.jpg" height="122" />

(四)如何通過查看到的命令記錄資訊來運行曆史命令呢?

例如上面查到的命令記錄有Get-ChildItem,這個命令的記錄的ID號是3,那麼我們可以通過使用PS C:\< Invoke-History -Id 3來運行這個曆史命令,。

650) this.width=650;" title="clipboard[3]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[3]" src="http://s3.51cto.com/wyfs02/M02/5A/37/wKiom1T5pIGQpqs0AAD5AZKEUyk789.jpg" height="241" />

(五)如何查看特定的命令記錄呢?

可以通過使用Get-History ID號的形式,,擷取ID號為3、4的命令記錄。

650) this.width=650;" title="clipboard[4]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[4]" src="http://s3.51cto.com/wyfs02/M01/5A/33/wKioL1T5pZqy33y5AABQLycfkEA659.jpg" height="121" />

但是不能使用 Invoke-History -Id 3,4的方式來運行,否則會報錯,。

650) this.width=650;" title="clipboard[5]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[5]" src="http://s3.51cto.com/wyfs02/M01/5A/37/wKiom1T5pIfTOTVCAAC5oFX3rvk898.jpg" height="105" />

同樣的,

我們可以使用Get-History 3 | Invoke-History的方式去執行曆史命令,。

650) this.width=650;" title="clipboard[6]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[6]" src="http://s3.51cto.com/wyfs02/M02/5A/37/wKiom1T5pI-QVzZiAADUxJ38fVo743.jpg" height="190" />

但是,

不能使用Get-History 3,4 | Invoke-History的方式去執行命令,否則會報錯,。

650) this.width=650;" title="clipboard[7]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[7]" src="http://s3.51cto.com/wyfs02/M01/5A/33/wKioL1T5pa7xvo15AADBQtBTNSE676.jpg" height="104" />

通過的報錯可以看出,管道不能同時接受多重專案的輸入,我們可以添加foreach迴圈語句來解決該問題,。

使用命令:

Get-History -Id 3, 4 | foreach { Invoke-History -id $_.ID}

命令成功執行,。

650) this.width=650;" title="clipboard[8]" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="clipboard[8]" src="http://s3.51cto.com/wyfs02/M01/5A/37/wKiom1T5pJ_jxUF9AAENcIlsMYs201.jpg" height="296" />

Tip:http://blogs.technet.com/b/heyscriptingguy/archive/2015/03/03/avoid-scripting-use-powershell-command-history.aspx

==============================================================================

本文出自 “曾垂鑫的技術專欄” 部落格,謝絕轉載!

Windows PowerShell命令曆史用法詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.