校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Windows Powershell方法(對象能做什么)

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

熱門標簽:莫拉克電梯系統外呼怎么設置 如何根據經緯度百度地圖標注 六寸地圖標注點怎么刪除 地圖標注的圖案 騰訊地圖標注中心怎么標注 印臺區呼叫中心外呼系統 新鄭電銷機器人一個月多少錢 電話機器人公司招聘 萬全縣地圖標注app

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

復制代碼 代碼如下:

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()

過濾內部方法

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

Get_ 和 Set_ 方法

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

復制代碼 代碼如下:

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賦值,可以直接通過屬性賦值調用。如果一個對象中只有”get_someinfo”,沒有對應的”set_someinfo”,說明someinfo這個屬性為只讀屬性。

標準方法

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

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

復制代碼 代碼如下:

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()

調用方法

一定要注意,在調用一個方法前,必須知道這個方法的功能。因為有的命令可能比較危險,例如錯誤地修改環境變量。調用一個方法,通過圓點加圓括號:
$Host.GetType()

調用帶參數的方法

UI對象有很多實用的方法,可以通過get-member預覽

復制代碼 代碼如下:

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)

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

復制代碼 代碼如下:

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屬性告訴你怎樣調用一個方法,每一個方法的定義都會返回一個Objec對象,System.Void 是一個特殊的類型,代表什么都沒有,即返回值為空。
接下來就可以根據函數的定義,給它傳進合適的參數調用了。

復制代碼 代碼如下:

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

低級函數

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

復制代碼 代碼如下:

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

上述的命令并沒有輸出黃色的調試信息,這和$DebugPreference配置有關,因為$DebugPreference的默認值為:SilentlyContinue。
當$DebugPreference為Stop,Continue,Inquire時就會輸出調試消息:

復制代碼 代碼如下:

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"
調試: Hello 2012
Write-Debug : 已停止執行命令,因為首選項變量“DebugPreference”或通用參數被設置為 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"
調試: Hello 2012

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

多個方法的簽名

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

復制代碼 代碼如下:

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的輸出閱讀不方便,可是稍加潤色。

復制代碼 代碼如下:

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)

創建選擇菜單

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

復制代碼 代碼如下:

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

下面的腳本演示如何創建選擇菜單:

復制代碼 代碼如下:

$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 {"休眠"}
}

復制代碼 代碼如下:

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

您可能感興趣的文章:
  • Windows Powershell調用靜態方法
  • Windows Powershell屬性:描述對象是什么
  • Windows Powershell對象=屬性+方法
  • Windows Powershell創建對象

標簽:臨汾 南昌 湘潭 喀什 疫苗接種 襄陽 汕頭 天水

