在PowerShell v5中使用新的類函數,並且如果我們可以將方法放入類中,我正試圖讓自己的頭腦清晰。PowerShell v5類方法 - 問題
我已經嘗試了下面並玩了一下,但一直沒有運氣。
class Server {
[string]$computerName = "192.168.0.200"
[bool]$ping = (Test-Connection -ComputerName $computerName).count
}
$1 = [server]::new()
$1.computerName = "blah"
我已經通過設置屬性手動輸入計算機名試過,但我認爲你需要它在創建對象
$1 = [server]::new($computerName = "192.168.0.200")
例外我越來越有
從$錯誤[ERROR] Exception calling ".ctor" with "0" argument(s): "Cannot validate argument on parameter 'ComputerName'. The argument is null or empty. Provide an argument that is not null or empty, and then try the
[ERROR] command again."
[ERROR] At D:\Google Drive\Projects\VSPowerShell\DiscoveryFramework\DiscoveryFramework\DiscoveryFramework\class.ps1:12 char:1
[ERROR] + $1 = [server]::new()
[ERROR] + ~~~~~~~~~~~~~~~~~~~~
[ERROR] + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
[ERROR] + FullyQualifiedErrorId : ParameterBindingValidationException
[ERROR]
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE>
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> $1
Server
[DBG]: PS C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE> $1.gettype()
Server
完全例外鏈接是http://pastebin.com/WtxfYzb5
得到了一點進一步使用$ this.prop,但你不能用你自己的參數啓動構造函數。
PS path:\> class Server {
[string]$computerName = "192.168.0.200"
[bool]$ping = (Test-Connection -ComputerName $this.computerName).count
}
PS path:\>
PS path:\> $var = [server]::new()
PS path:\> $var
computerName ping
------------ ----
192.168.0.200 True
管理得到更進一步,使用$ this.PROP ---聽以下播客和傑弗裏·斯諾弗設法回答 [http:// powershell。org/wp/2014/12/17/episode-290-powerscripting-podcast-jeffrey-snover-and-john-slack-on-powershell-5-0-technical-preview /] [1] –
必須改變你的代碼有點,它是在我大喊大叫不使用$ this.prop ---類編譯,但我不能用參數重載構造函數。 –