查询远程桌面服务状态:
get-wmiobject -query "select * from win32_service where name='TermService'" | out-string
下面自定义函数,在本地执行下面脚本,开启和关闭远程桌面,win7 、2008可以执行,在win 2003需要稍微修改一点
write-host "---------------华丽的分割线------------------"
function OTR(){
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 1)
{
Write-Host "远程桌面处于开启状态,无需重复开启"
}else{
$ts.SetAllowTSConnections(1) | Out-Null
Write-Host "远程桌面开启成功"
}
}
function CTR(){
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 0)
{
Write-Host "远程桌面处于关闭状态,无需重复关闭"
}else{
$ts.SetAllowTSConnections(0) | Out-Null
Write-Host "远程桌面关闭成功"
}
}
#查看状态
function STR(){
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 0)
{
Write-Host "远程桌面处于[关闭]状态"
}
if($r -eq 1)
{
Write-Host "远程桌面处于[开启]状态"
}
}
#End查看状态
#输入控制
function CustOrderSW() {
$ins=read-host "输入命令"
switch($ins){
(0)
{CTR
CustOrderSW;
break}
(1)
{
OTR
CustOrderSW;
break
}
(2)
{
STR
CustOrderSW;
break}
default{
"不存De命令"
CustOrderSW;
break
}
}
}
#end输入控制
#调用自定义函数
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版本
------------------
Win7 x64的WMI版本
-------------------------------------------------------------------------------------------------------------------------------------------------------------
下面代码放在web版中可执行,主要是没用write-host输出,其他没改变
搜索
function OTR(){
$resu=""
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 1)
{
$resu= "远程桌面处于开启状态,无需重复开启"
}else{
$ts.SetAllowTSConnections(1) | Out-Null
$resu= "远程桌面开启成功"
}
$resu
}
function CTR(){
$resu=""
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 0)
{
$resu= "远程桌面处于关闭状态,无需重复关闭"
}else{
$ts.SetAllowTSConnections(0) | Out-Null
$resu= "远程桌面关闭成功"
}
$resu
}
#查看状态
function STR(){
$resu=""
$ts=get-WMIObject Win32_TerminalServiceSetting -Namespace ROOT\CIMV2\TerminalServices
$r=$ts.AllowTSConnections
if($r -eq 0)
{
$resu= "远程桌面处于[关闭]状态"
}
if($r -eq 1)
{
$resu= "远程桌面处于[开启]状态"
}
$resu
}
#End查看状态
#输入控制
function CustOrderSW() {
$ins=read-host "输入命令"
switch($ins){
(0)
{CTR
CustOrderSW;
break}
(1)
{
OTR
CustOrderSW;
break
}
(2)
{
STR
CustOrderSW;
break}
default{
"不存De命令"
CustOrderSW;
break
}
}
}
#end输入控制
#调用自定义函数
CustOrderSW