Get parent property in child select in Powershell?

有些话、适合烂在心里 提交于 2019-12-13 01:50:32

问题


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.


回答1:


Don't you want the Parent property?

$s.Databases | select Parent, Name, AutoShrink

Edit:

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


来源:https://stackoverflow.com/questions/2151880/get-parent-property-in-child-select-in-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!