如何编写 PowerShell cmdlet 以采用 HashTable 或 PODO 作为输入?
我有一个包含一些 Web 服务的 powershell 模块。 Web 服务采用复杂的普通旧点网络对象 (PODO),我一直在使用 哈希表如 cmdlet 参数和New-Object MyPODO -Property $MyHashTable 将哈希表转换为请求对象,如下所示
function Get-Stuff ([HashTable]$WhatStuff) {
$service = New-ServiceProxy . . . .
$request = New-Object GetStuffRequest -Property $WhatStuff;
return $service.GetStuff($request);
$response;
}
但是,有时我有一个 cmdlet,其响应对象可以直接变成请求对象,如下所示:
function Find-Stuff ([HashTable]$KindaStuff) {
$service = New-ServiceProxy . . . .
$request = New-Object GetStuffRequest -Property $KindaStuff;
return $service.SearchStuff($request);
}
是否有某种方法可以修饰 $WhatStuff 参数以接受特定类型的 HashTable 或 PODO?
I have a powershell module that wraps around some web services. The web services take complex Plain Old Dot Net Objects (PODOs) and I have been using HashTables as in cmdlet parameters and New-Object MyPODO -Property $MyHashTable to transform the hashtable into the request object like so
function Get-Stuff ([HashTable]$WhatStuff) {
$service = New-ServiceProxy . . . .
$request = New-Object GetStuffRequest -Property $WhatStuff;
return $service.GetStuff($request);
$response;
}
However, sometimes I have a cmdlet whose response object can directly become a request object like so:
function Find-Stuff ([HashTable]$KindaStuff) {
$service = New-ServiceProxy . . . .
$request = New-Object GetStuffRequest -Property $KindaStuff;
return $service.SearchStuff($request);
}
Is there some sort of way to decorate the $WhatStuff parameter to accept either a HashTable or a PODO of a particular type?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
James Tryand 在一条推文。
答案是使用参数集。
在一个参数集中,您接受 HashTable 类型的参数,在另一参数集中,您接受 PODO 类型。
James Tryand gave me this answer in a tweet.
The answer is to use Parameter Sets.
In one paramater set you accept a parameter of type HashTable, and in the other one, you accept the PODO type.
也许像下面这样,取决于你想如何使用它:
Maybe like below, depending on how you want to use it: