楼主: ReneeBK
2169 15

SQL Server 2014 with PowerShell v5 Cookbook [推广有奖]

  • 1关注
  • 62粉丝

VIP

已卖:4898份资源

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49640 个
通用积分
55.8137
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57805 点
帖子
4005
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

楼主
ReneeBK 发表于 2017-4-10 09:29:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. Author:Donabel Santos
  2. ISBN-10:1785283324
  3. Year:2016
  4. Pages:760
  5. Language:English
  6. File size:26.81 MB
  7. File format:PDF
  8. Category:ASP.NET Network Administration
复制代码

本帖隐藏的内容

SQL Server 2014 with PowerShell v5 Cookbook.pdf (26.18 MB, 需要: 5 个论坛币)


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Cookbook Server Powers Power Shell Network 2014

沙发
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:33:19
  1. Listing SQL Server instances

  2. In this recipe, we will list all SQL Server instances in the local network.

  3. Getting ready
  4. Log in to the server that has your SQL Server development instance as an administrator.

  5. How to do it...
  6. Let's look at the steps to list your SQL Server instances:

  7. Open PowerShell ISE as administrator.
  8. Let's use the Start-Service cmdlet to start the SQL Browser service:
  9. Import-Module SQLPS -DisableNameChecking

  10. #out of the box, the SQLBrowser is disabled. To enable:
  11. Set-Service SQLBrowser -StartupType Automatic

  12. #sql browser must be installed and running for us
  13. #to discover SQL Server instances
  14. Start-Service "SQLBrowser"
  15. Next, you need to create a ManagedComputer object to get access to instances. Type the following script and run:
  16. $instanceName = "localhost"
  17. $managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $instanceName

  18. #list server instances
  19. $managedComputer.ServerInstances
复制代码

藤椅
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:34:48
  1. Discovering SQL Server services
  2. How to do it...
  3. Let's assume you are running this script on the server box:

  4. Open PowerShell ISE as administrator.
  5. Add the following code and execute:
  6. Import-Module SQLPS -DisableNameChecking

  7. #you can replace localhost with your instance name
  8. $instanceName = "localhost"

  9. $managedComputer = New-Object Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer $instanceName

  10. #list services
  11. $managedComputer.Services |
  12. Select-Object Name, Type, ServiceState, DisplayName |
  13. Format-Table -AutoSize
复制代码

板凳
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:35:44
  1. Starting/stopping SQL Server services
  2. How to do it...
  3. Let's look at the steps to toggle your SQL Server services states:

  4. Open PowerShell ISE as administrator.
  5. Add the following code:
  6. $verbosepreference = "Continue"
  7. $services = @("SQLBrowser", "ReportServer")
  8. $hostName = "localhost"

  9. $services |
  10. ForEach-Object {
  11.    $service = Get-Service -Name $_
  12.    if($service.Status -eq "Stopped")
  13.    {
  14.       Write-Verbose "Starting $($service.Name) ...."
  15.       Start-Service -Name $service.Name
  16.    }
  17.    else
  18.    {
  19.       Write-Verbose "Stopping $($service.Name) ...."
  20.       Stop-Service -Name $service.Name
  21.    }
  22. }
  23. $verbosepreference = "SilentlyContinue"
复制代码

报纸
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:36:57
  1. Starting/stopping SQL Server services
  2. How to do it...
  3. Let's look at the steps to toggle your SQL Server services states:

  4. Open PowerShell ISE as administrator.
  5. Add the following code:
  6. $verbosepreference = "Continue"
  7. $services = @("SQLBrowser", "ReportServer")
  8. $hostName = "localhost"

  9. $services |
  10. ForEach-Object {
  11.    $service = Get-Service -Name $_
  12.    if($service.Status -eq "Stopped")
  13.    {
  14.       Write-Verbose "Starting $($service.Name) ...."
  15.       Start-Service -Name $service.Name
  16.    }
  17.    else
  18.    {
  19.       Write-Verbose "Stopping $($service.Name) ...."
  20.       Stop-Service -Name $service.Name
  21.    }
  22. }
  23. $verbosepreference = "SilentlyContinue"
