静态类

静态类是不能更改状态的方法和属性的引用库,不能使用 New-Object 进行创建。

导出所有静态类:

([AppDomain]::CurrentDomain.GetAssemblies()|?{$_.location -ne $null}| ForEach { $_.GetExportedTypes() }).FullName|Out-File D:\StaticClass.txt

 

查找 IPAddress 相关的静态类
[AppDomain]::CurrentDomain.GetAssemblies()|?{$_.location -ne $null}| ForEach-Object { $_.GetExportedTypes() } | Where-Object { $_.Name -like "IPAddress" } |Foreach {$_.FullName}

 

查看[System.Net.IPAddress] 可用的属性和方法
[System.Net.IPAddress]|gm -Static
[System.DateTime] |gm -static -membertype Method

 

将字符串转换为IP地址格式:
[System.Net.IPAddress]::parse("192.168.10.2")
[System.Net.IPAddress]'192.168.2.2'
[System.Net.IPAddress]'192.168.2.2222'   #如果该字符串不满足IP格式将会转换失败

 

查找 Environment相关的静态类
[AppDomain]::CurrentDomain.GetAssemblies()|?{$_.location -ne $null}| ForEach-Object { $_.GetExportedTypes() } | Where-Object { $_.Name -like "Environment" } |Foreach {$_.FullName}

 

=====================================================================================

 

[datetime]::now ===== (Get-Date).DateTime

[Diagnostics.Process]::getprocesses() ===== Get-Process
[Diagnostics.Process]::Start("calc.exe") #打开计算机器程序

$m=get-date
$m.GetType().fullname
System.DateTime 就是一个静态类


[System.DateTime]::isleapyear(1998) #判断1998年是否是闰年
[System.DateTime]::Parse("2013-08-13") #将字符串转换为DateTime类型

 

=====================================================


Web文件下载,FTP下载需要用户名和密码,Net.WebClient不可用:
$url = "http://192.168.12.6/iisstart.htm"
$m="e:\aa.txt"
$webclient = New-Object Net.WebClient
$webclient.DownloadFile($url,$m)

 

更改文件扩展名
[System.IO.Path]::ChangeExtension("test.txt", "ps1")

还有其他静态类参考如下:
[System.IO.Path]|gm -Static

 

posted on 2013-08-13 15:04  momingliu11  阅读(1398)  评论(0编辑  收藏  举报