I'm at a loss at how to introduce this question with a summary, so I'll dive straight in with an explanation.
I am currently implementing some functionality in a C# application that relies on executing various powershell commands. To be precise, I'm trying to retrieve a list of Server Targets from the Windows Server 2012 iSCSI Target feature.
The PS command is Get-IscsiServerTarget and as an example it returns the following output:
Id : Server0.contoso.local:SQLTarget TargetName : SQLTarget TargetIqn : iqn.1991-05.com.microsoft:server0-sqltarget-target Description : Enable : True Status : Idle LastLogin : 12/31/1600 4:00:00 PM EnableChap : False EnableReverseChap : False ComputerName : Server0.contoso.local MaxReceiveDataSegmentLength : 65536 FirstBurstLength : 65536 MaxBurstLength : 262144 ReceiveBufferCount : 10 EnforceIdleTimeoutDetection : True InitiatorIds : {IPAddress:10.1.1.3} LunMappings : {TargetName:SQLTarget;WTD:2;LUN:0} Version : 3.3.16543 ServerInfo : Server0.contoso.local
The PSObject collection that returns after invoking the command on the pipeline is a collection of Microsoft.Iscsi.Target.Commands.IscsiServerTarget objects, which is all well and good as I can access all the properties that are primitive types. My problem is that LunMappings is of type Microsoft.Iscsi.Target.Commands.LunMapping and I can't find a way to access the properties of this particular object.
Calling ToString() on the property results in a string equaling "Microsoft.Iscsi.Target.Commands.LunMapping[]" - which is obviously not what I want.
In my mind what I want to do is
psobject[index].Properties["LunMappings"].Properties["Lun"]
or
((Microsoft.Iscsi.Target.Commands.LunMapping[])psobject[index].Properties["LunMappings"]).Lun
I've tried the last one, after placing a reference to the necessary assembly, but I received compile errors.
I'd appreciate any guidance, direction, or constructive comments.
EDIT
Placing a reference in the project to Microsoft.Iscsi.Target.Commands.dll to allow the casting of the PSObject ((Microsoft.Iscsi.Target.Commands.LunMapping[])psobject[index].Properties["LunMappings"]).Lun
causes a compile error - Error 4 The type or namespace name 'Iscsi' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
.
EDIT
Have fixed this issue now. The error 4 issue mentioned in the frist edit, when I tried to cast, was because the project was targeted at the 3.5 framework and the referenced assembly requires 4.0. Now I'm able to successfully cast the object.