Powershell将信息存储在对象中,每个对象都会有一个具体的类型,简单的文本会以System.String类型存储,日期会以System.DateTime类型存储。任何.NET对象都可以通过GetType()方法返回它的类型,该类型中有一个FullName属性,可以查看类型的完整名称。
2012年1月11日 15:19:49
PS C:Powershell> $date.GetType().FullName
System.DateTime
每一个类型都 可以包含一些静态的方法,可以通过方括号和类型名称得到类型对象本身,然后通过Get-Memeber命令查看该类型支持的所有静态方法。
TypeName: System.DateTime
Name MemberType Definition
---- ---------- ----------
Compare Method static int Compare(System.DateTime t1, System.Dat...
DaysInMonth Method static int DaysInMonth(int year, int month)
Equals Method static bool Equals(System.DateTime t1, System.Dat...
FromBinary Method static System.DateTime FromBinary(long dateData)
FromFileTime Method static System.DateTime FromFileTime(long fileTime)
FromFileTimeUtc Method static System.DateTime FromFileTimeUtc(long fileT...
FromOADate Method static System.DateTime FromOADate(double d)
IsLeapYear Method static bool IsLeapYear(int year)
Parse Method static System.DateTime Parse(string s), static Sy...
ParseExact Method static System.DateTime ParseExact(string s, strin...
ReferenceEquales Method static bool ReferenceEquals(System.Object objA, S...
SpecifyKind Method static System.DateTime SpecifyKind(System.DateTim...
TryParse Method static bool TryParse(string s, System.DateTime&, ...
TryParseExact Method static bool TryParseExact(string s, string format...
System.DateTime类支持的静态方法非常实用
使用Parse方法将一个字符串转换成DateTime类:
2012年10月13日 23:42:55
使用isLeapYear方法判断闰年
True
1988
1992
1996
2000
.NET支持成千上万的类型,有了这些类型可以做许多事情,幸运的是Powershell恰好支持这些类型。
对象类型转换
例如使用System.Net.IPAddress类将字符串IP地址转换成一个IPAddress实例
Address : 1199637258
AddressFamily : InterNetwork
ScopeId :
IsIPv6Multicast : False
IsIPv6LinkLocal : False
IsIPv6SiteLocal : False
IPAddressToString : 10.3.129.71
调用静态的方法
同样是System.Net.IPAddress类,根据IP地址查看主机名,8.8.8.8是谷歌的免费DNS服务器
HostName : google-public-dns-a.google.com
Aliases : {}
AddressList : {8.8.8.8}
根据类型创建实例
下面演示通过$webClient类的DownloadFile方法下载文件:
查看程序集
.NET中的类型定义在不同的程序集中,首先得知道当前程序已经加载了那些程序集。AppDomain类可以完成这个需求,因为它有一个静态成员CurrentDomain,CurrentDomain中有一个GetAssemblies()方法。
FriendlyName : DefaultDomain
Id : 1
ApplicationDescription :
BaseDirectory : C:WINDOWSsystem32WindowsPowerShellv1.0
DynamicDirectory :
RelativeSearchPath :
SetupInformation : System.AppDomainSetup
ShadowCopyFiles : False
PS C:Powershell> [AppDomain]::CurrentDomain.GetAssemblies()
GAC Version Location
--- ------- --------
True v2.0.50727 C:WindowsMicrosoft.NETFrameworkv2.0.50727mscorlib...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Cons...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem2.0.0.0__b77a5c561...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Management.Automat...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Comm...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Core3.5.0.0__b77a...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Configuration.Inst...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.WSMan.Managemen...
True v2.0.50727 C:WindowsassemblyGAC_32System.Transactions2.0.0.0...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Comm...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Comm...
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Secu...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Cons...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Xml2.0.0.0__b77a5...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Management2.0.0.0...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.DirectoryServices...
True v2.0 C:WindowsassemblyGAC_MSILSystem.Management.Automat...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft.WSMan.Managemen...
True v2.0.50727 C:WindowsMicrosoft.NETFrameworkv2.0.50727mscorlib...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Secu...
True v2.0.50727 C:WindowsassemblyGAC_32System.Data2.0.0.0__b77a5c...
True v2.0 C:WindowsassemblyGAC_MSILMicrosoft.PowerShell.Comm...
True v2.0.50727 C:WindowsassemblyGAC_MSILSystem.Configuration2.0....
True v2.0.50727 C:WindowsassemblyGAC_MSILMicrosoft.JScript8.0.0.0...
搜索指定类型
查询每个程序集中的方法可是使用GetExportedTypes() 方法。因为许多程序集中包含了大量的方法,在搜索时最好指定关键字。下面的代码演示如何查找包含”environment”关键字的类型。
System.EnvironmentVariableTarget
System.Environment
System.Environment+SpecialFolder
System.Runtime.InteropServices.RuntimeEnvironment
System.Security.Permissions.EnvironmentPermissionAccess
System.Security.Permissions.EnvironmentPermission
System.Security.Permissions.EnvironmentPermissionAttribute
Microsoft.PowerShell.Commands.EnvironmentProvider
上面搜索到的类型有一个为:System.Environment类,System.Environment类可以做很多事情,可以先查看以下System.Environment类的所有静态方法。
TypeName: System.Environment
Name MemberType Definition
---- ---------- ----------
Equals Method static bool Equals(System.Object objA,
Exit Method static System.Void Exit(int exitCode)
ExpandEnvironmentVariables Method static string ExpandEnvironmentVariabl
FailFast Method static System.Void FailFast(string mes
GetCommandLineArgs Method static string[] GetCommandLineArgs()
GetEnvironmentVariable Method static string GetEnvironmentVariable(s
GetEnvironmentVariables Method static System.Collections.IDictionary
GetFolderPath Method static string GetFolderPath(System.Env
GetLogicalDrives Method static string[] GetLogicalDrives()
ReferenceEquals Method static bool ReferenceEquals(System.Obj
SetEnvironmentVariable Method static System.Void SetEnvironmentVaria
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 {ge
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;}
例如System.Environment中的属性输出当前登录域、用户名、机器名:
搜索方法
下面的例子演示如何根据指定关键字”Address”,搜索方法。
[System.Net.IPAddress]::Parse(System.String) --> System.Net.IPAddress
[System.Net.IPAddress]::IsLoopback(System.Net.IPAddress) --> System.Boolean
[System.Net.IPAddress]::Any -->
[System.Net.IPAddress]::Loopback -->
[System.Net.IPAddress]::Broadcast -->
[System.Net.IPAddress]::None -->
[System.Net.IPAddress]::IPv6Any -->
[System.Net.IPAddress]::IPv6Loopback -->
[System.Net.IPAddress]::IPv6None -->
[System.Net.Sockets.AddressFamily]::Unknown -->
[System.Net.Sockets.AddressFamily]::Unspecified -->
[System.Net.Sockets.AddressFamily]::Unix -->
[System.Net.Sockets.AddressFamily]::InterNetwork -->
[System.Net.Sockets.AddressFamily]::ImpLink -->
[System.Net.Sockets.AddressFamily]::Pup -->
[System.Net.Sockets.AddressFamily]::Chaos -->
[System.Net.Sockets.AddressFamily]::NS -->
[System.Net.Sockets.AddressFamily]::Ipx -->
[System.Net.Sockets.AddressFamily]::Iso -->
[System.Net.Sockets.AddressFamily]::Osi -->
[System.Net.Sockets.AddressFamily]::Ecma -->
[System.Net.Sockets.AddressFamily]::DataKit -->
[System.Net.Sockets.AddressFamily]::Ccitt -->
[System.Net.Sockets.AddressFamily]::Sna -->
[System.Net.Sockets.AddressFamily]::DecNet -->
[System.Net.Sockets.AddressFamily]::DataLink -->
[System.Net.Sockets.AddressFamily]::Lat -->
[System.Net.Sockets.AddressFamily]::HyperChannel -->
[System.Net.Sockets.AddressFamily]::AppleTalk -->
[System.Net.Sockets.AddressFamily]::NetBios -->
[System.Net.Sockets.AddressFamily]::VoiceView -->
[System.Net.Sockets.AddressFamily]::FireFox -->
[System.Net.Sockets.AddressFamily]::Banyan -->
[System.Net.Sockets.AddressFamily]::Atm -->
[System.Net.Sockets.AddressFamily]::InterNetworkV6 -->
[System.Net.Sockets.AddressFamily]::Cluster -->
[System.Net.Sockets.AddressFamily]::Ieee12844 -->
[System.Net.Sockets.AddressFamily]::Irda -->
[System.Net.Sockets.AddressFamily]::NetworkDesigners -->
[System.Net.Sockets.AddressFamily]::Max -->
[System.Net.Sockets.IOControlCode]::GetBroadcastAddress -->
[System.Net.Sockets.IOControlCode]::AddressListQuery -->
[System.Net.Sockets.IOControlCode]::AddressListChange -->
[System.Net.Sockets.IOControlCode]::AddressListSort -->
[System.Net.Sockets.SocketError]::DestinationAddressRequired -->
[System.Net.Sockets.SocketError]::AddressFamilyNotSupported -->
[System.Net.Sockets.SocketError]::AddressAlreadyInUse -->
[System.Net.Sockets.SocketError]::AddressNotAvailable -->
[System.Net.Sockets.SocketOptionName]::ReuseAddress -->
[System.Net.Sockets.SocketOptionName]::ExclusiveAddressUse -->
[System.Net.NetworkInformation.DuplicateAddressDetectionState]::Invalid -->
[System.Net.NetworkInformation.DuplicateAddressDetectionState]::Tentative -->
[System.Net.NetworkInformation.DuplicateAddressDetectionState]::Duplicate -->
[System.Net.NetworkInformation.DuplicateAddressDetectionState]::Deprecated -->
[System.Net.NetworkInformation.DuplicateAddressDetectionState]::Preferred -->
[System.Net.NetworkInformation.NetworkChange]::add_NetworkAddressChanged(System
.Net.NetworkInformation.NetworkAddressChangedEventHandler) --> System.Void
[System.Net.NetworkInformation.NetworkChange]::remove_NetworkAddressChanged(Sys
tem.Net.NetworkInformation.NetworkAddressChangedEventHandler) --> System.Void
[System.Net.NetworkInformation.PhysicalAddress]::Parse(System.String) --> Syste
m.Net.NetworkInformation.PhysicalAddress
[System.Net.NetworkInformation.PhysicalAddress]::None -->
[System.Net.NetworkInformation.SuffixOrigin]::LinkLayerAddress -->
[System.DirectoryServices.ActiveDirectory.ActiveDirectorySyntax]::PresentationA
ddress -->
[Microsoft.JScript.JSError]::DoesNotHaveAnAddress -->
[Microsoft.JScript.JSError]::WrongUseOfAddressOf -->
新闻热点
疑难解答
图片精选