Get Table/Index Filegroup Listing using Powershell

Quick script to get a listing of tables\indexes and what filegroup said object belongs to via powershell.  This returns an array of PSObject.  The tables\indexes can be filtered via the ObjectType in the psobject array.  As always, this uses SQLPSX.

Import-Module SqlServer

$outputs = @();

Get-SqlDatabase -dbname 'database' -sqlserver 'server' | %{
	$db = $_
	Get-SqlTable -Database $_ | %{
		
		$output = New-Object -TypeName PSObject -Property @{
			ObjectName = $_.Name
			FileGroupName = $_.FileGroup
			ObjectType = "Table"
		}
		$outputs += $output
		
		$_.Indexes | %{
			$output = New-Object -TypeName PSObject -Property @{
				ObjectName = $_.Name
				FileGroupName = $_.FileGroup
				ObjectType = "Index"
			}
			$outputs += $output
		}
	}
	Get-SqlView -Database $_ | %{
		$vw = $_
		if($_.HasIndex)
		{
			$_.Indexes | %{
				$output = New-Object -TypeName PSObject -Property @{
					ObjectName = $_.Name
					FileGroupName = $_.FileGroup
					ObjectType = "Index"
				}
				$outputs += $output
			}
			
		}
	}
}

$outputs 

Get Login info using Get-SqlConnection

Quick post for someone on twitter on how to get login information using sqlpsxs’ Get-SqlConnection.  It appears that all you need is a sql login with public server role in order to list the logins on the server.  The only role that this login was in was the public role on the server.  Nothing else.

In order to get the Get-SqlLogins’ –sqlserver I passed in the ServerInstance from the Get-SqlConnection.  You could probably pipe this as Get-SqlLogin –sqlserver $(Get-SqlConnection –…) as well.

Import-Module SqlServer

$slqcn = Get-SqlConnection -sqlserver "YourServerName" -username "LoginName" -password "P@$$w0r9_123"
Get-SqlLogin -sqlserver $slqcn.ServerInstance | SELECT Name, LoginType, Language, IsSystemObject, CreateDate, DefaultDatabase | Format-Table -AutoSize