1. # Method 1
  2. # Pros: Does not require installation of additional items other than SMO.
  3. # Cons: A bit more complex than other methods.
  4. # Recommendation: Use SMO ExecuteWithResults when you want to build
  5. # scripts/functions which are not dependent on additional installations.
  6.  
  7. [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
  8. $srvr = new-object ("Microsoft.SqlServer.Management.Smo.Server") "CYG7007"
  9. $db = $srvr.Databases["ICT"]
  10. $db.ExecuteWithResults("SELECT * FROM tbl_ServerUptimeData")
  11.  
  12. # Method 2
  13. # Pros: Simple syntax, supports a wide range of options including XQuery
  14. # Cons: Requires installation of SQL 2008 Powershell host (sqlps.exe)
  15. # Recommendation: Use Invoke-SqlCmd if you have SQL Server 2008 Powershell
  16. # host (sqlps.exe) installed.
  17.  
  18. Invoke-Sqlcmd -Query "SELECT * FROM [ICT].[DBO].[tbl_ServerUptimeData]"
  19. -ServerInstance "SQLSERVER"
  20.  

Last Updated (Wednesday, 20 May 2009 19:13)