lørdag den 26. marts 2011

PowerShell PSCmdLets and Pipeline objects

I’m working on a PowerShell interface to the project I’m working on. Most of the commands return Class’s and I'm being very thorough about making sure passing parameters work both normally, unnamed and piped. But one function kept giving me problems.

PS C:\> $customerservice = Get-CustomerService skadefro.dk
PS C:\
> $customerservice | Get-CustomerServiceObject
Get
-CustomerServiceObject : Need to suply either CustomerServiceID or CustomerServiceObjectID
At line:
1 char:45
+ $customerservice | Get-CustomerServiceObject <<<<
+ CategoryInfo : NotSpecified: (:) [Get-CustomerServiceObject], E xception
+ FullyQualifiedErrorId : System.Exception,Cloud.Provisioning.CloudSnapin.
Cloud.Provisioning.GetCustomerServiceObject

PS C:\
> Get-CustomerServiceObject $customerservice

ExtensionData : System.Runtime.Serialization.ExtensionDataObject
CustomerServiceID :
1897
CustomerServiceObjectID :
2260
Label : 36cb002b
-355e-4dd3-9dca-f217922a4a63
Properties : {
2260, 2260, 2260, 2260...}
ServiceObjectID :
15
default : False
private :
True

ExtensionData : System.Runtime.Serialization.ExtensionDataObject
CustomerServiceID :
1897
CustomerServiceObjectID :
2261
Label : c38d1601
-df38-40d8-8ef8-6f8ac96eb09e
Properties : {
2261, 2261, 2261, 2261...}
ServiceObjectID :
15
default : False
private :
True

PS C:\
>

I can pass it as a normal parameter, but when pipelining the object(s) it wouldn’t get the object. I used the same parameter type in other CMDlets with no problems. I even tried deleting the project and then class by class copy’n’pasting over the code in case something behind the scene was messing with me, but to no wail. Then as I for the 1000’th time was giving it a crack, I fell a cross a news post (sorry I cant find the link again ) that instantly made it clear to me what was wrong.


 


PS C:\> trace-command parameterbinding -pshost { $customerservice | Get-Customer
ServiceObject }
This is one cool command. You can do all kinds of neat tricks with that, but most importantly right now, it showed me how PowerShell tries to pass the piped objects, and turns out my CMD let was throwing an exception every time PowerShell tried giving it the parameter
Protected Overrides Sub BeginProcessing()
If _CustomerServiceObjectID.Count = 0 And _CustomerServiceID.Count = 0 Then
Throw New Exception("Need to suply either CustomerServiceID or CustomerServiceObjectID")
End If
End Sub

Once I removed my check from BeginProcessing and moved it to ProcessRecord everything worked as intended.

Ingen kommentarer:

Send en kommentar