国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Powershell 管道原理分析

豆子一直以為管道其實(shí)是很簡單的,無非就是把前一個(gè)的輸出結(jié)果通過管道傳給下一個(gè)命令的輸入嘛,貌似很多網(wǎng)上教程也就這么解釋一下,然后就是各種演示命令了。昨天看了一個(gè)MVA2年前的powershell快速入門課程,才發(fā)現(xiàn)很多細(xì)節(jié)都忽略掉了,這個(gè)細(xì)節(jié)對于理解管道怎么工作是非常重要的,因?yàn)橛械臅r(shí)候不是所有的命令都支持互相管道傳輸。知道了他的工作方式,才能更有效的使用管道。


下面的解釋是基于Powershell V3以上的版本:


Powershell 的管道傳輸有兩種方式,byvalue和bypropertyname。


byvalue的意思就是輸出類型和輸入的類型是一樣,自然可以傳遞。


比如我可以把get-service的結(jié)果傳給stop-service,-whatif可以幫助我確認(rèn)這個(gè)命令的效果,這樣可以避免一些危險(xiǎn)的操作。

PS C:\windows\system32> Get-Service bits | Stop-Service -whatifWhat if: Performing the operation "Stop-Service" on target "Background Intelligent Transfer Service (bits)".

為什么他們可以傳遞呢,注意看get-service的類型是 servicecontroller


PS C:\windows\system32> Get-Service bits | gm   TypeName: System.ServiceProcess.ServiceControllerName                      MemberType    Definition----                      ----------    ----------Name                      AliasProperty Name = ServiceNameRequiredServices          AliasProperty RequiredServices = ServicesDependedOnDisposed                  Event         System.EventHandler Disposed(System.Object, System.EventArgs)Close                     Method        void Close()Continue                  Method        void Continue()CreateObjRef              Method        System.Runtime.Remoting.ObjRef CreateObjRef(type requestedType)Dispose                   Method        void Dispose(), void IDisposable.Dispose()


查看一下stop-service 的幫助文檔 (小技巧,-show可以打開一個(gè)新的窗口),搜索byvalue

PS C:\windows\system32> get-help Stop-Service -show

結(jié)果如下,他接受管道輸入,而且接受類型為serviceController,因此他可以接受get-service 的輸入。




bypropertyname 的意思是如果管道的輸入對象里面有一個(gè)屬性,他的名字和類型都和輸出命令的某一個(gè)參數(shù)的名字和類型都對的上號,那么這樣的管道也是成立的。


下面看一個(gè)例子,這樣執(zhí)行是失敗的。為什么呢,我們來分析一下

PS C:\windows\system32> get-adcomputer sydwsus | get-service bitsGet-Service : Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an athat is not null or empty, and then try the command again.At line:1 char:26+ get-adcomputer sydwsus | get-service bits+                          ~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidData: (CN=SYDWSUS,OU=C...om,DC=com,DC=au:PSObject) [Get-Service], Paramete   gValidationException    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetServiceCommand


首先看看輸出類型,是一個(gè)ADComputer的類型

PS C:\windows\system32> get-adcomputer sydwsus | gm   TypeName: Microsoft.ActiveDirectory.Management.ADComputerName              MemberType            Definition----              ----------            ----------Contains          Method                bool Contains(string propertyName)Equals            Method                bool Equals(System.Object obj)GetEnumerator     Method                System.Collections.IDictionaryEnumerator GetEnumerator()GetHashCode       Method                int GetHashCode()GetType           Method                type GetType()ToString          Method                string ToString()Item              ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string p.DistinguishedName Property              System.String DistinguishedName {get;set;}


查看一下get-service 的 幫助文檔,byvalue需要的是serviceController類型,對不上,因此掛了



那么我們看看bypropertyname,他接受一個(gè)管道對象的屬性為computername ,類型為字符串的作為他的參數(shù)輸入


看看我們的管道對象,可以看見他有一個(gè)叫做name的屬性,類型為字符串。因?yàn)槊植黄ヅ?,所以管道仍然無法傳輸。


