使用“0”参数调用 "Put" 的异常:Win32_TerminalServiceSetting 上的“”

Exception calling "Put" with "0" argument(s): "" on Win32_TerminalServiceSetting

我创建了一个 Powershell 函数来在服务器上远程启用或禁用会话登录。它基本上相当于 "change logon /enable".

的 Powershell

它适用于大多数机器,但出于某种原因我不明白,对于某些机器 returns 出现以下错误:

Exception             : System.Management.Automation.MethodInvocationException: Exception calling "Put" with "0" argument(s): "" ---> System.IO.FileNotFoundException
                           at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
                           at System.Management.ManagementObject.Put(PutOptions options)
                           at System.Management.ManagementObject.Put()
                           at Put(Object , Object[] )
                           at System.Management.Automation.MethodInformation.Invoke(Object target, Object[] arguments)
                           at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] 
                        originalArguments)
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.DotNetAdapter.AuxiliaryMethodInvoke(Object target, Object[] arguments, MethodInformation methodInformation, Object[] 
                        originalArguments)
                           at System.Management.Automation.DotNetAdapter.MethodInvokeDotNet(String methodName, Object target, MethodInformation[] methodInformation, 
                        PSMethodInvocationConstraints invocationConstraints, Object[] arguments)
                           at System.Management.Automation.DotNetAdapter.MethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, Object[] arguments)
                           at System.Management.Automation.Adapter.BaseMethodInvoke(PSMethod method, PSMethodInvocationConstraints invocationConstraints, Object[] arguments)
                           at System.Management.Automation.PSMethod.Invoke(PSMethodInvocationConstraints invocationConstraints, Object[] arguments)
                           at System.Management.Automation.PSMethod.Invoke(Object[] arguments)
                           at System.Management.Automation.Language.PSInvokeMemberBinder.InvokeAdaptedMember(Object obj, String methodName, Object[] args)
                           at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
                           at System.Management.Automation.Interpreter.DynamicInstruction`2.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
TargetObject          : 
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : DotNetMethodException
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

错误堆栈跟踪对我没有帮助。

我 运行 在所有机器上使用相同的本地管理员用户使用相同的代码。

这是实际的代码片段:

$TSConnector = Get-WmiObject -Class Win32_TerminalServiceSetting -Namespace "root/cimv2/terminalservices" -Authentication PacketPrivacy
$TSConnector.Logons = 0
$TSConnector.Put()

有什么想法吗?

事实证明,在 Windows 服务器 2008R2 上,如果未安装远程桌面会话主机角色,则服务器配置为 "Remote Desktop for Administration"。

this technet article 所述:

The following are limitations of Remote Desktop for Administration:

  1. The default connection (RDP-Tcp) only allows a maximum of two simultaneous remote connections.

  2. Licensing settings cannot be configured.

  3. RD Connection Broker settings cannot be configured.

  4. User logon mode cannot be configured.

所以最后我不得不捕获那个异常并在那个特定情况下恢复使用 change logon /disable

感谢@GrigorySergeev 为我指明了正确的方向!