Get-MSSQL-ServerAttrib-Csv


posted by Richard Giles
10-06-2008

Downloads: 496
File size: 2.4kB
Views: 1,265

Embed
Get-MSSQL-ServerAttrib-Csv
  1. ## ===================================================================== 
  2. ## Title       : Get-MSSQL-ServerAttrib-Csv 
  3. ## Description : Connect to SQL Server and output server attributes to CSV 
  4. ## Author      : Idera 
  5. ## Date        : 9/1/2008 
  6. ## Input       : -serverInstance <server\instance> 
  7. ##                   -tempDir <output path> 
  8. ##                   -verbose  
  9. ##                   -debug     
  10. ## Output      : CSV file with server attributes 
  11. ## Usage            : PS> . Get-MSSQL-ServerAttrib-Csv -serverInstance MyServer  
  12. ##                             -tempDir C:\TEMP\ -v -d 
  13. ## Notes            :  
  14. ## Tag            : SQL Server, Attributes, CSV 
  15. ## Change log  : 
  16. ## ===================================================================== 
  17.  
  18. param 
  19.     [string]$serverInstance = "(local)"
  20.       [string]$tempDir = "C:\TEMP\"
  21.     [switch]$verbose
  22.     [switch]$debug 
  23.  
  24. function main() 
  25.     if ($verbose) {$VerbosePreference = "Continue"
  26.     if ($debug) {$DebugPreference = "Continue"
  27.     Get-MSSQL-ServerAttrib-Csv $serverInstance $tempDir 
  28.  
  29. function Get-MSSQL-ServerAttrib-Csv($serverInstance, $tempDir
  30.     trap [Exception]  
  31.     
  32.         write-error $("TRAPPED: " + $_.Exception.Message); 
  33.         continue
  34.     
  35.  
  36.     # Create fully qualified output filename 
  37.     $outputFile = $tempDir + "SQLServerAttributes.csv" 
  38.     Write-Debug "Output directory: $outputFile" 
  39.      
  40.     # Validate path to temp directory 
  41.     if (-not (Test-Path -path $tempDir))  
  42.     
  43.         Write-Host Unable to validate path to temp directory: $tempDir 
  44.         break 
  45.     
  46.      
  47.     # Load SMO assemblies 
  48.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo"
  49.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SqlEnum"
  50.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoEnum"
  51.     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo"
  52.      
  53.     # Create a Server object for default instance 
  54.     Write-Debug "Connecting to server: $ServerInstance"  
  55.     $NamedInstance = New-Object ('Microsoft.SqlServer.Management.Smo.Server') ($serverInstance
  56.  
  57.     # Get server attributes with EnumServerAttributes() method and output to CSV 
  58.     #   and overwrite existing file 
  59.     Write-Debug "Outputing $outputFile..." 
  60.     $NamedInstance.EnumServerAttributes() | Export-Csv -path $outputFile 
  61.      
  62.     # Cleanup 
  63.     # TIP: variables will go out of scope when the function ends 
  64.     #      this is a why to specifically dispose them 
  65.     remove-variable NamedInstance  
  66.     remove-variable TempDir 
  67.     remove-variable OutputFile 
  68.  
  69. main 
Filed under: , ,

Connect to SQL Server and output server attributes to CSV.

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.