通过powershell管道推动expandos

发布于 2024-11-09 03:02:58 字数 838 浏览 0 评论 0原文

无论如何,任何人都会发现让这一切发生。我想要一个从数据库创建输出的 cmdlet。没有具体的模式,每个“行”可以有不同的字段。在像 javascript 这样的东西中,这不会有问题,每个对象都会有它需要的任何属性;但 powershell 不是那样的。

我尝试了一个简单的实现,但我得到的只是 Expandos 假装的 Key、Value 字典的枚举。

扩大问题。

get-datarows cmdlet 应将什么对象推入管道(它接受任意查询)。我不知道要实例化和推送什么对象类型。数据库查询可以返回 User=dave,Age=12 的行,然后返回 User=pete, Favcol = red 的另一行。下次运行 cmdlet 时,查询可能会返回完全不同的内容(Type=shoe、color=red、use=dancing)。能够实例化一个 Expandos 管道在这里完美地工作(除了它没有)

到目前为止,我最好的做法是使用 Reflection.emit 动态生成类型,但这需要我知道对象的模式。我可以通过查看数据库查询返回的第一个对象来做到这一点,但它可能不具有所有可能的属性(如上面的前 2 个用户行)。我可以从头到尾读完;制作动态创建类型的类型、倒带和推送实例,但这不是很有效

EDIT2:更多说明

我正在 C# 中编码,

我希望能够做到

mycmdlet -query "users" | ft

,或者

mycmdlet -query "products;type=shoe,size>1" | make-pretty

我不希望用户必须做一大堆数据整形;这就是 cmdlet 的全部目的

Anybody discovered anyway to make this happen. I want to have a cmdlet that creates output from a database. There is no concrete schema, each 'row' can have different fields. In something like javascript this would be no problem, each object would have whatever properties it needs; but powershell isnt like that.

I tried a naive implementation but all I got was an enumeration of the Key, Value dictionary that expandos pretend to be.

Expanding the question.

What object should the get-datarows cmdlet push down the pipe (it accepts arbitrary queries). I dont know what object types to instantiate and push. A db query could return a row with User=dave,Age=12, then another row with User=pete, Favcol = red. Next time I run the cmdlet a query might return something totally different (Type=shoe, color=red,use=dancing). Being able to instantiate a pipe of expandos works perfectly here (except it doesn't)

My best go so far is to generate a type on the fly using reflection.emit, but this requires me to know the schema of the objects. I could do it by looking at the first object returned by the db query but that might not have all possible attributes (as in the first 2 user rows above). I could read all the way to the end; make the type, rewind and the push instances of the dynamically created type, but thats not very efficient

EDIT2:even more clarification

I am coding in c#

I want to be able to do

mycmdlet -query "users" | ft

or

mycmdlet -query "products;type=shoe,size>1" | make-pretty

I dont want the user to have to do a whole bunch of data shaping; that's the whole purpose of the cmdlet

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

关于从前 2024-11-16 03:02:58

我想通了。 powershell有自己的expandos; psobject。这就是WriteObject真正推动的。但你可以自己做

所以做

var obj = new PSObject();
obj.properties.add(new PSNoteProperty("foo", 42));
obj.properties.add(new PSNoteProperty("bar", "xxxx"));
WriteObject(obj);

I figured it out. powershell has its own expandos; psobject. This is what is actaully pushed by WriteObject. But you can make your own

So do

var obj = new PSObject();
obj.properties.add(new PSNoteProperty("foo", 42));
obj.properties.add(new PSNoteProperty("bar", "xxxx"));
WriteObject(obj);
〃安静 2024-11-16 03:02:58

没有任何理由不能在管道中拥有具有不同属性集的对象。它应该工作正常。我一直这样做。

您能更具体地说明什么不起作用吗?也许是您想要做的事情的一个例子?

There isn't any reason you can't have objects with different sets of properties in the pipeline. It should work fine. I do this all the time.

Can you be more specific about what isn't working? Perhaps an example of what you're trying to do?

人心善变 2024-11-16 03:02:58

我要么误解了你的问题,要么我一直这样做。我的代码全部在工作,但是在伪代码中-

$results = Get-ResultSet...
$columns = @{}
foreach ($column in $results.Columns)
{
    columns.Add($column.Name,$column.Index)
}
foreach ($row in results)
{
    $return = New-Object PSObject
    foreach ($key in $columns.keys)
    {
        $return | Add-Member -MemberType NoteProperty -name $key $row[$columns[$key]]
    }
    $return
}

您的自定义对象将具有与列一样多的属性,并且每个属性都将具有您想要的值。

I'm either misunderstanding your question, or I do it all the time. My code is all at work, but in psuedo-

$results = Get-ResultSet...
$columns = @{}
foreach ($column in $results.Columns)
{
    columns.Add($column.Name,$column.Index)
}
foreach ($row in results)
{
    $return = New-Object PSObject
    foreach ($key in $columns.keys)
    {
        $return | Add-Member -MemberType NoteProperty -name $key $row[$columns[$key]]
    }
    $return
}

Your custom object will have as many properties as columns and each property will have the value you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文