I wish I could have been more help :S Glad you got it working
Brandon Shell
---------------
Blog:
http://www.bsonposh.com/
PSH Scripts Project:
www.codeplex.com/psobject
TM> I got nothing back from that, too. I have made some progress,
TM> though. Here' what I have so far:
TM>
TM> $smo.Databases["AdventureWorks"].Tables | Where-Object {$_.Schema
TM> -eq "Sales"} | foreach {$Table = $_.Name; Write-Host $Table; foreach
TM> ($Index in $_.Indexes) {Write-host "`t" $Index.Name "`t"
TM> $Index.SpaceUsed}}
TM>
TM> When I add other methods in the second foreach, I'm not seeing
TM> evidence that they were actually sent to SQL Server, but that may be
TM> more of a SMO issue than PowerShell.
TM>
TM> ----------------------------------------------------
TM> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
TM> SQL Server MVP
TM> Toronto, ON Canada
TM>
https://mvp.support.microsoft.com/profile/Tom.Moreau
TM> "Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in message
TM> news:29d4f64638948ca27e45cab9041@xxxxxx
TM> That is because Indexes is an array of arrays
TM> $smo.Databases["AdventureWorks"].Tables | Where-Object {$_.Schema
TM> -eq
TM> "Sales"}|
TM> Select-Object Indexes | %{$_} | %{$_.name}
TM> Perhaps I can explain what I think is happening
TM> * Select-Object Indexes # this passes just the Collection of Index
TM> Collections
TM> down the pipe
TM> * | %{$_} # This takes each Index Collection and
TM> passes
TM> it down the pipe
TM> * | %{$_.name} # This should get each Index in the Index
TM> Collection
TM> and Ouput just the Name
TM> Brandon Shell
TM> ---------------
TM> Blog:
http://www.bsonposh.com/
TM> PSH Scripts Project:
www.codeplex.com/psobject
TM>> I get nothing back from this. I still don't get why foreach just
TM>> isn't getting the object.
TM>>
TM>> ----------------------------------------------------
TM>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
TM>> SQL Server MVP
TM>> Toronto, ON Canada
TM>>
https://mvp.support.microsoft.com/profile/Tom.Moreau
TM>> "Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in message
TM>> news:29d4f646388c8ca27e0af10ce1d@xxxxxx
TM>>
TM>> Ok.. I think the problem is that Indexes returns and array of These
TM>>
TM>>
http://technet.microsoft.com/zh-cn/l...sqlserver.mana
TM>> g ement.smo.indexcollection_members.aspx
TM>>
TM>> which contain these
TM>>
TM>>
http://technet.microsoft.com/zh-cn/l...sqlserver.mana
TM>> g ement.smo.index.aspx
TM>>
TM>> While you could do this on oneline.. I think it is unreadable so I
TM>> would do this ( sorry I cant test im doing this in head :S )
TM>>
TM>> $Indexes = $smo.Databases["AdventureWorks"].Tables | Where-Object
TM>> {$_.Schema
TM>> -eq "Sales"}| Select-Object Indexes
TM>> foreach($Index in $Indexes)
TM>> {
TM>> $Index | %{$_.Name}
TM>> }
TM>> Brandon Shell
TM>> ---------------
TM>> Blog:
http://www.bsonposh.com/
TM>> PSH Scripts Project:
www.codeplex.com/psobject
TM>>> That was in my original post:
TM>>>
TM>>>>> $smo.Databases["AdventureWorks"].Tables | Where-Object
TM>>>>> {$_.Schema -eq "Sales"} | Select-Object Indexes
TM>>>>>
TM>>>>> ... I get the following output:
TM>>>>>
TM>>>>> Indexes
TM>>>>> -------
TM>>>>> {PK_ContactCreditCard_ContactID_CreditCardID}
TM>>>>> {IX_CountryRegionCurrency_CurrencyCode,
TM>>>>> PK_CountryRegionCurrency_CountryRegionCode_CurrencyCode}
TM>>>>> {AK_CreditCard_CardNumber, PK_CreditCard_CreditCardID}
TM>>>>> {AK_Currency_Name, PK_Currency_CurrencyCode}
TM>>>>> {AK_CurrencyRate_CurrencyRateDate_FromCurrencyCode_ToCurrencyCod
TM>>>>> e
TM>>>>> ,
TM>>>>> PK_CurrencyRate_CurrencyRateID}
TM>>> ----------------------------------------------------
TM>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
TM>>> SQL Server MVP
TM>>> Toronto, ON Canada
TM>>>
https://mvp.support.microsoft.com/profile/Tom.Moreau
TM>>> "Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in message
TM>>> news:29d4f64638818ca27dc0111014c@xxxxxx
TM>>> What do you get back when you do this.
TM>>> $smo.Databases["AdventureWorks"].Tables | Where-Object {$_.Schema
TM>>> -eq
TM>>> "Sales"}|
TM>>> Select-Object Indexes
TM>>> Brandon Shell
TM>>> ---------------
TM>>> Blog:
http://www.bsonposh.com/
TM>>> PSH Scripts Project:
www.codeplex.com/psobject
TM>>>> No, I get this back:
TM>>>>
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>> @{Indexes=Microsoft.SqlServer.Management.Smo.IndexCollection}
TM>>>>
TM>>>> It really is supposed to be a collection of Index objects.
TM>>>>
TM>>>> ----------------------------------------------------
TM>>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
TM>>>> SQL Server MVP
TM>>>> Toronto, ON Canada
TM>>>>
https://mvp.support.microsoft.com/profile/Tom.Moreau
TM>>>> "Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in
TM>>>> message
TM>>>> news:29d4f646387e8ca27da71057e80@xxxxxx
TM>>>> It seems to me it is just an array so this should work
TM>>>> $smo.Databases["AdventureWorks"].Tables | Where-Object {$_.Schema
TM>>>> -eq
TM>>>> "Sales"}
TM>>>> | Select-Object Indexes | foreach {Write-Host $_}
TM>>>> Brandon Shell
TM>>>> ---------------
TM>>>> Blog:
http://www.bsonposh.com/
TM>>>> PSH Scripts Project:
www.codeplex.com/psobject
TM>>>>> I am running the following code:
TM>>>>>
TM>>>>> [void][reflection.assembly]::LoadWithPartialName("Microsoft.SqlS
TM>>>>> e r v e r.Smo") $smo = new-object
TM>>>>> Microsoft.SqlServer.Management.Smo.Server ".\SQL2005"
TM>>>>>
TM>>>>> $smo.Databases["AdventureWorks"].Tables | Where-Object
TM>>>>> {$_.Schema -eq "Sales"} | Select-Object Indexes | foreach
TM>>>>> {Write-Host $_.name}
TM>>>>>
TM>>>>> The last statement writes an empty line for each table in the
TM>>>>> Tables collection. (I'm using Write-Host just for debugging. I
TM>>>>> actually want to manipulate each index.) When I run the
TM>>>>> following:
TM>>>>>
TM>>>>> $smo.Databases["AdventureWorks"].Tables | Where-Object
TM>>>>> {$_.Schema -eq "Sales"} | Select-Object Indexes
TM>>>>>
TM>>>>> ... I get the following output:
TM>>>>>
TM>>>>> Indexes
TM>>>>> -------
TM>>>>> {PK_ContactCreditCard_ContactID_CreditCardID}
TM>>>>> {IX_CountryRegionCurrency_CurrencyCode,
TM>>>>> PK_CountryRegionCurrency_CountryRegionCode_CurrencyCode}
TM>>>>> {AK_CreditCard_CardNumber, PK_CreditCard_CreditCardID}
TM>>>>> {AK_Currency_Name, PK_Currency_CurrencyCode}
TM>>>>> {AK_CurrencyRate_CurrencyRateDate_FromCurrencyCode_ToCurrencyCod
TM>>>>> e
TM>>>>> ,
TM>>>>> PK_CurrencyRate_CurrencyRateID}
TM>>>>> ...
TM>>>>> How do I pick up the object within the foreach loop?
TM>>>>> ----------------------------------------------------
TM>>>>> Thomas A. Moreau, BSc, PhD, MCSE, MCDBA, MCITP, MCTS
TM>>>>> SQL Server MVP
TM>>>>> Toronto, ON Canada
TM>>>>>
https://mvp.support.microsoft.com/profile/Tom.Moreau