无法使用 powershell 获取数据库或其对象

Mr_*_*Mac 6 sql-server-2008 sql-server powershell

我的服务器和开发电脑都安装了 Powershell。在我的开发机器上,显然安装了最新版本的 PowerShell 或其某些 SQL Server 'sqlps' 组件。我目前的问题是我无法像在开发机器中那样列出数据库,如下所示:

PS SQLSERVER:\> ls SQLSERVER:\\SQL\server\instance\Databases\
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

Get-ChildItem : No se encuentra la ruta de acceso 'SQLSERVER:\SQL\serverr2\serverr2\Databases' porque no existe.
En línea: 1 Carácter: 4
+ dir <<<<  SQLSERVER:\SQL\server\instance\Databases
    + CategoryInfo          : ObjectNotFound: (SQLSERVER:\SQL\server\instance\Databases:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
Run Code Online (Sandbox Code Playgroud)

显然,我无法获得诸如存储过程、视图等之类的子项。

但是,我可以列出已安装的实例,并且还可以通过列出带有以下内容的数据库来解决此问题:

DIR SQLSERVER:\\SQL\serverr2 | SELECT Databases -ExpandProperty Databases | SELECT Name,Owner,Urn
Run Code Online (Sandbox Code Playgroud)

但仍然需要获取另一个对象,如存储过程、视图、函数等。

这是 Get-Host cmdlet 提供的信息

Name             : ConsoleHost
Version          : 2.0
InstanceId       : c1976472-19c0-439e-a4f6-debe59a18616
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : es-MX
CurrentUICulture : es-ES
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
Run Code Online (Sandbox Code Playgroud)

SQL Server 的@@version 是 Microsoft SQL Server 2008 (SP1) - 10.0.2531.0 (X64)

编辑:根据$PSVersionTable.PSVersion我的服务器,我已经安装了 PowerShell 2.0。我还发现我不能,Import-Module sqlps但由于某种原因,我能够做到,dir正如我已经描述的那样(显然存在当前的问题)。

Mr_*_*Mac 4

我在serverfault中找到了答案:

https://serverfault.com/questions/355014/sql-server-powershell-cannot-find-path-error

我必须运行一个脚本才能在 PowerShell 中正确加载管理单元:

#
# Add the SQL Server provider.
#

$ErrorActionPreference = "Stop"

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

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


#
# Set mandatory variables for the SQL Server rovider
#
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
Run Code Online (Sandbox Code Playgroud)

加载后,我可以列出该数据库实例中的每个对象