解決方式很簡單,自定義一個(gè)屬性,保證他的名字一樣就行了,比如

PS C:\windows\system32> get-adcomputer sydwsus | select name, @{name="computername";expression={$_.name}}name                                                        computername----                                                        ------------SYDWSUS                                                     SYDWSUS


再執(zhí)行一次,就工作了

PS C:\windows\system32> get-adcomputer sydwsus | select name, @{name="computername";expression={$_.name}} | Get-ServicebitsStatus   Name               DisplayName------   ----               -----------Running  bits               Background Intelligent Transfer Ser...


當(dāng)然上面的寫法比較復(fù)雜繁瑣,一個(gè)小技巧在 computername 后面 指定一個(gè)大括號{},他會把管道前面的整個(gè)對象結(jié)果替換過來,然后直接在里面取name的屬性就好了

PS C:\windows\system32> get-adcomputer sydwsus | Get-Service -ComputerName {$_.name} bitsStatus   Name               DisplayName------   ----               -----------Running  bits               Background Intelligent Transfer Ser...


下面再來看幾個(gè)例子


我知道get-wmiobject 可以獲取很多系統(tǒng)信息,比如

PS C:\windows\system32> Get-WmiObject -class win32_biosSMBIOSBIOSVersion : 3.11.0950Manufacturer      : American Megatrends Inc.Name              : 3.11.0950SerialNumber      : 017349452253Version           : OEMC - 300


但是如果我通過管道執(zhí)行就會報(bào)錯(cuò)

PS C:\windows\system32> get-adcomputer sydwsus | Get-WmiObject -ComputerName {$_.name} -class win32_biosGet-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At line:1 char:26+ get-adcomputer sydwsus | Get-WmiObject -ComputerName {$_.name} -class win32_bios+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommandGet-WmiObject : The input object cannot be bound to any parameters for the command either because the command does nottake pipeline input or the input and its properties do not match any of the parameters that take pipeline input.At line:1 char:26+ get-adcomputer sydwsus | Get-WmiObject -ComputerName {$_.name} -class win32_bios+                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidArgument: (CN=SYDWSUS,OU=C...om,DC=com,DC=au:PSObject) [Get-WmiObject], Parameter


原因很簡單,這個(gè)命令既不支持byvalue,也不支持bypropertyname, 盡管他有comptuername這個(gè)參數(shù),這個(gè)參數(shù)拒絕接受管道的輸入




這種不支持管道的命令,豆子的一般處理方式要么要么直接運(yùn)行,要么是在管道后面用foreach的方式。


首先看看直接跑,報(bào)錯(cuò)

