I’m trying to get a WorkFlow that will manage Sql Database Indexes. The intention is to pass a Sql Server Instance name and have the WorkFlow get the databases, then in parallel get the tables in each database, then in parallel get the indexes in each table, and then in parallel check for fragmentation and return the results.
In the WorkFlow I reference other Functions, all of which have been tested and work as expected when used outside of the WorkFlow. Calling the WorkFlow returns nothing and I cant figure out where I’ve gone wrong.
Any guidance, suggestions, greatfully received.
WorkFlow Manage-Indexes
{
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
$SqlServer
)
InlineScript {
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $using:SqlServer
$Dbs = $srv.Databases | ?{$_.Name -Like "csdev*"}
}
foreach -parallel ($Db in $Dbs ) {
$Db
Get-Tables -SqlServer $using:SqlServer -DbID $($Db.ID)
$Tables = Get-Tables -SqlServer $using:SqlServer -DbID $($Db.ID)
foreach -parallel ($tbid in $tables.id) {
$tbid
$Indexes = Get-Indexes -SqlServer $using:SqlServer -DbID $($Db.ID) -TbID $TbID
$Indexes
foreach -parallel ($idx in $Indexes) {
$idx
$Results = Get-Fragmentation -SqlServer $idx.SqlServer -DbID $idx.ID -TbID $idx.TbID -IxID $idx.IxID
$Results
}
}
}
}
If I call the Functions in the WorkFlow outside of the WorkFlow I get results expected.
cls
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | out-null
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $SqlServer
$Dbs = $srv.Databases | ?{$_.Name -Like "csdev*"}
foreach ($Db in $Dbs ) {
$Tables = Get-Tables -SqlServer $SqlServer -DbID $($Db.ID)
foreach($tbid in $tables) {
$Indexes = Get-Indexes -SqlServer $SqlServer -DbID $($Db.ID) -TbID $($TbID.ID)
foreach($idx in $Indexes) {
$Results = Get-Fragmentation -SqlServer $SqlServer -DbID $($Db.ID) -TbID $($TbID.ID) -IxID $($idx.IxID)
$Results
}
}
}
Returns: DbID : 8 TbID : 935010412 IxID : 3 Fragmentation : 0 PageCount : 1 DbID : 8 TbID : 935010412 IxID : 4 Fragmentation : 0 PageCount : 1 DbID : 8 TbID : 935010412 IxID : 1 Fragmentation : 50 PageCount : 2 DbID : 8 TbID : 935010412 IxID : 2 Fragmentation : 0 PageCount : 1