复制代码

地板
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:37:19
  1. Listing SQL Server configuration settings

  2. This recipe walks through how to list SQL Server configurable and nonconfigurable instance settings using PowerShell.

  3. How to do it...
  4. Let's look at the steps involved in listing SQL Server configuration settings:

  5. Open PowerShell ISE as administrator.
  6. Import the SQLPS module and create a new SMO Server Object:
  7. #import SQL Server module
  8. Import-Module SQLPS -DisableNameChecking

  9. #replace this with your instance name
  10. $instanceName = "localhost"
  11. $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  12. To explore what members and methods are included in the SMO server, use the following:

  13. #Explore: get all properties available for a server object
  14. #see http://msdn.microsoft.com.ezproxy.torontopubliclibrary.ca/en-us/library/ms212724.aspx

  15. $server |
  16. Get-Member |
  17. Where-Object MemberType -eq "Property"
复制代码

7
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:39:41
  1. Creating a database

  2. This recipe walks through creating a database with default properties using PowerShell.

  3. Getting ready
  4. In this example, we are going to create a database called TestDB, and we assume that this database does not yet exist in your instance.

  5. For your reference, the equivalent T-SQL code of this task is as follows:

  6. CREATE DATABASE TestDB
  7. How to do it...
  8. Follow these steps to create a simple database in SQL Server:

  9. Open PowerShell ISE as administrator.
  10. Import the SQLPS module and create a new SMO Server Object:
  11. #import SQL Server module
  12. Import-Module SQLPS -DisableNameChecking

  13. #replace this with your instance name
  14. $instanceName = "localhost"
  15. $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  16. Add the following script and run:
  17. #database TestDB with default settings
  18. #assumption is that this database does not yet exist
  19. $dbName = "TestDB"
  20. $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database($server, $dbName)
  21. $db.Create()

  22. #to confirm, list databases in your instance
  23. $server.Databases |
  24. Select Name, Status, Owner, CreateDate
复制代码

8
ReneeBK(未真实交易用户) 发表于 2017-4-10 09:41:18
  1. Creating a view

  2. How to do it...
  3. Let's check out the steps to create a view using PowerShell:

  4. Open PowerShell ISE as administrator.
  5. Import the SQLPS module and create a new SMO Server object:
  6. #import SQL Server module
  7. Import-Module SQLPS -DisableNameChecking

  8. #replace this with your instance name
  9. $instanceName = "localhost"
  10. $server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $instanceName
  11. Add the following script and run:

  12. $dbName = "AdventureWorks2014"
  13. $db = $server.Databases[$dbName]
  14. $viewName = "vwVCPerson"
  15. $view = $db.Views[$viewName]

  16. #if view exists, drop it
  17. if ($view)
  18. {
  19.    $view.Drop()
  20. }

  21. $view = New-Object -TypeName Microsoft.SqlServer.Management.SMO.View -ArgumentList $db, $viewName, "dbo"

  22. #TextMode = false meaning we are not
  23. #going to explicitly write the CREATE VIEW header
  24. $view.TextMode = $false
  25. $view.TextBody = @"
  26. SELECT
  27.     TOP 100
  28.   BusinessEntityID,
  29.   LastName,
  30.   FirstName
  31. FROM
  32.   Person.Person
  33. WHERE
  34.     PersonType = 'IN'
  35. "@

  36. $view.Create()
  37. Test the view from PowerShell by running the following code:

  38. #code below all in one line
  39. $result = Invoke-Sqlcmd -Query "SELECT * FROM vwVCPerson"
  40. -ServerInstance $instanceName -Database $dbName

  41. #display results
  42. $result | Format-Table -AutoSize
复制代码

9
h2h2(未真实交易用户) 发表于 2017-4-10 10:46:36
谢谢分享

10
franky_sas(未真实交易用户) 发表于 2017-4-10 10:54:43

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-10 01:15