问题 没有为Windows PowerShell版本2注册管理单元


我正在尝试在未安装SQL Server Management Studio的Web服务器上运行Powershell脚本,但已安装了Microsoft SQL Server 2008 R2 SP2 Feature Pack中的所有相关软件包。您需要安装这些小块,以便Powershell能够运行SQL命令。

然后我运行了这个安装脚本,为使用Powershell运行的SQL Server命令准备环境:

$ErrorActionPreference = "Stop"

$sqlpsreg="HKLM:\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.SqlServer.Management.PowerShell.sqlps"

if (Get-ChildItem $sqlpsreg -ErrorAction "SilentlyContinue")
{
    throw "SQL Server Powershell is not installed."
}
else
{
    $item = Get-ItemProperty $sqlpsreg
    $sqlpsPath = [System.IO.Path]::GetDirectoryName($item.Path)
}



/* Preload the assemblies. Note that most assemblies will be loaded when the provider
 is used. if you work only within the provider this may not be needed. It will reduce
 the shell's footprint if you leave these out.*/

$assemblylist =
"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"


foreach ($asm in $assemblylist)
{
    $asm = [Reflection.Assembly]::LoadWithPartialName($asm)
}


//Set variables that the provider expects (mandatory for the SQL provider)

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


//Load the snapins, type data, format data

Push-Location
cd $sqlpsPath


Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100 
Update-TypeData -PrependPath SQLProvider.Types.ps1xml  
update-FormatData -prependpath SQLProvider.Format.ps1xml  
Pop-Location

Add-PSSnapin SqlServerCmdletSnapin100,脚本失败,出现以下错误:

没有为Windows PowerShell版本2注册管理单元   C:\ Vantiv \ Initialize-SqlpsEnvironment.ps1:75 char:13   + Add-PSSnapin <<<< SqlServerCmdletSnapin100#-ErrorAction SilentlyContinue       + CategoryInfo:InvalidArgument:(SqlServerCmdletSnapin100:String)[Add-PSSnapin],PSArgumentException       + FullyQualifiedErrorId:AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

我对这个错误进行了谷歌搜索,我能找到的唯一解决方案就是人们说我的64位Powershell控制台快捷方式是指向SysWOW64目录而不是System32。对我来说情况并非如此。我指向System32。

有任何想法吗?要让Powershell注册这些管理单元,我需要做些什么?


9132
2017-11-09 15:57


起源



答案:


如果你这样做:

Get-PSSnapin -Registered

您将获得PowerShell的即用型管理单元列表(这里只是SQL的管理单元):

Name        : SqlServerCmdletSnapin100
PSVersion   : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name        : SqlServerProviderSnapin100
PSVersion   : 2.0
Description : SQL Server Provider

如果您无法在列表中看到这些管理单元,请尝试发布的解决方案 这里


7
2017-11-09 16:15



链接的解决方案就是答案。谢谢! - crackedcornjimmy


答案:


如果你这样做:

Get-PSSnapin -Registered

您将获得PowerShell的即用型管理单元列表(这里只是SQL的管理单元):

Name        : SqlServerCmdletSnapin100
PSVersion   : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server cmdlets.

Name        : SqlServerProviderSnapin100
PSVersion   : 2.0
Description : SQL Server Provider

如果您无法在列表中看到这些管理单元,请尝试发布的解决方案 这里


7
2017-11-09 16:15



链接的解决方案就是答案。谢谢! - crackedcornjimmy


与上述答案略有相似之处,我发现默认情况下我使用的是32位运行的Visual Studio命令提示符。

因此,当对我的powershell库使用“InstallUtil”时,对32位版本的Powershell进行了注册,所以我很困惑,我在-Registered集合中找不到我的cmdlet。

如果我随后启动了powershell(x86),那么我的snapins确实按预期注册了。因此,对我来说,解决方案是从x64命令提示符重新注册我的snapins,或者只使用x86版本的powershell。


3
2018-01-16 15:33





Windows Server 2008 R2 PowerShell 修复我是复制 PowerShell 从工作服务器到有问题的注册表项。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\ 

问题解决了。该 SharePoint PowerShell snapin现已注册,并且已安装 SharePoint 作品。


0
2018-01-23 15:21