开发者

How to convert cmdlet parameter (which is string) to any other type

开发者 https://www.devze.com 2023-03-18 09:57 出处:网络
I want to pass parameter of custom type to my cmdlet. I declare parameter like this: [Parameter(Position = 1)]

I want to pass parameter of custom type to my cmdlet. I declare parameter like this:

[Parameter(Position = 1)]
public StoreIdParameter Identity { get; set; }

But when I run cmdlet with parameter (like this: Get-Store VS) I get an exception:

Cannot bind parameter 'Identity'. Cannot convert the "VS" value of type "System.String" to type "R.Management.StoreIdParameter".

I created a constructor of StoreIdParameter, which accepts String but it didn't help. Should it be some type convertor from string to my type?开发者_JS百科


I have found a solution. That was my mistake - I had constructor accepting string argument only in base class. I have added constructor to derived class (StoreIdParameter) with string argument and Powershell could create an instance of this class by calling proper constructor

0

精彩评论

暂无评论...
验证码 换一张
取 消