![](/img/trans.png)
[英]Can I use multiple Invoke-Sqlcmd commands in the same PowerShell script against different SQL servers?
[英]SQL installed but powershell not picking up sqlps commands (like invoke-sqlcmd)
我用巧克力安装了 ms sql server:
choco install SQLServer2012DeveloperEditionWithSP1 -y -f -source 'http://choco.developers.tcpl.ca/chocolatey' -c "$env:WINDIR\temp"
SQL 似乎已安装并在无法正常工作的 powershell 之外运行良好。 我可以看到 sqlps 模块:
Get-Module -listavailable
...
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.0 SQLASCMDLETS
Manifest 1.0 SQLPS
但是命令似乎丢失了。 我没有 invoke-sqlcmd 等。理论上,如果我安装模块,我应该可以访问它们,但是当我尝试导入模块 sqlps 时,却收到关于没有 sqlserver 驱动器的错误:
PS C:\WINDOWS\system32> Import-Module SQLPS
Set-Location : Cannot find drive. A drive with the name 'SQLSERVER' does not exist.
At C:\Program Files (x86)\Microsoft SQL Server\110\Tools\PowerShell\Modules\SQLPS\SqlPsPostScript.ps1:1 char:1
+ Set-Location SQLSERVER:
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (SQLSERVER:String) [Set-Location], DriveNotFoundException
+ FullyQualifiedErrorId : DriveNotFound,Microsoft.PowerShell.Commands.SetLocationCommand
我知道我的小组中有几个人完成了这些步骤并且确实使正确的 sql ps 设置正常工作。
任何提示或想法都会非常有帮助。 谢谢。
再会,
我猜您正在使用 SQL Server 2017,因为这是 2017 年的常见问题,我将在下面解释。 我不确定使用的是什么版本,因为这个问题有点旧,并且在 2017 年 5 月 2 日 22:58 被问到
您得到的错误包括基本问题
设置位置:找不到驱动器。 名为“SQLSERVER”的驱动器不存在。
它并不是说模块“SQLPS”不存在,而是模块“SQLSERVER”不存在
解释是直到 2016 年 SQLPS 模块都包含在 SQL Server 安装中,但我们使用的 PowerShell 模块是“SqlServer”模块。 “SqlServer”模块包含在 SQL Server Management Studio (SSMS) 16.x 中,但如果您使用的是 SSMS 2017 (17.x),则必须从 PowerShell Gallery 安装“SqlServer”模块。
安装它的过程是执行命令:
Install-Module -Name SqlServer
如果您收到类似PackageManagement\\Install-Package : The following commands are already available on this system:...
错误PackageManagement\\Install-Package : The following commands are already available on this system:...
然后您可以使用以下参数强制安装: -Force
和–AllowClobber
由于我不熟悉你的系统,我不会建议你做什么或说你是否应该强制安装,但这是我在大多数情况下可能会做的解决方案(根据我在此注意到的信息)线)
Install-Module -Name SqlServer -Force –AllowClobber
为了确认该模块是相反的,您可以执行以下命令:
Get-Module -Name SqlServer -listAvailable | select Name, ModuleType, Version
使用上面的命令检查您的安装版本,并在以下命令中使用它以导入最新版本(在我写这个答案时,版本是 21.0.17279):
Import-Module SqlServer -Version 21.0.17279
就是这样...如果一切顺利,那么您应该能够使用所有 SQL Server PowerShell 命令
正如所指出的,这里是您现在应该参考的链接。
Jarret 给出了答案。 简单地加载模块实际上无济于事。 这组命令必须在那之后运行。
Push-Location
cd $sqlpsPath
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
Update-TypeData -PrependPath SQLProvider.Types.ps1xml
update-FormatData -prependpath SQLProvider.Format.ps1xml
Pop-Location
希望能帮助到你。
只是给未来读者的一个旁注,我试图通过 powershell 创建一个 sql 备份。 cmdlet 在Administrator
帐户下正常运行,但是在普通用户帐户下运行脚本,我收到以下错误:
Cannot find a provider with the name 'SqlServer'
谷歌搜索这个问题把我带到了这里,但我的问题的答案在这里的论坛帖子中: https : //social.technet.microsoft.com/Forums/windowsserver/en-US/626bb81a-00ba-4239-ad0a-fec32546350a/ check-if-drive-exists-if-not-map?forum=winserverpowershell
我遇到了一个奇怪的问题,希望有人能解决这个问题。
Microsoft SQL Server 2014 (SP2-GDR) (KB4505217) - 12.0.5223.6 (X64)(是的,我知道......这是一个开发服务器)
视窗 2012 R2
PS版本 4.0
我在 PS 脚本中加载 SQL 程序集
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO") | 出空
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended") | 出空
如果我在加载程序集后立即运行它,以下将失败
Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME
Get-ChildItem:找不到驱动器。 名为“SQLSERVER”的驱动器不存在。
如果我先运行以下,我很好。
Invoke-Sqlcmd -Query "SELECT TOP 1 * FROM sys.sysobjects" | 出空
Get-ChildItem SQLSERVER:\\SQL$env:COMPUTERNAME
我只是想修改我只需要运行
调用-Sqlcmd | 出空
这似乎然后将程序集完全加载到内存中,之后一切正常。
所以我将此添加到我的脚本中:
# SQL cmdlets below need some dll imports from system
# These do not get loaded sometime when running under an non-admin account (Cannof find a provider with the name 'SqlServer')
# Running this dummy command seems to load all needed dlls
# Also see: https://www.sqlservercentral.com/forums/topic/unable-access-sql-provider-in-powershell-without-running-an-invoke-sqlcmd-first
Invoke-Sqlcmd | Out-Null
似乎有价值的信息不应该丢失,所以我想我会在谷歌搜索该特定错误时将其发布在最高的 SO 中。
有人可能会争辩说,安装模块而不是这种虚拟方法会更干净。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.