powershell connects to sqlserver

来源:互联网 发布:下载桌面软件 编辑:程序博客网 时间:2024/05/01 19:34

How to issue sql query to sql server database by using powershell ?


Here is the Link : http://sev17.com/2010/07/making-a-sqlps-module/


Steps:

1) Make a module 

    1.1) Copy file/folder under 100/Binn to powershell/vx.0/modules/sqlps

  • en
  • Microsoft.SqlServer.Management.PSProvider.dll
  • Microsoft.SqlServer.Management.PSSnapins.dll
  • SQLProvider.Format.ps1xml
  • SQLProvider.Types.ps1xml

    1.2) create a file named "sqlps.psd1"

@{

ModuleVersion="0.0.0.1"Description="A Wrapper for Microsoft's SQL Server PowerShell Extensions Snapins"Author="Chad Miller"Copyright="© 2010, Chad Miller, released under the Ms-PL"CompanyName="http://sev17.com"CLRVersion="2.0"FormatsToProcess="SQLProvider.Format.ps1xml"NestedModules="Microsoft.SqlServer.Management.PSSnapins.dll","Microsoft.SqlServer.Management.PSProvider.dll"RequiredAssemblies="Microsoft.SqlServer.Smo","Microsoft.SqlServer.Dmf","Microsoft.SqlServer.SqlWmiManagement","Microsoft.SqlServer.ConnectionInfo","Microsoft.SqlServer.SmoExtended","Microsoft.SqlServer.Management.RegisteredServers","Microsoft.SqlServer.Management.Sdk.Sfc","Microsoft.SqlServer.SqlEnum","Microsoft.SqlServer.RegSvrEnum","Microsoft.SqlServer.WmiEnum","Microsoft.SqlServer.ServiceBrokerEnum","Microsoft.SqlServer.ConnectionInfoExtended","Microsoft.SqlServer.Management.Collector","Microsoft.SqlServer.Management.CollectorEnum"TypesToProcess="SQLProvider.Types.ps1xml"ScriptsToProcess="Sqlps.ps1"}

    1.3) craete a file named "sqlps.ps1"

Set-Variable -scope Global -name SqlServerMaximumChildItems -Value 0Set-Variable -scope Global -name SqlServerConnectionTimeout -Value 30Set-Variable -scope Global -name SqlServerIncludeSystemObjects -Value $falseSet-Variable -scope Global -name SqlServerMaximumTabCompletion -Value 1000

2) import-module SQLPS 


Now you can issues query to sql server by using invoke-sqlcmd -query xxxx -database xxxx -serverinstance xxxx

原创粉丝点击