Windows Powershell屬性:描述對(duì)象是什么
屬性可以描述一個(gè)對(duì)象,對(duì)象的屬性可以被Powershell自動(dòng)轉(zhuǎn)換成文本,并且輸出到控制臺(tái)。因此可以通過這種方法查看任何對(duì)象,例如$host:
PS C:Powershell> $host
Name : ConsoleHost
Version : 2.0
InstanceId : 7fefa1fa-fb2e-47c7-a867-c13b123da5c2
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
InternalHost對(duì)象存儲(chǔ)在$host變量中,包含9個(gè)屬性。輸出的第一列為對(duì)象的屬性,第二列為文本形式的屬性值。例如要查看當(dāng)前Powershell的版本號(hào),可以訪問$host對(duì)象的Version屬性:
PS C:Powershell> $host.Version
Major Minor Build Revision
----- ----- ----- --------
2 0 -1 -1
由此可知,Version并不是以一串單獨(dú)的數(shù)字存儲(chǔ)的,它本身也是一個(gè)對(duì)象,包含 Major,Minor,Build,Revision四個(gè)屬性,可以查看Version的具體類型,也可以訪問它的每一個(gè)屬性:
PS C:Powershell> $Host.Version.GetType().FullName
System.Version
PS C:Powershell> $Host.Version.Build
-1
PS C:Powershell> $Host.Version.Major
2
PS C:Powershell> $Host.Version.MajorRevision
-1
PS C:Powershell> $Host.Version.Revision
-1
查看一個(gè)對(duì)象的類型很實(shí)用,因?yàn)榭梢酝ㄟ^這個(gè)類型構(gòu)造新的對(duì)象或者進(jìn)行類型轉(zhuǎn)換等等。
PS C:Powershell> [System.Version]'2012.12.20.4444'
Major Minor Build Revision
----- ----- ----- --------
2012 12 20 4444
例如CurrentCulture屬性,可以通過$host的CurrentCulture訪問當(dāng)前系統(tǒng)的本地化信息和該信息的類型:
PS C:Powershell> $Host.CurrentCulture
LCID Name DisplayName
---- ---- -----------
2052 zh-CN 中文(中華人民共和國(guó))
PS C:Powershell> $Host.CurrentCulture.GetType().FullName
System.Globalization.CultureInfo
CurrentCulture包含3個(gè)屬性,LCID, Name, and DisplayName。通過MSDN查看System.Globalization.CultureInfo的構(gòu)造函數(shù)可知,可以將國(guó)家代碼和國(guó)家名稱標(biāo)志字符串轉(zhuǎn)換成一個(gè)新的CultureInfo對(duì)象。
PS C:Powershell> [System.Globalization.CultureInfo]'zh-cn'
LCID Name DisplayName
---- ---- -----------
2052 zh-CN 中文(中華人民共和國(guó))
PS C:Powershell> [System.Globalization.CultureInfo]'zh-tw'
LCID Name DisplayName
---- ---- -----------
1028 zh-TW 中文(臺(tái)灣)
PS C:Powershell> [System.Globalization.CultureInfo]'en-us'
LCID Name DisplayName
---- ---- -----------
1033 en-US 英語(美國(guó))
PS C:Powershell> [System.Globalization.CultureInfo] 55
LCID Name DisplayName
---- ---- -----------
55 ka 格魯吉亞語
PS C:Powershell> [System.Globalization.CultureInfo] 1
LCID Name DisplayName
---- ---- -----------
1 ar 阿拉伯語
PS C:Powershell> [System.Globalization.CultureInfo] 33
LCID Name DisplayName
---- ---- -----------
33 id 印度尼西亞語
屬性中包含對(duì)象
一個(gè)對(duì)象的屬性用來存儲(chǔ)數(shù)據(jù),反過來這些數(shù)據(jù)又可以存儲(chǔ)其它對(duì)象。$host有兩個(gè)比較特別的屬性UI和PrivateData。把$host對(duì)象輸出到控制臺(tái)上后,除了UI和PrivateData所有的屬性都會(huì)被轉(zhuǎn)換成確定的文本:
PS C:Powershell> $Host
Name : ConsoleHost
Version : 2.0
InstanceId : 7fefa1fa-fb2e-47c7-a867-c13b123da5c2
UI : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : zh-CN
CurrentUICulture : zh-CN
PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace : System.Management.Automation.Runspaces.LocalRunspace
原因是這兩個(gè)屬性中又包含了一個(gè)對(duì)象:
PS C:Powershell> $Host.UI
RawUI
-----
System.Management.Automation.Internal.Host.InternalHostRawUserInterface
PS C:Powershell> $Host.UI.RawUI
ForegroundColor : DarkYellow
BackgroundColor : DarkMagenta
CursorPosition : 0,23
WindowPosition : 0,0
CursorSize : 25
BufferSize : 100,200
WindowSize : 100,61
MaxWindowSize : 100,62
MaxPhysicalWindowSize : 160,62
KeyAvailable : False
WindowTitle : Windows PowerShell
“RawUI” 為 “Raw User Interface” 提供了配置Powershell控制臺(tái)用戶界面的接口。上面的屬性可以讀取,但是個(gè)別卻不能更改。
只讀屬性和讀寫屬性
屬性可以準(zhǔn)確的描述對(duì)象,一旦屬性更改了。這一更改也會(huì)體現(xiàn)在對(duì)象上。如果不能更改,屬性就是“只讀”屬性。
通過簡(jiǎn)單地修改控制臺(tái)的背景和前景的顏色,可以發(fā)現(xiàn)屬性更改可以直接反映到對(duì)象上。
PS C:Powershell> $host.ui.rawui.BackgroundColor = "Green"
PS C:Powershell> $host.ui.rawui.ForegroundColor = "White"
PS C:Powershell> cls
有的屬性不能更改,如果嘗試修改,就會(huì)拋出異常。
PS C:Powershell> $Host.UI.RawUI.KeyAvailable
False
PS C:Powershell> $Host.UI.RawUI.KeyAvailable=$false
“KeyAvailable”為 ReadOnly 屬性。
所在位置 行:1 字符: 16
+ $Host.UI.RawUI. <<<< KeyAvailable=$false
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
控制臺(tái)是否接收到了一個(gè)按鍵請(qǐng)求,應(yīng)當(dāng)取決于用戶的操作,因此該屬性拒絕被更改,你只能讀取它。
RawUI的屬性
ForegroundColor:前景色
BackgroundColor:背景色
CursorPosition:光標(biāo)的位置
WindowPosition:窗口的位置
CursorSize:光標(biāo)的大小
BufferSize:緩沖區(qū)的大小
WindowSize:窗口的大小
MaxWindowSize:允許窗口的最大值
MaxPhysicalWindowSize:窗口可能的最大值
KeyAvailable:是否存在按鍵
WindowTitle:窗口的標(biāo)題
屬性的類型
有些屬性只接受整數(shù)值,例如控制臺(tái)光標(biāo)的大小,值域在0-100,用來控制關(guān)閉大小的百分比??梢詫⒐鈽?biāo)設(shè)置為75%,但是不能超過100%,否則就會(huì)產(chǎn)生錯(cuò)誤。
PS C:Powershell> $Host.UI.RawUI.CursorSize=75
PS C:Powershell> $Host.UI.RawUI.CursorSize=101
設(shè)置“CursorSize”時(shí)發(fā)生異常:“無法處理 CursorSize,因?yàn)橹付ǖ墓鈽?biāo)大小無效。
參數(shù)名: value
實(shí)際值是 101?!?br />
所在位置 行:1 字符: 16
+ $Host.UI.RawUI. <<<< CursorSize=101
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
另一個(gè)屬性ForegoundColor的類型為Color枚舉值。因此給ForegoundColor所賦的值必須是已經(jīng)在System.ConsoleColor中定義過的??梢詫ⅰ癇lack”但是不能使用“Pink”
PS C:Powershell> $Host.UI.RawUI.ForegroundColor="Black"
PS C:Powershell> $Host.UI.RawUI.ForegroundColor="Pink"
設(shè)置“ForegroundColor”時(shí)發(fā)生異常:“由于枚舉值無效,無法將值“Pink”轉(zhuǎn)換為類型“System.ConsoleColor
”。請(qǐng)指定以下枚舉值之一,然后重試??赡艿拿杜e值為“Black、DarkBlue、DarkGreen、DarkCyan、DarkRed、
DarkMagenta、DarkYellow、Gray、DarkGray、Blue、Green、Cyan、Red、Magenta、Yellow、White”?!?br /> 所在位置 行:1 字符: 16
+ $Host.UI.RawUI. <<<< ForegroundColor="Pink"
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
可以使用[System.Enum]::GetNames 方法查看ConsoleColor定義的所有顏色。
PS C:Powershell> [System.Enum]::GetNames([System.ConsoleColor])
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
有時(shí)一個(gè)屬性期望的賦值必須是一個(gè)指定類型的對(duì)象。例如WindowSize,如果想改變Powershell的窗口大小,可是設(shè)置WindowSize屬性,但是它是一個(gè)System.Management.Automation.Host.Size對(duì)象,怎樣獲取這個(gè)對(duì)象呢?
1.先讀取屬性,保存為臨時(shí)變量,更改臨時(shí)變量,將臨時(shí)變量賦給WindowSize
2.直接創(chuàng)建一個(gè)System.Management.Automation.Host.Size,賦給WindowSize
PS C:Powershell> $tmp=$Host.UI.RawUI.WindowSize
PS C:Powershell> $tmp
Width Height
----- ------
100 60
PS C:Powershell> $tmp.Height=30
PS C:Powershell> $tmp.Width=60
PS C:Powershell> $Host.UI.RawUI.WindowSize=$tmp
Width Height
----- ------
60 30
PS C:Powershell> $Host.UI.RawUI.WindowSize=New-Object System.Management.Automation.Host.Size(60,40)
PS C:Powershell> $Host.UI.RawUI.WindowSize
Width Height
----- ------
60 40
查看所有屬性
因?yàn)閷傩院头椒ǘ际菍?duì)象的成員,可以使用Get-Member可以返回它們的成員的詳細(xì)信息,如果只顯示屬性可以使用參數(shù) memberType 為“Property”
PS C:Powershell> $host | Get-Member -memberType property
TypeName: System.Management.Automation.Internal.Host.InternalHost
Name MemberType Definition
---- ---------- ----------
CurrentCulture Property System.Globalization.CultureInfo CurrentCulture {get;}
CurrentUICulture Property System.Globalization.CultureInfo CurrentUICulture {get;}
InstanceId Property System.Guid InstanceId {get;}
IsRunspacePushed Property System.Boolean IsRunspacePushed {get;}
Name Property System.String Name {get;}
PrivateData Property System.Management.Automation.PSObject PrivateData {get;}
Runspace Property System.Management.Automation.Runspaces.Runspace Runspace {get;}
UI Property System.Management.Automation.Host.PSHostUserInterface UI {get;}
Version Property System.Version Version {get;}
在Name列,可以看到$host支持的所有屬性。在Definition列首先列出屬性的具體類型,然后列出構(gòu)造器,如果一個(gè)構(gòu)造器中只有Get方法,沒有Set方法,表示該屬性為只讀屬性。
相關(guān)文章
PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動(dòng)獲取IP地址、設(shè)置靜態(tài)IP地址的方法
這篇文章主要介紹了PowerShell腳本實(shí)現(xiàn)網(wǎng)卡DHCP自動(dòng)獲取IP地址、設(shè)置靜態(tài)IP地址的方法,本文同時(shí)講解了用PowerShell設(shè)置網(wǎng)卡DHCP、靜態(tài)IP的方法,需要的朋友可以參考下2014-08-08PowerShell中的強(qiáng)類型數(shù)組介紹
這篇文章主要介紹了PowerShell中的強(qiáng)類型數(shù)組介紹,強(qiáng)類型數(shù)組可以理解為強(qiáng)制數(shù)據(jù)類型的數(shù)組,也就是一個(gè)數(shù)組里只包含一種數(shù)據(jù)類型,需要的朋友可以參考下2014-08-08PowerShell腳本中查看網(wǎng)卡的高級(jí)屬性
這篇文章主要介紹了PowerShell腳本中查看網(wǎng)卡的高級(jí)屬性,本文直接給出實(shí)現(xiàn)代碼和運(yùn)行結(jié)果,需要的朋友可以參考下2014-11-11Powershell小技巧之使用Update-TypeData擴(kuò)展類型系統(tǒng)
昨天演示獲取時(shí)辰《PowerShell獲取當(dāng)前的時(shí)辰 》,并不只是為了獲取時(shí)辰,還為了使用Update-TypeData命令將時(shí)辰這一屬性,牢牢地幫到DateTime類型上。2014-09-09PowerShell創(chuàng)建Byte數(shù)組例子
這篇文章主要介紹了PowerShell創(chuàng)建Byte數(shù)組例子,Byte數(shù)組即字節(jié)數(shù)組,它是一種強(qiáng)類型的數(shù)組,需要的朋友可以參考下2014-08-08PowerShell函數(shù)參數(shù)使用智能提示功能例子
這篇文章主要介紹了PowerShell函數(shù)參數(shù)使用智能提示功能例子,這個(gè)功能一般需要在PowerShell集成開發(fā)環(huán)境ISE中才會(huì)顯示智能提示,需要的朋友可以參考下2014-07-07PowerShell中使用正則表達(dá)式篩選數(shù)組實(shí)例
這篇文章主要介紹了PowerShell中使用正則表達(dá)式篩選數(shù)組實(shí)例,使用match操作符配合相應(yīng)正則表達(dá)式實(shí)現(xiàn)數(shù)組篩選功能,需要的朋友可以參考下2014-07-07