如何利用PowerShell来进行针对数据库的操作,比如查询、更新表格等等?
听起来挺复杂哦!不过不用担心,我也不是数据库方面的专家,我能够利用PS来查询数据库,你就一定也可以。
好了,第一步!
一般来说IT技术人员遇到问题,第一步应该打开www.google.com,不过今天很幸运,我手头正好有一本书叫做《Windows PowerShell Cookbook》,2007年5月由OReilly出版的。与大多数讲PowerShell的书不同,这本“PowerShell菜谱”不着重讲解PS的语言,而是罗列了若干管理员所要面对的问题,然后给出PowerShell的解决方法,大家有兴趣不妨也下载看看。
从这本书上我看到下面一段针对数据库的脚本,写得非常好,不用改,拿过来就可以用,好东西不敢藏私,与大家分享。
- ##############################################################################
- ##
- ## Invoke-SqlCommand.ps1
- ##
- ## From Windows PowerShell Cookbook (O'Reilly)
- ## by Lee Holmes (http://www.leeholmes.com/guide)
- ##
- ## Return the results of a SQL query or operation
- ##
- ## ie:
- ##
- ## ## Use Windows authentication
- ## Invoke-SqlCommand.ps1 -Sql "SELECT TOP 10 * FROM Orders"
- ##
- ## ## Use SQL Authentication
- ## $cred = Get-Credential
- ## Invoke-SqlCommand.ps1 -Sql "SELECT TOP 10 * FROM Orders" -Cred $cred
- ##
- ## ## Perform an update
- ## $server = "MYSERVER"
- ## $database = "Master"
- ## $sql = "UPDATE Orders SET EmployeeID = 6 WHERE OrderID = 10248"
- ## Invoke-SqlCommand $server $database $sql
- ##
- ## $sql = "EXEC SalesByCategory 'Beverages'"
- ## Invoke-SqlCommand -Sql $sql
- ##
- ## ## Access an access database
- ## Invoke-SqlCommand (Resolve-Path access_test.mdb) -Sql "SELECT * FROM Users"
- ##
- ## ## Access an excel file
- ## Invoke-SqlCommand (Resolve-Path xls_test.xls) -Sql 'SELECT * FROM [Sheet1$]'
- ##
- ##############################################################################
-
- param(
- [string] $dataSource = ".\SQLEXPRESS",
- [string] $database = "Northwind",
- [string] $sqlCommand = $(throw "Please specify a query."),
- [System.Management.Automation.PsCredential] $credential
- )
-
-
- ## Prepare the authentication information. By default, we pick
- ## Windows authentication
- $authentication = "Integrated Security=SSPI;"
-
- ## If the user supplies a credential, then they want SQL
- ## authentication
- if($credential)
- {
- $plainCred = $credential.GetNetworkCredential()
- $authentication =
- ("uid={0};pwd={1};" -f $plainCred.Username,$plainCred.Password)
- }
-
- ## Prepare the connection string out of the information they
- ## provide
- $connectionString = "Provider=sqloledb; " +
- "Data Source=$dataSource; " +
- "Initial Catalog=$database; " +
- "$authentication; "
-
- ## If they specify an Access database or Excel file as the connection
- ## source, modify the connection string to connect to that data source
- if($dataSource -match '\.xls$|\.mdb
-
- 呵呵,里面都已经有了详细的解释如何使用,就不需要我多做解释了。我用这段脚本查询过sql数据库和access数据库,另外它还可以用来查询excel文档!真的很棒!
-
- http://bbs.winos.cn/thread-28867-1-1.html)
- {
- $connectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dataSource; "
-
- if($dataSource -match '\.xls
-
- 呵呵,里面都已经有了详细的解释如何使用,就不需要我多做解释了。我用这段脚本查询过sql数据库和access数据库,另外它还可以用来查询excel文档!真的很棒!
-
- http://bbs.winos.cn/thread-28867-1-1.html)
- {
- $connectionString += 'Extended Properties="Excel 8.0;"; '
-
- ## Generate an error if they didn't specify the sheet name properly
- if($sqlCommand -notmatch '\[.+\$\]')
- {
- $error = 'Sheet names should be surrounded by square brackets, and ' +
- 'have a dollar sign at the end: [Sheet1$]'
- Write-Error $error
- return
- }
- }
- }
-
- ## Connect to the data source and open it
- $connection = New-Object System.Data.OleDb.OleDbConnection $connectionString
- $command = New-Object System.Data.OleDb.OleDbCommand $sqlCommand,$connection
- $connection.Open()
-
- ## Fetch the results, and close the connection
- $adapter = New-Object System.Data.OleDb.OleDbDataAdapter $command
- $dataset = New-Object System.Data.DataSet
- [void] $adapter.Fill($dataSet)
- $connection.Close()
-
- ## Return all of the rows from their query
- $dataSet.Tables | Select-Object -Expand Rows
复制代码
呵呵,里面都已经有了详细的解释如何使用,就不需要我多做解释了。我用这段脚本查询过sql数据库和access数据库,另外它还可以用来查询excel文档!真的很棒!
http://bbs.winos.cn/thread-28867-1-1.html |