Powershell将信息存储在对象中,每个对象都会有一个具体的类型,简单的文本会以System.String类型存储,日期会以System.DateTime类型存储。任何.NET对象都可以通过GetType()方法返回它的类型,该类型中有一个FullName属性,可以查看类型的完整名称。
复制代码 代码如下:
PS C:Powershell> $date=get-date
PS C:Powershell> $date
2012年1月11日 15:19:49
PS C:Powershell> $date.GetType().FullName
System.DateTime
每一个类型都 可以包含一些静态的方法,可以通过方括号和类型名称得到类型对象本身,然后通过Get-Memeber命令查看该类型支持的所有静态方法。
复制代码 代码如下:
PS C:Powershell> [System.DateTime] | Get-Member -static -memberType *Method
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类:
复制代码 代码如下:
PS C:Powershell> [System.DateTime]::Parse("2012-10-13 23:42:55")
2012年10月13日 23:42:55
使用isLeapYear方法判断闰年
复制代码 代码如下:
#1988年是闰年吗?
[System.DateTime]::IsLeapYear(1988)
#打印1988到2000年的所有闰年
for($year=1988;$year -le 2000;$year++)
{
if( [System.DateTime]::IsLeapYear($year) ){$year}
}
True
1988
1992
1996
2000
另一个常用的类为Math类,在Math类中定义了很多实用的静态方法:
例如求绝对值,三角函数,取整:
复制代码 代码如下:
PS C:Powershell> [Math]::Abs(-10.89)
10.89
PS C:Powershell> [Math]::Sin([Math]::PI/2)
1
PS C:Powershell> [Math]::Truncate(2012.7765)
2012
查看感兴趣的.NET类型
.NET支持成千上万的类型,有了这些类型可以做许多事情,幸运的是Powershell恰好支持这些类型。
对象类型转换
例如使用System.Net.IPAddress类将字符串IP地址转换成一个IPAddress实例
复制代码 代码如下:
PS C:Powershell> [Net.IPAddress]'10.3.129.71'
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服务器
复制代码 代码如下:
PS C:Powershell> [system.Net.Dns]::GetHostByAddress('8.8.8.8') | fl
HostName : google-public-dns-a.google.com
Aliases : {}
AddressList : {8.8.8.8}
根据类型创建实例
下面演示通过$webClient类的DownloadFile方法下载文件:
复制代码 代码如下:
PS C:Powershell> $localName="C:Powershellindex.php"
PS C:Powershell> Test-Path $localName
False
PS C:Powershell> $add="http://www.mossfly.com/index.php"
PS C:Powershell> $webClient=New-Object Net.WebClient
PS C:Powershell> $webClient.DownloadFile($add,$localName)
PS C:Powershell> Test-Path $localName
True
查看程序集
.NET中的类型定义在不同的程序集中,首先得知道当前程序已经加载了那些程序集。AppDomain类可以完成这个需求,因为它有一个静态成员CurrentDomain,CurrentDomain中有一个GetAssemblies()方法。
复制代码 代码如下:
PS C:Powershell> [AppDomain]::CurrentDomain
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”关键字的类型。
复制代码 代码如下:
PS C:Powershell> [AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $_.GetExportedTypes() } | Where-Object { $_ -like $searchtext } | ForEach-Object { $_.FullName }
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类的所有静态方法。
复制代码 代码如下:
PS C:Powershell> [Environment] | Get-Member -Static
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中的属性输出当前登录域、用户名、机器名:
复制代码 代码如下:
PS C:Powershell> [Environment]::UserDomainName
MyHome
PS C:Powershell> [Environment]::UserName
xiaoming
PS C:Powershell> [Environment]::MachineName
LocalHost
搜索方法
下面的例子演示如何根据指定关键字”Address”,搜索方法。
复制代码 代码如下:
[AppDomain]::CurrentDomain.GetAssemblies() | ForEach-Object { $_.GetExportedTypes() } | ForEach-Object { $_.getmembers() } | Where-Object { $_.isStatic} | Where-Object { $_ -like $searchtext } | ForEach-Object { "[{0}]::{1} --> {2}" -f $_.declaringtype, $_.toString().SubString($_.toString().IndexOf(" ")+1), $_.ReturnType }
[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 -->
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]