首页 > 解决方案 > 通过 PowerShell 连接到 SQL 2014

问题描述

这让我很难过。

.NET 4.7 版 Powershell 5.1

我正在尝试使用 :new-object System.Data.SqlClient.SqlConnection语句连接到 MS-SQL 服务器。

代码在以管理员身份运行时连接,但在作为本地管理员组成员的另一个用户运行时失败。用户拥有(我认为)所有必需的 SQL 权限,并且没有进行 SQL 强化。它是服务器上的系统管理员,具有 Windows 帐户和 SQL 帐户。

使用

    try { new-object System.Data.SqlClient.SqlConnection }
catch { write-host $_.Exception.ToString() }

简单地捕获异常。

它返回 Requested registry access is not allowed 使用 ProcMon 时,两个用户之间的注册表调用绝对没有区别,除了在管理员下 3 个线程退出并且在用户下只有 1 个。

我不相信这是一个实际的注册表权限,因为我在实验室中为该帐户提供了 HKLM 和 HKCU 的明确权限。

如果有人看到这个和/或可以推荐修复,请联系我。

(该脚本必须作为用户帐户运行,因为它是从 SQL 内部调用的,因此在 SQL Server 代理的服务帐户上下文中调用)

这是一个例外:

try { new-object System.Data.SqlClient.SqlConnection }
catch { write-host $_.Exception.ToString() }
System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "0" argument(s): "The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception." ---> System
.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClien
t.SqlConnectionFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---> System.Security.Se
curityException: **Requested registry access is not allowed.**
   at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource)
   at Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable)
   at System.Diagnostics.PerformanceCounterLib.FindCustomCategory(String category, PerformanceCounterCategoryType& categoryType)
   at System.Diagnostics.PerformanceCounterLib.IsCustomCategory(String machine, String category)
   at System.Diagnostics.PerformanceCounter.InitializeImpl()
   at System.Diagnostics.PerformanceCounter.set_RawValue(Int64 value)
   at System.Data.ProviderBase.DbConnectionPoolCounters.Counter..ctor(String categoryName, String instanceName, String counterName, PerformanceCounterType counterType)
   at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
   at System.Data.SqlClient.SqlPerformanceCounters..ctor()
   at System.Data.SqlClient.SqlPerformanceCounters..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnectionFactory..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
   at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
   at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArguments)
   --- End of inner exception stack trace ---
   at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(MethodInformation methodInformation, Object[] arguments, Object[] originalArguments)
   at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type, ConstructorInfo[] constructors, Object[] arguments)
   at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type, ConstructorInfo[] constructors, Object[] args)

PS SQL服务器:>

标签: sqlpowershell

解决方案


推荐阅读