在Powershell中获取子选择中的父属性?

发布于 2024-08-19 14:00:17 字数 664 浏览 3 评论 0原文

我想在 Powershell 的子结果集中包含父级别属性,但我不知道如何执行此操作。我想要做的一个例子是将 ServerName 作为管道的第一列来获取 DatabaseName 和服务器上所有数据库的数据库属性列表,类似于:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "SQLSERVER"

$s.Databases | select $s.Name, Name, AutoShrink, AutoClose

这是我的简化示例尝试这样做(我可以轻松地使用数据库父属性来获取 $s.Name),但我有更复杂的应用程序,我想使用这种类似的方法,而父属性不是我想要的。另外,如果我可以将 $s.Name 别名为 ServerName,将 $_.Name 别名为 DatabaseName,那么这将是理想的输出。

编辑:

我花了两天时间在网上搜索如何执行此操作,但找不到任何参考。如果您碰巧使用 Google/Bing/无论答案是什么,如果您也愿意分享您用来找到它的内容,我将非常感激。我一般都能找到问题的答案,但两天后我就开始把美好的时光抛在糟糕的事情后面。

I want to include a parent level property in a child result set in Powershell and I can't figure out how to do it. An example of what I want to do would be to include the ServerName as the first column of a pipe to get the DatabaseName and a list of Database properties of all the databases on the server similar to:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') "SQLSERVER"

$s.Databases | select $s.Name, Name, AutoShrink, AutoClose

This is a simplified example of what I am trying to do, (I could easily use the Database Parent property to get the $s.Name) but I have much more complex applications where I'd like to use this similar methodology and a Parent property isn't what I am after. Also if I could alias the $s.Name to be ServerName and the $_.Name as DatabaseName it would be the ideal output.

EDIT:

I have spent two days searching for how to do this online and can't find any reference. If you happen to Google/Bing/whatever the answer, if you'd also share what you used to find it I'd be really appreciative. I generally can find the answers to stuff, but after two days I am just throwing good time after bad.

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

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

发布评论

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

评论(1

勿忘初心 2024-08-26 14:00:17

你不想要父财产吗?

$s.Databases | select Parent, Name, AutoShrink

编辑:

$s.Databases | select @{Name="ServerPlatform"; Expression={$_.Parent.Platform}}, Name, AutoShrink

Don't you want the Parent property?

$s.Databases | select Parent, Name, AutoShrink

Edit:

$s.Databases | select @{Name="ServerPlatform"; Expression={$_.Parent.Platform}}, Name, AutoShrink
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文