签到成功

知道了

CNDBA社区CNDBA社区

使用PowerShell 管理 Linux 上的 SQL Server

2018-12-29 16:13 2867 0 原创 SQL Server
作者: Expect-乐

1.安装SQL Server PowerShell

首先要安装最新的SQL PowerShell,因为SQL PowerShell包含SQL Server Management Studio (SSMS)工具。http://www.cndba.cn/Expect-le/article/3217

备注:
SQL Server PowerShell 模块有两种;SqlServer 和 SQLPS。 虽然 SQL Server 安装附带了 SQLPS 模块(用于向后兼容),但该模块不再更新。 最新的 PowerShell 模块是 SqlServer 模块。 SqlServer 模块不仅包含 SQLPS 更新版本的 cmdlet,并且还包含新的 cmdlet 以支持最新的 SQL 功能。
虽然 SQL Server Management Studio (SSMS) 随附了以前版本的 SqlServer 模块,但仅限 16.x 版本的 SSMS。 要在 SSMS 17.0 和更高版本中使用 PowerShell,则必须从 PowerShell 库安装 SqlServer 模块。 http://www.cndba.cn/Expect-le/article/3217http://www.cndba.cn/Expect-le/article/3217

如果已安装了PowerShell,则安装SqlServer模块即可,如下:http://www.cndba.cn/Expect-le/article/3217http://www.cndba.cn/Expect-le/article/3217

2.安装SqlServer模块

通过cmd调用powershell命令,输入:Install-Module -Name SqlServer即可。如下:

http://www.cndba.cn/Expect-le/article/3217

  • 第一次安装SqlServer 模块
PS C:/Users/Administrator> Install-Module -Name SqlServer

不受信任的存储库
你正在从不受信任的存储库安装模块。如果你信任该存储库,请通过运行 Set-PSRepository cmdlet 更改其 InstallationPolicy
值。是否确实要从“PSGallery”安装模块?
[Y] 是(Y)  [A] 全是(A)  [N] 否(N)  [L] 全否(L)  [S] 暂停(S)  [?] 帮助 (默认值为“N”): y
  • 如果已经安装了旧版本的 SqlServer 模块,则可使用 Update-Module或提供 -AllowClobber 参数:
    Install-Module -Name SqlServer -AllowClobber
  • 如果不能以管理员身份运行 PowerShell 会话,则可为当前用户安装:
    Install-Module -Name SqlServer -Scope CurrentUser
  • 使用 Update-Module 更新SqlServer:
    Update-Module -Name SqlServer
  • 查看已安装模块的版本:
    Get-Module SqlServer -ListAvailable

更多命令,请参考官方文档:https://docs.microsoft.com/zh-cn/sql/powershell/download-sql-server-ps-module?view=sql-server-2017http://www.cndba.cn/Expect-le/article/3217

由于下载速度较慢,也可以手动下载包来安装,地址:
https://psg-prod-eastus.azureedge.net/packages/sqlserver.21.0.17279.nupkg
安装:
将sqlserver.21.0.17279.nupkg解压出来,将文件夹重命名为SqlServer,然后复制到C:/Program Files/WindowsPowerShell/Modules目录下即可
然后在powershell命令行中查看:http://www.cndba.cn/Expect-le/article/3217

http://www.cndba.cn/Expect-le/article/3217

PS C:/Users/Administrator> Import-Module -Name SqlServer
PS C:/Users/Administrator> Get-Module -Name SqlServer

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     21.0.17279 SqlServer                           {Add-RoleMember, Add-SqlAvailabilityDatabase, Add-SqlAvailabilityGroupListenerS...

3.连接SQLServer

  • 通过输入IP地址,用户名,密码。下面通过SqlServer模块来获取服务器的信息:
PS C:/Users/Administrator> $serverInstance="192.168.30.228"  --主机名,IP地址
PS C:/Users/Administrator> $credential = Get-Credential

位于命令管道位置 1 的 cmdlet Get-Credential
请为以下参数提供值:
Credential
PS C:/Users/Administrator> [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
PS C:/Users/Administrator> $server = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $serverInstance
PS C:/Users/Administrator> $server.ConnectionContext.LoginSecure=$false
PS C:/Users/Administrator> $server.ConnectionContext.set_Login($credential.UserName)
PS C:/Users/Administrator> $server.ConnectionContext.set_SecurePassword($credential.Password)
PS C:/Users/Administrator> $server.Information | Select-Object Edition, HostPlatform, HostDistribution | Format-List

Edition          : Developer Edition (64-bit)
HostPlatform     : Linux
HostDistribution : Red Hat Enterprise Linux Server
  • 检查 SQL Server 错误日志
    PS C:/Users/Administrator> $serverInstance="192.168.30.228"  --主机名,IP地址
    PS C:/Users/Administrator> $credential = Get-Credential
    位于命令管道位置 1 的 cmdlet Get-Credential
    请为以下参数提供值:
    Credential
    PS C:/Users/Administrator> Get-SqlErrorLog -ServerInstance $serverInstance -Credential $credential -Since Yesterday | Out-GridView
    
    如果有错误日志,会自动打开一个文本文档显示详细的错误信息。如下:
    在这里插入图片描述

以上就是关于通过PowerShell中调用SqlServer模块来连接,管理Linux SQLServer一些简单演示。

http://www.cndba.cn/Expect-le/article/3217

版权声明:本文为博主原创文章,未经博主允许不得转载。

powershell sqlserver

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
Expect-乐

Expect-乐

关注

Without the continuous bitter cold, there can be no fragrant plum blossom

  • 336
    原创
  • 6
    翻译
  • 100
    转载
  • 41
    评论
  • 访问:1592732次
  • 积分:1957
  • 等级:核心会员
  • 排名:第4名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