Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server powershell集数据库IndexOutOfRangeException_Sql Server_Powershell - Fatal编程技术网

Sql server powershell集数据库IndexOutOfRangeException

Sql server powershell集数据库IndexOutOfRangeException,sql-server,powershell,Sql Server,Powershell,我正在尝试使用Microsoft.SqlServer.SMO在powershell中设置数据库名称。当我执行脚本时,它会错误运行,错误文本如下: format-default : Index was outside the bounds of the array. + CategoryInfo : NotSpecified: (:) [format-default], IndexOutOfRangeException + FullyQualifiedError

我正在尝试使用Microsoft.SqlServer.SMO在powershell中设置数据库名称。当我执行脚本时,它会错误运行,错误文本如下:

format-default : Index was outside the bounds of the array.
    + CategoryInfo          : NotSpecified: (:) [format-default],  IndexOutOfRangeException
    + FullyQualifiedErrorId : System.IndexOutOfRangeException,Microsoft.PowerShell.Commands.FormatDefaultCommand
服务器设置如下

$srv = New-Object "Microsoft.SqlServer.Management.SMO.Server" $server 
$srv.ConnectionContext.LoginSecure=$false;
$srv.ConnectionContext.set_Login("login");
$srv.ConnectionContext.set_Password("password")  
$srv.Databases |选择名称
, 显示正确的数据库,但在设置数据库时

$db = $srv.Databases[$database]
错误被抛出

这个脚本在其他SQLServer中也可以使用。
有没有解决此问题的方法?

脚本抛出错误的服务器上似乎不存在数据库“CRD_DEV”。 该用户或该用户无权访问该数据库


引发
索引自动失效异常
,因为
$srv.Databases
中没有与“CRD\u DEV”匹配的项

脚本引发错误的服务器上似乎不存在数据库“CRD\u DEV”。 该用户或该用户无权访问该数据库


由于
$srv.Databases
中没有与“CRD_DEV”

匹配的项,
$database
设置为什么,因此引发了
索引自动失效异常?b) 
$srv.Databases.GetType()
的输出是什么?PS U:\>$srv.Databases.GetType()IsPublic IsSerial名称基类型-----------------------True False DatabaseCollection Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBasePS U:\>$database CRD\U DEVa)将
$database
设置为什么?b)
$srv.Databases.GetType()
的输出是什么?PS U:\>$srv.Databases.GetType()IsPublic IsSerial名称基类型-----------------------True False DatabaseCollection Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBasePS U:\>$database CRD\U DEV当我列出该特定服务器中的数据库时,它会在master、model、msdb和tempdb中显示CRD\U DEV。奇怪的是,我可以使用脚本中使用的相同凭据登录MS SQL Server Management Studio。登录与打开数据库不同。能否在MS SQL Server Management Studio中展开数据库并对表进行选择?这是否与版本相关?当我想在MS SQL Server Management Studio中检查数据库的属性时,它会显示无法读取属性UserAccess。此属性在SQL Server 7.0上不可用。(Microsoft.SqlServer.Smo)是否可以输出此变量
$srv.Databases
?在哪里运行powershell?它是在SQL Server控制台中吗?当我列出该特定服务器中的数据库时,它会在master、model、msdb和tempdb中显示CRD_DEV。奇怪的是,我可以使用脚本中使用的相同凭据登录MS SQL Server Management Studio。登录与打开数据库不同。能否在MS SQL Server Management Studio中展开数据库并对表进行选择?这是否与版本相关?当我想在MS SQL Server Management Studio中检查数据库的属性时,它会显示无法读取属性UserAccess。此属性在SQL Server 7.0上不可用。(Microsoft.SqlServer.Smo)是否可以输出此变量
$srv.Databases
?在哪里运行powershell?它是否在SQL Server控制台中?