3

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.

2 Answers2

0

The LunMappings property is an array of LunMapping objects, so you first need to cast into that array type. Once you've done that you need to access some index of the array, at which point you can access the .Lun property

var arr = (Microsoft.Iscsi.Target.Commands.LunMapping[])psobject[index].Properties["LunMappings"];
arr[0].Lun;
latkin
  • 16,402
  • 1
  • 47
  • 62
  • Minor correction in the casting, but it generates other errors (see note from previous answer. `var arr = (Microsoft.Iscsi.Target.Commands.LunMapping[])psobject[index].Properties["LunMappings"].Value` – user2544260 Jul 03 '13 at 00:46
0

Calling ToString() on the property results in a string equaling "Microsoft.Iscsi.Target.Commands.LunMapping[]" - which is obviously not what I want.

Instead of calling ToString, cast to Microsoft.Iscsi.Target.Commands.LunMapping[]:

// obj is the thing than you used to call ToString() on that retuned
// "Microsoft.Iscsi.Target.Commands.LunMapping[]
var mappings = (Microsoft.Iscsi.Target.Commands.LunMapping[])obj;
// Check the length of the array and read the index 
// you are interested in instead of 0
int myLun = mappings[0].Lun;

Just to address your comment about the compiler error:

enter image description here

When correctly referenced you should not be getting a error. Double check everything. There is something wrong with your setup that you are not telling. As you can see on the picture the cast compiles just fine.

There are a few reasons why you might get the error even if you referenced the assembly. For example see here and here.

Community
  • 1
  • 1
Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • var t = ((Microsoft.Iscsi.Target.Commands.LunMapping[])pso.Properties["LunMapping"].Value is the correct casting, but generates an error (once the appropriate assembly is referenced): Error 2 The type or namespace name 'Iscsi' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?). This is with a reference to Microsoft.Iscsi.Target.Commands.dll – user2544260 Jul 03 '13 at 00:42
  • @user2544260: you doing something wrong that you are not telling us. Most likely you are not referencing the required assembly properly. The error that you are receiving indicates that the proper assembly is not referenced. You need to think why. The name of the dll that you specified is correct. – Andrew Savinykh Jul 03 '13 at 01:45
  • Thanks for the responses. Turned out the project I was working was targeted at .NET 3.5 and the Microsoft.Iscsi.Target.commands.dll required CLR4. So, despite my referencing the assembly correctly, the "error 2" error was occurring. – user2544260 Jul 03 '13 at 02:44