靜态類是不能更改狀态的方法和屬性的引用庫,不能使用 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