並非所有的 .NET Framework 類別都可使用 New-Object 建立。例如,若您嘗試使用 New-Object 建立 System.EnvironmentSystem.Math 物件,將出現下列錯誤訊息:

PS> New-Object System.Environment
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Environment.
At line:1 char:11
+ New-Object  <<<< System.Environment
PS> New-Object System.Math
New-Object : Constructor not found. Cannot find an appropriate constructor for
type System.Math.
At line:1 char:11
+ New-Object  <<<< System.Math

發生上列錯誤是因為不能從這些類別建立新物件。這些類別是始終都不會變更狀態的方法和屬性參照程式庫。您可以直接取用而不需要建立物件。像這樣的類別和方法稱為「靜態類別」(Static Class),因為您無法予以建立、終結或變更。為了釐清觀念,底下將提供幾個使用靜態類別的範例。

使用 System.Environment 取得環境資料

使用 Windows PowerShell 物件的第一步通常是利用 Get-Member 找出物件包含的成員。靜態類別的處理方式則稍有不同,因為實際的類別並非物件。

參照 System.Environment 靜態類別

將類別名稱置於方括號中即可參照靜態類別。例如,您可將名稱 System.Environment 輸入括號中以參照此類別。這樣就會顯示若干一般型別資訊:

PS> [System.Environment]

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    Environment                              System.Object
附註:

如前所述,當您使用 New-Object 時,Windows PowerShell 會自動在型別名稱前面加上 'System.'。使用加括號的型別名稱時也是同樣的情況,因此您可以將 [System.Environment] 指定成 [Environment]

System.Environment 類別包含目前處理序的工作環境一般資訊;以 Windows PowerShell 為例,也就是 powershell.exe 處理序。

如果您試著輸入 [System.Environment] | Get-Member 以檢視此類別的詳細資料,會發現物件類型是 System.RuntimeType 而非 System.Environment

PS> [System.Environment] | Get-Member

   TypeName: System.RuntimeType

若要使用 Get-Member 檢視靜態成員,請指定 Static 參數:

PS> [System.Environment] | Get-Member -Static


   TypeName: System.Environment

Name                       MemberType Definition
----                       ---------- ----------
Equals                     Method     static System.Boolean Equals(Object ob...
Exit                       Method     static System.Void Exit(Int32 exitCode)
...
CommandLine                Property   static System.String CommandLine {get;}
CurrentDirectory           Property   static System.String CurrentDirectory ...
ExitCode                   Property   static System.Int32 ExitCode {get;set;}
HasShutdownStarted         Property   static System.Boolean HasShutdownStart...
MachineName                Property   static System.String MachineName {get;}
NewLine                    Property   static System.String NewLine {get;}
OSVersion                  Property   static System.OperatingSystem OSVersio...
ProcessorCount             Property   static System.Int32 ProcessorCount {get;}
StackTrace                 Property   static System.String StackTrace {get;}
SystemDirectory            Property   static System.String SystemDirectory {...
TickCount                  Property   static System.Int32 TickCount {get;}
UserDomainName             Property   static System.String UserDomainName {g...
UserInteractive            Property   static System.Boolean UserInteractive ...
UserName                   Property   static System.String UserName {get;}
Version                    Property   static System.Version Version {get;}
WorkingSet                 Property   static System.Int64 WorkingSet {get;}
TickCount                               ExitCode

接著即可選取 System.Environment 的屬性進行檢視。

顯示 System.Environment 的靜態屬性

System.Environment 的屬性也是靜態,而且指定方式與一般的屬性不同。您必須使用 :: 指示 Windows PowerShell 存取靜態方法或屬性。下列命令檢查 CommandLine 屬性,以得知用於啟動 Windows PowerShell 的命令:

PS> [System.Environment]::Commandline
"C:\Program Files\Windows PowerShell\v1.0\powershell.exe"

下列命令顯示 OSVersion 屬性,以檢查作業系統版本:

PS> [System.Environment]::OSVersion

           Platform ServicePack         Version             VersionString
           -------- -----------         -------             -------------
            Win32NT Service Pack 2      5.1.2600.131072     Microsoft Window...

顯示 HasShutdownStarted 屬性則可檢查電腦是否正在關機:

PS> [System.Environment]::HasShutdownStarted
False  

使用 System.Math 執行數學運算

System.Math 靜態類別可用於執行某些數學運算。System.Math 的重要成員幾乎都是方法,這可藉由使用 Get-Member 獲得證實。

附註:

System.Math 的某些方法具有相同的名稱,但可透過參數的型別來分辨。

請輸入下列命令以列舉 System.Math 類別的方法。

PS> [System.Math] | Get-Member -Static -MemberType Methods


   TypeName: System.Math

Name            MemberType Definition
----            ---------- ----------
Abs             Method     static System.Single Abs(Single value), static Sy...
Acos            Method     static System.Double Acos(Double d)
Asin            Method     static System.Double Asin(Double d)
Atan            Method     static System.Double Atan(Double d)
Atan2           Method     static System.Double Atan2(Double y, Double x)
BigMul          Method     static System.Int64 BigMul(Int32 a, Int32 b)
Ceiling         Method     static System.Double Ceiling(Double a), static Sy...
Cos             Method     static System.Double Cos(Double d)
Cosh            Method     static System.Double Cosh(Double value)
DivRem          Method     static System.Int32 DivRem(Int32 a, Int32 b, Int3...
Equals          Method     static System.Boolean Equals(Object objA, Object ...
Exp             Method     static System.Double Exp(Double d)
Floor           Method     static System.Double Floor(Double d), static Syst...
IEEERemainder   Method     static System.Double IEEERemainder(Double x, Doub...
Log             Method     static System.Double Log(Double d), static System...
Log10           Method     static System.Double Log10(Double d)
Max             Method     static System.SByte Max(SByte val1, SByte val2), ...
Min             Method     static System.SByte Min(SByte val1, SByte val2), ...
Pow             Method     static System.Double Pow(Double x, Double y)
ReferenceEquals Method     static System.Boolean ReferenceEquals(Object objA...
Round           Method     static System.Double Round(Double a), static Syst...
Sign            Method     static System.Int32 Sign(SByte value), static Sys...
Sin             Method     static System.Double Sin(Double a)
Sinh            Method     static System.Double Sinh(Double value)
Sqrt            Method     static System.Double Sqrt(Double d)
Tan             Method     static System.Double Tan(Double a)
Tanh            Method     static System.Double Tanh(Double value)
Truncate        Method     static System.Decimal Truncate(Decimal d), static...

上列清單顯示多種數學運算方法。下列命令示範一些常用方法的運作情形:

PS> [System.Math]::Sqrt(9)
3
PS> [System.Math]::Pow(2,3)
8
PS> [System.Math]::Floor(3.3)
3
PS> [System.Math]::Floor(-3.3)
-4
PS> [System.Math]::Ceiling(3.3)
4
PS> [System.Math]::Ceiling(-3.3)
-3
PS> [System.Math]::Max(2,7)
7
PS> [System.Math]::Min(2,7)
2
PS> [System.Math]::Truncate(9.3)
9
PS> [System.Math]::Truncate(-9.3)
-9




目錄