巨人網絡通訊聲明:本文標題《Windows Powershell方法(對象能做什么)》,本文關鍵詞  Windows,Powershell,方法,對象,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Windows Powershell方法(對象能做什么)》相關的同類信息!
  • 本頁收集關于Windows Powershell方法(對象能做什么)的相關信息資訊供網民參考!
  • 推薦文章
    校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃
    午夜精品福利一区二区三区av| 中文字幕不卡三区| 欧美精品一区二区三区蜜臀| 亚洲不卡一区二区三区| 欧美日韩中文字幕一区二区| 日韩专区在线视频| www国产成人免费观看视频 深夜成人网| 日韩精品久久理论片| 亚洲欧美在线观看| 国内成+人亚洲+欧美+综合在线| 日韩午夜在线观看视频| 久久精品国产亚洲a| 欧美大片在线观看| 成人白浆超碰人人人人| 亚洲人成人一区二区在线观看| 色婷婷综合久色| 日本一不卡视频| 国产亚洲短视频| 欧美亚洲一区二区三区四区| 伦理电影国产精品| 亚洲欧洲三级电影| 51精品国自产在线| 国产精品亚洲第一区在线暖暖韩国| 亚洲视频在线一区二区| 日韩精品综合一本久道在线视频| 成人av网址在线观看| 免费成人小视频| 亚洲网友自拍偷拍| 国产精品人妖ts系列视频| 欧美男女性生活在线直播观看 | 欧美剧在线免费观看网站| 狠狠狠色丁香婷婷综合激情| 一区二区三区丝袜| 国产欧美日韩另类视频免费观看| 欧美日韩大陆一区二区| av电影天堂一区二区在线| 国产精品99久久久| 国产美女一区二区| 激情五月激情综合网| 毛片av一区二区| 日韩高清在线电影| 亚洲444eee在线观看| 亚洲理论在线观看| 综合激情网...| 最新欧美精品一区二区三区| 久久免费电影网| 欧美精品一区二区在线观看| 日韩一级二级三级精品视频| 这里只有精品电影| 欧美一级免费大片| 欧美电影免费观看完整版| 欧美日本视频在线| 国内外精品视频| 亚洲精品免费看| 国产精品国产馆在线真实露脸| 欧美男女性生活在线直播观看| 91啪亚洲精品| 99re热这里只有精品视频| 色综合夜色一区| 韩国精品一区二区| 久久免费看少妇高潮| 午夜精品久久久久久久99水蜜桃| 成人精品小蝌蚪| 韩国女主播成人在线| 亚洲色欲色欲www| 92国产精品观看| 日韩专区一卡二卡| 国产亚洲精品7777| 91黄色在线观看| 舔着乳尖日韩一区| 日韩女优制服丝袜电影| 美女尤物国产一区| 亚洲综合在线视频| 一区二区三区在线视频播放| 亚洲少妇屁股交4| 欧美午夜精品一区| 91亚洲精品一区二区乱码| 日韩一区二区三区在线观看| 国产高清在线观看免费不卡| 日韩激情一区二区| 日韩一区精品字幕| 久久er99热精品一区二区| 精品在线免费观看| 韩国视频一区二区| 国产成人av电影在线| 粉嫩aⅴ一区二区三区四区五区| 成人毛片老司机大片| 欧美性猛交一区二区三区精品| 欧美蜜桃一区二区三区| 欧美三级三级三级爽爽爽| 欧美日韩综合一区| 成人av免费在线播放| 国内精品伊人久久久久av一坑 | 在线播放视频一区| 欧美色图一区二区三区| 日韩中文字幕区一区有砖一区| 欧洲中文字幕精品| 高清久久久久久| 成人av在线资源网站| 久久综合九色欧美综合狠狠| 欧美在线你懂得| 日韩一区二区三区视频| 狠狠色丁香九九婷婷综合五月| 欧美国产欧美亚州国产日韩mv天天看完整| 婷婷开心激情综合| 国产91综合网| 日韩精品中文字幕一区二区三区| 国产精品久久久久一区| 美女视频黄a大片欧美| 91国内精品野花午夜精品| 久久精品视频网| 久久精品国产成人一区二区三区| 在线观看视频一区| 亚洲乱码中文字幕| 色综合激情五月| 国产精品久久国产精麻豆99网站| 日本色综合中文字幕| 欧美三级电影在线观看| 椎名由奈av一区二区三区| 国产高清不卡一区| 久久精品亚洲精品国产欧美kt∨| 久久精品二区亚洲w码| 日韩一区二区三区观看| 亚洲成人动漫精品| 欧美日韩激情在线| 亚洲制服丝袜在线| 日韩电影免费在线| 久久精品人人爽人人爽| 日韩精品高清不卡| 国产午夜精品久久久久久免费视| 成人综合婷婷国产精品久久| 亚洲制服丝袜av| 国产精品女人毛片| 欧美精品一区二区三区蜜桃视频 | 精品播放一区二区| 亚洲精品五月天| 在线观看视频一区二区欧美日韩 | 国产日韩高清在线| 国产麻豆视频一区| 国产精品美女www爽爽爽| 成人免费精品视频| 一区二区三区色| 欧美精品一二三四| 九九精品一区二区| 国产精品卡一卡二| 在线视频亚洲一区| 麻豆91免费观看| 亚洲欧洲99久久| 在线看日本不卡| 老司机午夜精品| 日韩美女视频一区二区 | 欧美片网站yy| 国产一区二区三区观看| 亚洲日本中文字幕区| 欧美日韩精品欧美日韩精品一| 日韩专区一卡二卡| 中文字幕永久在线不卡| 777a∨成人精品桃花网| 国产精品12区| 亚洲成av人片一区二区梦乃| 精品国产乱码久久久久久1区2区| 成人美女视频在线观看18| 石原莉奈一区二区三区在线观看| 精品久久久久久久久久久院品网| 99精品视频在线观看免费| 婷婷一区二区三区| 国产精品久久久久婷婷二区次| 欧美精品一二三区| 91在线观看视频| 黄色日韩三级电影| 夜夜亚洲天天久久| 国产午夜亚洲精品不卡| 欧美伦理影视网| a在线欧美一区| 成人黄色a**站在线观看| 成年人国产精品| 亚洲成a天堂v人片| 久久久99精品免费观看| 欧美日韩精品久久久| www.亚洲色图| 蜜桃一区二区三区四区| 一区二区免费在线播放| 久久久99久久精品欧美| 欧美日韩精品欧美日韩精品| 91网站在线观看视频| 秋霞成人午夜伦在线观看| 国产精品国产精品国产专区不蜜| 欧美一区二区三区日韩| 欧美性感一类影片在线播放| 国产99一区视频免费| 久久精品99国产国产精| 午夜精彩视频在线观看不卡| 亚洲女与黑人做爰| www.欧美.com| 99精品欧美一区二区蜜桃免费| 亚洲成人综合网站| 国产精品久久久久久久久快鸭| 欧美日韩精品电影| 欧美在线观看视频在线| av不卡在线播放|