管道有一個很好的比喻”棒球手”,它其實就是将資訊從一端投遞到另一端接收。
一 先舉幾個例子說明吧:
1利用管道停止zabbix服務(可以看到我這裡也用了通配符,但這不是一個好的習慣,除非你确認比對的就是你要的服務。是以在做管道之前首先要需要确認一下通配符比對的資訊,否則可能釀成不好的後果)
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201018TZKL.png" target="_blank"></a>
2 将擷取的服務導出到一個CSV檔案。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201019vR3t.png" target="_blank"></a>
3 将擷取的服務導出到一個XML檔案。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201022RKPj.png" target="_blank"></a>
4當然也可以導入
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201022jG6a.png" target="_blank"></a>
導入的功能我在以前寫過好多現實管理當中的例子:
<a href="http://lixiaosong.blog.51cto.com/705126/1341710" target="_blank">http://lixiaosong.blog.51cto.com/705126/1341710</a>
二 通過以上的舉例大家已經對管道有了一個初步的了解。但大家是不是始終有一個疑問,管道到底為我們傳遞了什麼東西。好接下來就是一個NB的東東了 get-member。
1接着舉例還是get-services,可以看到他得屬性和傳遞的參數:
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201022XYV1.png" target="_blank"></a>
2 這些參數有什麼用呢。我們看一眼:
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023jzeg.png" target="_blank"></a>
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023QwLA.png" target="_blank"></a>
3 說以上這些隻是為了下面這種情況。我知道了以上的功能,興緻勃勃的想通過管道擷取域内所有計算機的bios資訊。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023Qz2l.png" target="_blank"></a>
但是卻傳回了一堆錯誤資訊,這太郁悶了:
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023neX9.png" target="_blank"></a>
這是什麼原因導緻的呢。哼哼這點很重要打起精神來。
首先我們通過看一眼管道符左面指令的屬性,可以看到是ADComputer。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023xkO1.png" target="_blank"></a>
其次我們通過get-help看一眼管道符右面指令的byvalue屬性(這個屬性顯示是否接收參數)。卻沒有搜到關于ADComputer資訊。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201023nG4Z.png" target="_blank"></a>
然後回頭看一眼成功的get-service的指令,先看一眼get-memer屬性
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024mCzE.png" target="_blank"></a>
然後再看一眼stop-service的詳細幫助資訊。發現了關于“servicecontroller”byvalue資訊。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024kess.png" target="_blank"></a>
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024xa34.png" target="_blank"></a>
這下我們知道了 ,管道能否成功需要有一個前提。管道符左邊的屬性必須符合右邊指令的接收參數。
太郁悶了!!!難道我們就不能實作我們上邊的想法了麼?當然不是。我們仔細研究一下:
首先在看一眼get-wmiobject的幫助資訊(看!!!get-help又派上了用場)他居然支援-computername參數,然後接字元串!
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024bZqk.png" target="_blank"></a>
其次再看一眼get-adcomputer這個指令,可以看到能夠做為上面“-computername”參數的隻有Name屬性。
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024FD0I.png" target="_blank"></a>
好那我們怎麼能将Name屬性提取出來呢?有兩種方法:
1使用select
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_13922010249wIA.png" target="_blank"></a>
2 使用%{$_.加參數}方法(這種方法在以前的部落格裡也有參考)
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_13922010240FQy.png" target="_blank"></a>
3 确定一些擷取到的這些是否為字元串。哈哈有戲!!!
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024b2tN.png" target="_blank"></a>
4 讓我們應用它吧!好了成功了爽!!!
<a href="http://lixiaosong.blog.51cto.com/attachment/201402/12/705126_1392201024OFvs.png" target="_blank"></a>
Powershell的管道功能就介紹到這裡了!!!
接下來就是關于Session和大規模自動化介紹了很期待吧哈哈哈!
本文轉自handsome7038 51CTO部落格,原文連結:http://blog.51cto.com/lixiaosong/1358547