2016-11-24 51 views
2

我正在使用使用自定義對象的PowerShell編寫腳本。但棘手的部分是這個自定義對象使用另一個自定義對象作爲屬性。我試圖用C#語法來完成它。添加依賴於第一個的第二個類型的錯誤

當我嘗試運行我得到這個錯誤的腳本:

The type or namespace name 'Disk' could not be found (are you missing a using directive or an assembly reference?) 
c:\Users\trumpe\AppData\Local\Temp\orydsmyw.0.cs(1) : public class VirtualMachine{ 
c:\Users\trumpe\AppData\Local\Temp\orydsmyw.0.cs(2) : >>>  public Disk Disks; 
c:\Users\trumpe\AppData\Local\Temp\orydsmyw.0.cs(3) :  public string IPAddress; 

有沒有辦法做到這一點在所有使用PowerShell?這裏是我的課堂代碼在一個文件中:

Add-Type -Language CSharp @" 
public class Disk{ 
public string VirtualMachineDiskID; 
public string StorageID; 
public string StorageIdentifier; 
public string DiskFileName; 
public string StorageProfileID; 
public int CapacityKB; 
public int ControllerKey; 
public int DeviceKey; 
public int DiskMode; 
public int UnitNumber; 
} 
"@; 



Add-Type -Language CSharp @" 
public class VirtualMachine{ 
public Disk Disks; #<=== This part stops working because it cannot find the type Disk 
public string IPAddress; 
public string Name; 
public string OSFullName; 
public string PowerState; 
public string ResourcePoolID; 
public string TenantID; 
public string UniqueId; 
public string VirtualMachineID; 
public int NumCPU; 
public int NumNic; 
public int StorageCapacityAllocatedMB; 
public int RamAllocatedMB; 
public bool IsRemoved; 
public bool IsTemplate; 
} 
"@; 

我不限於C#語法,所以所有的解決方案都是受歡迎的。

+0

FYI我刪除在後來添加的問題,身體的標籤。 http://meta.stackoverflow.com/questions/280315/tags-in-question-bodies – Matt

+0

@Matt好的,我不會再添加它們了。 –

+1

我_think_我找到了你正在尋找的東西,這就解釋了你爲什麼沒有工作。關於這裏的課程的答案也可以幫助你。 – Matt

回答

2

介紹類:

Class Disk{ 

    [string] $VirtualMachineDiskID; 
    [string] $StorageID; 
    [string] $StorageIdentifier; 
    [string] $DiskFileName; 
    [string] $StorageProfileID; 
    [int] $CapacityKB; 
    [int] $ControllerKey; 
    [int] $DeviceKey; 
    [int] $DiskMode; 
    [int] $UnitNumber; 

} 

Class VirtualMachine { 
    [Disk] $Disks; 
    [string] $IPAddress; 
    [string] $Name; 
    [string] $OSFullName; 
    [string] $PowerState; 
    [string] $ResourcePoolID; 
    [string] $TenantID; 
    [string] $UniqueId; 
    [string] $VirtualMachineID; 
    [int] $NumCPU; 
    [int] $NumNic; 
    [int] $StorageCapacityAllocatedMB; 
    [int] $RamAllocatedMB; 
    [bool] $IsRemoved; 
    [bool] $IsTemplate; 
} 

$disk = New-Object Disk 
$disk.StorageID = 4711 

$vm = New-Object VirtualMachine 
$vm.Disks = $disk 
相關問題