欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Windows Powershell方法(對象能做什么)

 更新時(shí)間:2014年09月25日 10:15:30   投稿:hebedich  
方法定義了一個(gè)對象可以做什么事情。當(dāng)你把一個(gè)對象輸出在控制臺時(shí),它的屬性可能會被轉(zhuǎn)換成可視的文本。但是它的方法卻不可見。

方法定義了一個(gè)對象可以做什么事情。當(dāng)你把一個(gè)對象輸出在控制臺時(shí),它的屬性可能會被轉(zhuǎn)換成可視的文本。但是它的方法卻不可見。列出一個(gè)對象的所有方法可是使用Get-Member命令,給“MemeberType”參數(shù) 傳入“Method”:

復(fù)制代碼 代碼如下:

PS C:Powershell> $Host | Get-Member -MemberType Method

   TypeName: System.Management.Automation.Internal.Host.InternalHost

Name                     MemberType Definition
----                     ---------- ----------
EnterNestedPrompt       Method     System.Void EnterNestedPrompt()
Equals                   Method     bool Equals(System.Object obj)
ExitNestedPrompt        Method     System.Void ExitNestedPrompt()
GetHashCode             Method     int GetHashCode()
GetType                  Method     type GetType()
NotifyBeginApplication  Method     System.Void NotifyBeginApplication()
NotifyEndApplication    Method     System.Void NotifyEndApplication()
PopRunspace             Method     System.Void PopRunspace()
PushRunspace            Method     System.Void PushRunspace(runspace runspace)
SetShouldExit            Method     System.Void SetShouldExit(int exitCode)
ToString                 Method     string ToString()

過濾內(nèi)部方法

Get-Memeber列出了一個(gè)對象定義的所有方法,但并不是所有的方法都有用,有些方法的的用處非常有限。

Get_ 和 Set_ 方法

所有名稱以”get_”打頭的方法都是為了給對應(yīng)的屬性返回一個(gè)值。例如”get_someInfo()”方法的作用就是返回屬性someInfo的值,因此可以直接通過屬性調(diào)用。

復(fù)制代碼 代碼如下:

PS C:Powershell> $Host.Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

PS C:Powershell> $Host.get_Version()

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      -1     -1

類似的象”set_someinfo”一樣,該方法只是為了給屬性someinfo賦值,可以直接通過屬性賦值調(diào)用。如果一個(gè)對象中只有”get_someinfo”,沒有對應(yīng)的”set_someinfo”,說明someinfo這個(gè)屬性為只讀屬性。

標(biāo)準(zhǔn)方法

幾乎每個(gè)對象都有一些繼承自父類的方法,這些方法并不是該對象所特有的方法,而是所有對象共有的方法。
Equals 比較兩個(gè)對象是否相同
GetHashCode 返回一個(gè)對象的數(shù)字格式的指紋
GetType 返回一個(gè)對象的數(shù)據(jù)類型
ToString 將一個(gè)對象轉(zhuǎn)換成可讀的字符串

過濾包含了下劃線的方法可是使用操作符 -notlike 和 通配符 *

復(fù)制代碼 代碼如下:

