2

I'm writing a script using PowerShell which uses a custom object. But the tricky part is that this custom object uses another custom object as a property. I'm trying to do it with C# syntax.

When I'm trying to run the script I'm getting this error:

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;

Is there a way to do it at all in PowerShell? Here are my class code in one file:

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;
}
"@;

I'm not limited to C# syntax so all solutions are welcome.

Matt
  • 45,022
  • 8
  • 78
  • 119
SokIsKedu
  • 196
  • 1
  • 2
  • 17
  • FYI I removed the tags in the question body that were added later. http://meta.stackoverflow.com/questions/280315/tags-in-question-bodies – Matt Nov 24 '16 at 13:12
  • @Matt Ok, I won't add them anymore. – Martin Brandl Nov 24 '16 at 13:15
  • 1
    I _think_ I found what you are looking for that explains why what you did was not working. The answer about classes here will also help you. – Matt Nov 24 '16 at 13:19
  • Thank you very much for your answers. It helped me a lot and on top of that I found another solution using C# syntax. All you have to do is to put depending classes in one Add-Type i.e Add-Type -Language CSharp @" public class Disk{ public string VirtualMachineDiskID; public string StorageID; } public class VirtualMachine{ public Disk Disks; #<=== This part stops working because it cannot find the type Disk public string IPAddress; public string Name; } "@; Now this works with no problems. Thanks again for help – SokIsKedu Nov 30 '16 at 08:42

1 Answers1

3

introduces classes:

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
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172