
设置电脑除了一些标准命令行工具,还需要WMI和CIM class
学习这些可以使我们了解到powershell与外部工具交互的方式。
唯一直接上锁的方法是调用 user32dll 里的 LockWorkstation() 函数
执行以下命令即可
rundll32exe user32dll,LockWorkStation
rundll32exe是一个可以执行windows dll的工具,user32dll里面包含了一系列管理的函数。
在本地系统中注销会话有几种方法,其中最简单的方法是使用命令行工具logoffexe。
( logoff / 查看具体使用帮助),直接执行logoff命令就会注销当前会话
也可以使用shutdownexe,l选项就是用于注销的。
shutdownexe -l
还有一个方法是使用WMI, Win32_OperatingSystem 的Shutdown方法
Get-CimInstance -Classname Win32_OperatingSystem | Invoke-CimMethod -MethodName Shutdown
这两个 *** 作是类似的工作。有两个直接的选项可以重启电脑 tsshutdnexe or shutdownexe 这两个命令行工具加上合适的参数就可以。
不过 powershell自身也提供了关闭电脑和重启的方法
Stop-Computer
Restart-Computer
我一般是搭配sleep命令实现定时关机
Start-Sleep -Seconds (60603) ; Stop-Computer -Force
当然shutdown这个命令工具的功能更加强大
Get-CimInstance -ClassName Win32_Desktop
这会获取所有的桌面信息。
因为大多数属性是以Cim开头的,可以用Select-Object来过滤掉这些Cim开头的项目
Get-CimInstance -ClassName Win32_Desktop | Select-Object -ExcludeProperty "CIM"
Get-CimInstance -ClassName Win32_BIOS
Get-CimInstance -ClassName Win32_Processor | Select-Object -ExcludeProperty "CIM"
获取处理器家族信息,可以加上SystemType属性
Get-CimInstance -ClassName Win32_ComputerSystem
Get-CimInstance -ClassName Win32_QuickFixEngineering
为了更简洁的输出,你可能想要排除掉一些属性。
但是你如果只指定一个属性比如 HotFixID ,还是会返回给更多的信息
这里我们同样需要用 Select-Object
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property BuildNumber,BuildType,OSType,ServicePackMajorVersion,ServicePackMinorVersion
也可以用Select进行过来吧,因为Build和ServicePack开头的信息比较重要
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property Build,OSType,ServicePack
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property NumberOfLicensedUsers,NumberOfUsers,RegisteredUser
更简洁的用法
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -Property user
Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3"
统计使用空间和未使用空间
Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" | Measure-Object -Property FreeSpace,Size -Sum | Select-Object -Property Property,Sum
Get-CimInstance -ClassName Win32_LogonSession
Get-CimInstance -ClassName Win32_ComputerSystem -Property UserName
Get-CimInstance -ClassName Win32_LocalTime
Get-CimInstance -ClassName Win32_Service | Select-Object -Property Status,Name,DisplayName
为了显示个别较长的名字,需要使用 Format-Table 调整格式
关于服务开启关闭的属性是State
可以使用以下两个命令获取,通过where命令来过滤
也可以构建过滤哈希表
Get-WinEvent -FilterHashtable @{ LogName='Application' ProviderName='defrag' }
$c=New-PSSession "1921681499" -Credential "administrator"
$log=icm $c {get-eventlog -log Security -After "2013-3-12"}
$log|select TimeWritten,@{N="client";E={($_Messagesplit("`n")|Select-String "工作站名:")ToString()split()[2]trim()}},
@{N="ip";E={($_Messagesplit("`n")|Select-String "源网络地址:")ToString()split()[2]trim()}}|
{$_client}
这里有个例子 不知道能否帮到你。
get-eventlog -log Security -After "2013-3-12" #是提取日志
下面是提取信息和时间
以上就是关于使用powershell管理电脑(翻译自官方文档7.1 Managing computers)全部的内容,包括:使用powershell管理电脑(翻译自官方文档7.1 Managing computers)、如何通过powershell获取win7域用户的安全日志,过滤出域用户名、机器、时间、信息、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)