PS C:Powershell> $Host.UI.RawUI | Get-Member -me method | where {$_.Name -notlike '*_*'}

   TypeName: System.Management.Automation.Internal.Host.InternalHostRawUserInterface

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
FlushInputBuffer      Method     System.Void FlushInputBuffer()
GetBufferContents    Method     System.Management.Automation.Host.BufferCell[,] GetBufferCo
GetHashCode           Method     int GetHashCode()
GetType               Method     type GetType()
LengthInBufferCells  Method     int LengthInBufferCells(string str), int LengthInBufferCell
NewBufferCellArray  Method     System.Management.Automation.Host.BufferCell[,] NewBufferCe
ReadKey               Method     System.Management.Automation.Host.KeyInfo ReadKey(System.Ma
ScrollBufferContents Method     System.Void ScrollBufferContents(System.Management.Automati
SetBufferContents    Method     System.Void SetBufferContents(System.Management.Automation.
ToString              Method     string ToString()

調(diào)用方法

一定要注意,在調(diào)用一個(gè)方法前,必須知道這個(gè)方法的功能。因?yàn)橛械拿羁赡鼙容^危險(xiǎn),例如錯(cuò)誤地修改環(huán)境變量。調(diào)用一個(gè)方法,通過圓點(diǎn)加圓括號:
$Host.GetType()

調(diào)用帶參數(shù)的方法

UI對象有很多實(shí)用的方法,可以通過get-member預(yù)覽

復(fù)制代碼 代碼如下:

PS C:Powershell> $Host.UI | Get-Member -MemberType method

   TypeName: System.Management.Automation.Internal.Host.InternalHostUserInterface

Name                   MemberType Definition
----                   ---------- ----------
Equals                 Method     bool Equals(System.Object obj)
GetHashCode            Method     int GetHashCode()
GetType                Method     type GetType()
Prompt                 Method     System.Collections.Generic.Dictionary[string,psob
PromptForChoice        Method     int PromptForChoice(string caption, string messag
PromptForCredential    Method     System.Management.Automation.PSCredential PromptF
ReadLine                Method     string ReadLine()
ReadLineAsSecureString Method     System.Security.SecureString ReadLineAsSecureStri
ToString                Method     string ToString()
Write  Method     System.Void Write(string value), System.Void Writ
WriteDebugLine        Method     System.Void WriteDebugLine(string message)
WriteErrorLine          Method     System.Void WriteErrorLine(string value)
WriteLine               Method     System.Void WriteLine(), System.Void WriteLine(Sy
WriteProgress           Method     System.Void WriteProgress(long sourceId, System.M
WriteVerboseLine      Method     System.Void WriteVerboseLine(string message)
WriteWarningLine      Method     System.Void WriteWarningLine(string message)

哪一個(gè)參數(shù)是必須的
從列表中篩選出一個(gè)方法,再通過Get-Member得到更多的信息。

復(fù)制代碼 代碼如下:

PS C:Powershell> $info=$Host.UI |  Get-Member WriteDebugLine
PS C:Powershell> $info

   TypeName: System.Management.Automation.Internal.Host.InternalHostUserInterface

Name           MemberType Definition
----           ---------- ----------
WriteDebugLine Method     System.Void WriteDebugLine(string message)

PS C:Powershell> $info.Definition
System.Void WriteDebugLine(string message)

Definition屬性告訴你怎樣調(diào)用一個(gè)方法,每一個(gè)方法的定義都會返回一個(gè)Objec對象,System.Void 是一個(gè)特殊的類型,代表什么都沒有,即返回值為空。
接下來就可以根據(jù)函數(shù)的定義,給它傳進(jìn)合適的參數(shù)調(diào)用了。

復(fù)制代碼 代碼如下:

PS C:Powershell> $Host.UI.WriteDebugLine("Hello 2012 !")
調(diào)試: Hello 2012 !

低級函數(shù)

上述的WriteDebugLine()函數(shù)并沒有什么特別。事實(shí)上所謂的$Host中的很多方法只不過是一些簡單的Cmdlets命令。例如使用如下cmdlet輸出一條調(diào)試通知

復(fù)制代碼 代碼如下:

PS C:Powershell> Write-Debug "Hello 2012 !"
PS C:Powershell> Write-Debug -Message "Hello 2012 !"

上述的命令并沒有輸出黃色的調(diào)試信息,這和$DebugPreference配置有關(guān),因?yàn)?DebugPreference的默認(rèn)值為:SilentlyContinue。
當(dāng)$DebugPreference為Stop,Continue,Inquire時(shí)就會輸出調(diào)試消息:

復(fù)制代碼 代碼如下:

PS C:Powershell> [System.Enum]::GetNames([System.Management.Automation.ActionPreference])
SilentlyContinue
Stop
Continue
Inquire
PS C:Powershell> $DebugPreference="stop"
PS C:Powershell> Write-Debug "Hello 2012"
調(diào)試: Hello 2012
Write-Debug : 已停止執(zhí)行命令,因?yàn)槭走x項(xiàng)變量“DebugPreference”或通用參數(shù)被設(shè)置為 Stop。
所在位置 行:1 字符: 12
+ Write-Debug <<<<  "Hello 2012"     + CategoryInfo          : OperationStopped: (:) [Write-Debug], ParentContainsErrorRecordException     + FullyQualifiedErrorId : ActionPreferenceStop,Microsoft.PowerShell.Commands.WriteDebugCommand PS C:Powershell> $DebugPreference="continue"
PS C:Powershell> Write-Debug "Hello 2012"
調(diào)試: Hello 2012

WriteErrorLine,WriteVerboseLine,WriteWarningLine的情況也類似。如果你不想受$DebugPreference配置的依賴,輸出錯(cuò)誤消息可以直接使用 $host.UI.WriteDebugLine()方法。

多個(gè)方法的簽名

有些方法名相同,可以接受不同類型或者不同個(gè)數(shù)的參數(shù),如何查看一個(gè)方法支持的所有簽名 ,使用Get-Member獲取方法對象,然后查看Definition屬性。

復(fù)制代碼 代碼如下:

PS C:Powershell> $method
PS C:Powershell> $method=$Host.UI | Get-Member WriteLine
PS C:Powershell> $method.Definition
System.Void WriteLine(), System.Void WriteLine(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor
, string value), System.Void WriteLine(string value)

但是Definition的輸出閱讀不方便,可是稍加潤色。

復(fù)制代碼 代碼如下:

PS C:Powershell> $method.Definition.Replace("),",")`n")
System.Void WriteLine()
System.Void WriteLine(System.ConsoleColor foregroundColor, System.ConsoleColor backgroundColor, string value)
System.Void WriteLine(string value)

創(chuàng)建選擇菜單

這里需要使用$host.UI.PromptForChoice()方法,先查看方法的定義:

復(fù)制代碼 代碼如下:

PS C:Powershell> $host.ui.PromptForChoice

MemberType          : Method
OverloadDefinitions : {int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection[Sy
                      stem.Management.Automation.Host.ChoiceDescription] choices, int defaultChoice), System.Collection
                      s.ObjectModel.Collection[int] PromptForChoice(string caption, string message, System.Collections.
                      ObjectModel.Collection[System.Management.Automation.Host.ChoiceDescription] choices, System.Colle
                      ctions.Generic.IEnumerable[int] defaultChoices)}
TypeNameOfValue     : System.Management.Automation.PSMethod
Value               : int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection[Sys
                      tem.Management.Automation.Host.ChoiceDescription] choices, int defaultChoice), System.Collections
                      .ObjectModel.Collection[int] PromptForChoice(string caption, string message, System.Collections.O
                      bjectModel.Collection[System.Management.Automation.Host.ChoiceDescription] choices, System.Collec
                      tions.Generic.IEnumerable[int] defaultChoices)
Name                : PromptForChoice
IsInstance          : True

下面的腳本演示如何創(chuàng)建選擇菜單:

復(fù)制代碼 代碼如下:

$SwitchUser = ([System.Management.Automation.Host.ChoiceDescription]"&Switchuser")
$LoginOff = ([System.Management.Automation.Host.ChoiceDescription]"&LoginOff")
$Lock= ([System.Management.Automation.Host.ChoiceDescription]"&Lock")
$Reboot= ([System.Management.Automation.Host.ChoiceDescription]"&Reboot")
$Sleep= ([System.Management.Automation.Host.ChoiceDescription]"&Sleep")

$selection = [System.Management.Automation.Host.ChoiceDescription[]]($SwitchUser,$LoginOff,$Lock,$Reboot,$Sleep)
$answer=$Host.UI.PromptForChoice('接下來做什么事呢?','請選擇:',$selection,1)
"您選擇的是:"
switch($answer)
{
0 {"切換用戶"}
1 {"注銷"}
2 {"鎖定"}
3 {"重啟"}
4 {"休眠"}
}

復(fù)制代碼 代碼如下:

PS C:PowerShell> .test.ps1
接下來做什么事呢?
請選擇:
[S] Switchuser  [L] LoginOff  [L] Lock  [R] Reboot  [S] Sleep  [?] 幫助 (默認(rèn)值為“L”): Reboot
您選擇的是:
重啟

相關(guān)文章

  • Powershell小技巧之創(chuàng)建短網(wǎng)址

    Powershell小技巧之創(chuàng)建短網(wǎng)址

    短網(wǎng)址服務(wù),可能很多朋友都已經(jīng)不再陌生,特別是在微博應(yīng)用中十分普遍,比如,當(dāng)我們在騰訊、新浪微博發(fā)微博時(shí)有時(shí)發(fā)很長的網(wǎng)址連接,但由于微博只限制140個(gè)字,所以微博就自動把您發(fā)的長網(wǎng)址給轉(zhuǎn)換成短網(wǎng)址了。今天我們來探討下,如何用powershell來便捷的使用短網(wǎng)址
    2014-10-10
  • Powershell腳本的4種執(zhí)行權(quán)限介紹

    Powershell腳本的4種執(zhí)行權(quán)限介紹

    這篇文章主要介紹了Powershell腳本的4種執(zhí)行權(quán)限介紹,Windows默認(rèn)不允許任何腳本運(yùn)行,你可以使用"Set-ExecutionPolicy"cmdlet來改變的你PowerShell環(huán)境,共有4種運(yùn)行權(quán)限,需要的朋友可以參考下
    2015-06-06
  • Tornado中database模塊被取消的替代方法

    Tornado中database模塊被取消的替代方法

    這篇文章主要介紹了Tornado中database模塊被取消的替代方法,新的方法是使用torndb模塊,需要的朋友可以參考下
    2014-08-08
  • Powershell小技巧之找出腳本中的錯(cuò)誤

    Powershell小技巧之找出腳本中的錯(cuò)誤

    這篇文章主要介紹了使用Powershell進(jìn)行篩選然后你可以快速掃描一個(gè)目錄或整個(gè)電腦中的PS文件找出他們中的語法錯(cuò)誤。
    2014-09-09
  • WMI入門教程之怎么使用WMI?

    WMI入門教程之怎么使用WMI?

    這篇文章主要介紹了WMI入門教程之怎么使用WMI?本文講解了在軟件中、PowerShell中、.NET中使用WMI的例子,需要的朋友可以參考下
    2014-10-10
  • Windows Powershell For 循環(huán)

    Windows Powershell For 循環(huán)

    這篇文章主要介紹了Windows Powershell For 循環(huán)的定義、用法以及示例,非常簡單實(shí)用,有需要的朋友可以參考下
    2014-10-10
  • Powershell批量給文件增加前輟實(shí)例

    Powershell批量給文件增加前輟實(shí)例

    這篇文章主要介紹了Powershell批量給文件增加前輟實(shí)例,即Powershell批量修改文件名,文件名中統(tǒng)一設(shè)定一個(gè)帶數(shù)字的前輟,需要的朋友可以參考下
    2014-06-06
  • 使用PowerShell獲取Trustedinstaller權(quán)限的問題

    使用PowerShell獲取Trustedinstaller權(quán)限的問題

    這篇文章主要介紹了使用PowerShell獲取Trustedinstaller權(quán)限,獲取到?Trustedinstaller?權(quán)限,就可以通過一些命令來修改系統(tǒng)文件了,本文給大家詳細(xì)講解,需要的朋友可以參考下
    2023-01-01
  • PowerShell使用Clear-Content命令刪除、清空文件內(nèi)容的例子

    PowerShell使用Clear-Content命令刪除、清空文件內(nèi)容的例子

    這篇文章主要介紹了PowerShell使用Clear-Content命令刪除、清空文件內(nèi)容的例子,并對Clear-Content作了介紹,需要的朋友可以參考下
    2014-08-08
  • Powershell小技巧之用變量累積記錄錯(cuò)誤

    Powershell小技巧之用變量累積記錄錯(cuò)誤

    這篇文章主要介紹了Powershell用變量累積記錄錯(cuò)誤的小技巧,非常的簡單實(shí)用,有需要的朋友可以參考下
    2014-10-10

最新評論