PS C:\windows\system32> get-wmiobject win32_bios -computername (Get-ADComputer -filter{operatingsystem -like "*2008 R2*"}| select name)get-wmiobject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At line:1 char:1+ get-wmiobject win32_bios -computername (Get-ADComputer -filter{operatingsystem - ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommandget-wmiobject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At line:1 char:1+ get-wmiobject win32_bios -computername (Get-ADComputer -filter{operatingsystem - ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


原因很簡單,類型不匹配,注意這個(gè)select的輸出對象仍然是ADComputer, 但是wmi的computer輸入要求是字符串


PS C:\windows\system32> Get-ADComputer -filter{operatingsystem -like "*2008 R2*"}| select name | gm   TypeName: Selected.Microsoft.ActiveDirectory.Management.ADComputerName        MemberType   Definition----        ----------   ----------Equals      Method       bool Equals(System.Object obj)GetHashCode Method       int GetHashCode()GetType     Method       type GetType()ToString    Method       string ToString()name        NoteProperty System.String name=SYDDC02


更改一下輸出方式,類型就變成字符串了,再次運(yùn)行就成功了


PS C:\windows\system32> Get-ADComputer -filter{operatingsystem -like "*2008 R2*"}| select -ExpandProperty name | gm   TypeName: System.StringName             MemberType            Definition----             ----------            ----------Clone            Method                System.Object Clone(), System.Object ICloneable.Clone()CompareTo        Method                int CompareTo(System.Object value), int CompareTo(string strB), int IComparab...Contains         Method                bool Contains(string value)CopyTo           Method                void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int co...EndsWith         Method                bool EndsWith(string value), bool EndsWith(string value, System.StringCompari...Equals           Method                bool Equals(System.Object obj), bool Equals(string value), bool Equals(string...GetEnumerator    Method                System.CharEnumerator GetEnumerator(), System.Collections.Generic.IEnumerator...GetHashCode      Method                int GetHashCode()GetType          Method                type GetType()GetTypeCode      Method                System.TypeCode GetTypeCode(), System.TypeCode IConvertible.GetTypeCode()
PS C:\windows\system32> get-wmiobject win32_bios -computername (Get-ADComputer -filter{operatingsystem -like "*2008 R2*"} |select -ExpandProperty name) | ftSMBIOSBIOSVersion       Manufacturer            Name                    SerialNumber            Version-----------------       ------------            ----                    ------------            -------6.00                    Phoenix Technologies... PhoenixBIOS 4.0 Rele... VMware-42 1e 84 9f d... INTEL  - 60400006.00                    Phoenix Technologies... PhoenixBIOS 4.0 Rele... VMware-42 0d c3 19 1... INTEL  - 60400006.00                    Phoenix Technologies... PhoenixBIOS 4.0 Rele... VMware-42 0d ec 36 7... INTEL  - 6040000


這種表達(dá)方式在2.0的時(shí)代很常見,3.0以后,可以直接當(dāng)作數(shù)組處理,比如下面更簡潔的方式也是可以的

PS C:\windows\system32> get-wmiobject win32_bios -computername (Get-ADComputer -filter{operatingsystem -like "*2008 R2*"}).nameSMBIOSBIOSVersion : 6.00Manufacturer      : Phoenix Technologies LTDName              : PhoenixBIOS 4.0 Release 6.0SerialNumber      : VMware-42 1e 84 9f d6 f6 b5 a0-01 6d 8a c0 13 ee e6 e4Version           : INTEL  - 6040000SMBIOSBIOSVersion : 6.00Manufacturer      : Phoenix Technologies LTDName              : PhoenixBIOS 4.0 Release 6.0SerialNumber      : VMware-42 0d c3 19 1b 3a d2 43-19 36 bb c5 00 5b 69 d2


除了直接執(zhí)行,通過foreach來接受管道信息,然后對每一個(gè)對象單獨(dú)執(zhí)行也是可以的


PS C:\windows\system32> Get-ADComputer -filter{operatingsystem -like "*2008 R2*"}| ForEach-Object{Get-WmiObject win32_bios}SMBIOSBIOSVersion : 3.11.0950Manufacturer      : American Megatrends Inc.Name              : 3.11.0950SerialNumber      : 017349452253Version           : OEMC - 300SMBIOSBIOSVersion : 3.11.0950Manufacturer      : American Megatrends Inc.Name              : 3.11.0950SerialNumber      : 017349452253Version           : OEMC - 300SMBIOSBIOSVersion : 3.11.0950Manufacturer      : American Megatrends Inc.Name              : 3.11.0950SerialNumber      : 017349452253Version           : OEMC - 300



另外,3.0以后增加了get-ciminstance 的commandlet,這個(gè)是用來替代get-wmiobject,而且他支持管道,比如, 查看所有2008 R2 上次重啟的時(shí)間

PS C:\windows\system32> get-adcomputer -filter {operatingsystem -like "*2008 R2*"} | select -ExpandProperty name |Get-CimInstance -class win32_operatingsystem  | select pscomputername,lastbootuptime | Out-GridView



本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Windows管理員常用的25個(gè)PowerShell命令
PowerShell應(yīng)用之-Get-WmiObject -Class Win32_Service
系統(tǒng)集 47
DOS下常用網(wǎng)絡(luò)命令解釋大全
NET命令詳解
網(wǎng)絡(luò)命令net
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服