windows service控制
获取spooler服务:
Get-WmiObject win32_service -filter "name='spooler'"
也可以写成:
Get-WmiObject win32_service |where {$_.name -eq "spooler"}
用下面这个语句能看到这个service的members:
Get-WmiObject win32_service |where {$_.name -eq "spooler"} |Get-Member
这里能看到ProcessId属性,同时也能看到StartService/StopService这样的方法。操作方式:
(Get-WmiObject win32_service |where {$_.name -eq "spooler"}).State
(Get-WmiObject win32_service |where {$_.name -eq "spooler"}).StopService()
如果要查看该服务的启动时间:
get-process | where {$_.id -eq (get-wmiobject win32_service | where {$_.name -eq "spooler"}).ProcessID} | select -property StartTime
将能看到:
StartTime
---------
8/2/2009 3:08:54 AM
如果要访问远程机器的wmi可以这样用,但是要注意权限:
gwmi win32_service -computer onlyvc –filter "name='spooler'" -credential "onlyvc\only"
会自动弹出权限验证。gwmi是get-wmiobject的alias。