欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Powershell 管道原理分析

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


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


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


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


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

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的類(lèi)型是 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可以打開(kāi)一個(gè)新的窗口),搜索byvalue

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

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




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


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

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


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

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類(lèi)型,對不上,因此掛了



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


看看我們的管道對象,可以看見(jiàn)他有一個(gè)叫做name的屬性,類(lèi)型為字符串。因為名字不匹配,所以管道仍然無(wú)法傳輸。


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

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


再執行一次,就工作了

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


當然上面的寫(xiě)法比較復雜繁瑣,一個(gè)小技巧在 computername 后面 指定一個(gè)大括號{},他會(huì )把管道前面的整個(gè)對象結果替換過(guò)來(lái),然后直接在里面取name的屬性就好了

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


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


我知道get-wmiobject 可以獲取很多系統信息,比如

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


但是如果我通過(guò)管道執行就會(huì )報錯

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


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




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


首先看看直接跑,報錯

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 - ...+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


原因很簡(jiǎn)單,類(lèi)型不匹配,注意這個(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


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


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


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

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


除了直接執行,通過(guò)foreach來(lái)接受管道信息,然后對每一個(gè)對象單獨執行也是可以的


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

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久