查询远程桌面服务状态:

get-wmiobject -query "select * from win32_service where name='TermService'" | out-string

下面自定义函数,在本地执行下面脚本,开启和关闭远程桌面,win7 、2008可以执行,在win 2003需要稍微修改一点

 

[javascript] view plaincopy

  1. write-host "---------------华丽的分割线------------------"    
  2. function OTR(){    
  3. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  4.     $r=$ts.AllowTSConnections    
  5.     if($r -eq 1)    
  6.     {    
  7.     Write-Host "远程桌面处于开启状态,无需重复开启"    
  8.     }else{    
  9.     $ts.SetAllowTSConnections(1) | Out-Null    
  10.     Write-Host "远程桌面开启成功"    
  11.     }    
  12. }    
  13. function CTR(){  
  14. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  15. $r=$ts.AllowTSConnections    
  16.     if($r -eq 0)    
  17.     {    
  18.     Write-Host "远程桌面处于关闭状态,无需重复关闭"    
  19.     }else{    
  20.     $ts.SetAllowTSConnections(0) | Out-Null    
  21.      Write-Host "远程桌面关闭成功"    
  22.     }    
  23. }  
  24. #查看状态  
  25. function STR(){  
  26. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  27. $r=$ts.AllowTSConnections    
  28.     if($r -eq 0)    
  29.     {    
  30.     Write-Host "远程桌面处于[关闭]状态"    
  31.     }  
  32.     if($r -eq 1)    
  33.     {    
  34.     Write-Host "远程桌面处于[开启]状态"    
  35.     }  
  36. }  
  37. #End查看状态  
  38. #输入控制  
  39. function CustOrderSW()  {    
  40. $ins=read-host "输入命令"  
  41. switch($ins){  
  42. (0)  
  43. {CTR   
  44. CustOrderSW;  
  45. break}  
  46. (1)  
  47. {  
  48. OTR   
  49. CustOrderSW;  
  50. break  
  51. }  
  52. (2)  
  53. {  
  54. STR   
  55. CustOrderSW;  
  56. break}  
  57. default{  
  58. "不存De命令"   
  59. CustOrderSW;  
  60. break  
  61. }  
  62. }  
  63. }  
  64. #end输入控制  
  65. #调用自定义函数  
  66. CustOrderSW  

上面自定义函数放在win 2003下执行却无效,报错

Get-WmiObject : 无效名称空间 
所在位置 行:1 字符: 18
+ $ts=get-WMIObject <<<<  Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

原因是WMI版本不同,在win 2003的WMI里面的 ROOT\CIMV2里面没有TerminalServices对象

 

解决办法很简单,因为在2003里面远程控制对象时在CIMV2命名空间下面

win2003这样写:$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2

win2008这样写:$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices

--------------------------------------------------------------------------------------------------------------------------------------------

Win server 2003的WMI版本

PowerShell探秘(4)WMI对象:远程桌面开关

------------------

 

Win7 x64的WMI版本

 

PowerShell探秘(4)WMI对象:远程桌面开关

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------

下面代码放在web版中可执行,主要是没用write-host输出,其他没改变

搜索

 

[javascript] view plaincopy

  1.     
  2. function OTR(){    
  3. $resu=""  
  4. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  5.     $r=$ts.AllowTSConnections    
  6.     if($r -eq 1)    
  7.     {    
  8.     $resu= "远程桌面处于开启状态,无需重复开启"    
  9.     }else{    
  10.     $ts.SetAllowTSConnections(1) | Out-Null    
  11.     $resu= "远程桌面开启成功"    
  12.     }   
  13. $resu  
  14. }    
  15. function CTR(){  
  16. $resu=""  
  17. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  18. $r=$ts.AllowTSConnections    
  19.     if($r -eq 0)    
  20.     {    
  21.    $resu= "远程桌面处于关闭状态,无需重复关闭"    
  22.     }else{    
  23.     $ts.SetAllowTSConnections(0) | Out-Null    
  24.    $resu= "远程桌面关闭成功"    
  25.     }    
  26. $resu  
  27. }  
  28. #查看状态  
  29. function STR(){  
  30. $resu=""  
  31. $ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices  
  32. $r=$ts.AllowTSConnections    
  33.     if($r -eq 0)    
  34.     {    
  35.    $resu= "远程桌面处于[关闭]状态"    
  36.     }  
  37.     if($r -eq 1)    
  38.     {    
  39.    $resu= "远程桌面处于[开启]状态"    
  40.     }  
  41. $resu  
  42. }  
  43. #End查看状态  
  44. #输入控制  
  45. function CustOrderSW()  {    
  46. $ins=read-host "输入命令"  
  47. switch($ins){  
  48. (0)  
  49. {CTR   
  50. CustOrderSW;  
  51. break}  
  52. (1)  
  53. {  
  54. OTR   
  55. CustOrderSW;  
  56. break  
  57. }  
  58. (2)  
  59. {  
  60. STR   
  61. CustOrderSW;  
  62. break}  
  63. default{  
  64. "不存De命令"   
  65. CustOrderSW;  
  66. break  
  67. }  
  68. }  
  69. }  
  70. #end输入控制  
  71. #调用自定义函数  
  72. CustOrderSW  

相关文章:

  • 2021-08-28
  • 2021-11-19
  • 2021-11-20
  • 2022-01-05
  • 2022-01-05
  • 2022-01-05
  • 2021-09-17
  • 2022-01-20
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-24
  • 2021-06-10
  • 2021-11-06
  • 2022-01-26
相关资源
相似解决方案