异常调用" Put"用" 0"参数:""在Win32_TerminalServiceSetting上

时间:2017-07-24 09:23:34

标签: powershell terminal-services

我创建了一个Powershell函数,用于在服务器上远程启用或禁用会话登录。它基本上是Powershell等同于"更改登录/启用"。

它可以在大多数机器上运行,但由于某些原因我不明白,对于某些人它会返回以下错误:

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()

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

事实证明,在Windows Server 2008R2上,如果未安装远程桌面会话主机角色,则服务器配置为“远程桌面管理”。

正如this technet article所述:

  

以下是远程桌面管理的限制:

     
      
  1. 默认连接(RDP-Tcp)最多只允许两个远程连接。

  2.   
  3. 无法配置许可设置。

  4.   
  5. 无法配置RD连接代理设置。

  6.   
  7. 无法配置用户登录模式。

  8.   

所以最后我必须抓住那个例外并在那个特定情况下恢复使用change logon /disable

感谢@GrigorySergeev指出我正确的方向!