在Autoit中使用WMI:第四部分
<p><strong>八、续接WMI的应用</strong></p><p dir="ltr">No.4.进程</p><blockquote dir="ltr" style="margin-right: 0px"><p dir="ltr">1.检查系统同时运行了多少个au3脚本</p><p dir="ltr">使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394372.aspx"><strong>Win32_Process</strong></a> 类并找出名字为AutoIt3.exe的进程.</p><blockquote dir="ltr" style="margin-right: 0px"><p dir="ltr"><span class="re0">$strComputer</span> = <span class="st0">"."</span> <br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span> _<br /><span class="st0">"winmgmts:\\"</span> & <span class="re0">$strComputer</span> & <span class="st0">"\root\CIMV2"</span><span class="br0">)</span> <br /><span class="re0">$colItems</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span><span class="br0">(</span> _<br /><span class="st0">"SELECT * FROM Win32_Process"</span> & _<br /><span class="st0">" WHERE Name = 'AutoIt3.exe'"</span><span class="br0">)</span> <br /><span class="kw1">For</span> <span class="re0">$objItem</span> <span class="kw1">in</span> <span class="re0">$colItems</span> <br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"-------------------------------------------"</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"CommandLine: "</span> & <span class="re0">$objItem</span>.<span class="me1">CommandLine</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Name: "</span> & <span class="re0">$objItem</span>.<span class="me1">Name</span> & <span class="re0">@CRLF</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote><p dir="ltr">2.修改进程的优先权</p><p dir="ltr">使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394372.aspx"><strong>Win32_Process</strong></a> 类和 <a href="http://msdn2.microsoft.com/en-us/library/aa393587.aspx"><strong>SetPriority </strong></a>途径</p><blockquote dir="ltr" style="margin-right: 0px"><p dir="ltr"><span class="kw1">Const</span> <span class="re0">$ABOVE_NORMAL</span> = <span class="nu0">32768</span><br /><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colProcesses</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_Process Where Name = 'Notepad.exe'"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objProcess</span> <span class="kw1">in</span> <span class="re0">$colProcesses</span><br /><span class="re0">$objProcess</span>.<span class="me1">SetPriority</span><span class="br0">(</span><span class="re0">$ABOVE_NORMAL</span><span class="br0">)</span> <br /><span class="kw1">Next</span></p></blockquote><p>3.列出每个进程所占用的内存</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394372.aspx"><strong>Win32_Process</strong></a> 类和诸如 <strong>KernelModeTime</strong>, <strong>WorkingSetSize</strong>, <strong>PageFileUsage</strong>, 与 <strong>PageFaults</strong> 的属性</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colProcesses</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_Process"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objProcess</span> <span class="kw1">in</span> <span class="re0">$colProcesses</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Process: "</span> & <span class="re0">$objProcess</span>.<span class="me1">Name</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="re0">$sngProcessTime</span> = <span class="br0">(</span><span class="kw3">String</span><span class="br0">(</span><span class="re0">$objProcess</span>.<span class="me1">KernelModeTime</span><span class="br0">)</span> + _<br /><span class="kw3">String</span><span class="br0">(</span><span class="re0">$objProcess</span>.<span class="me1">UserModeTime</span><span class="br0">)</span><span class="br0">)</span> / <span class="nu0">10000000</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Processor Time: "</span> & <span class="re0">$sngProcessTime</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Process ID: "</span> & <span class="re0">$objProcess</span>.<span class="me1">ProcessID</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Working $Size: "</span> _<br />& <span class="re0">$objProcess</span>.<span class="me1">WorkingSetSize</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Page File Size: "</span> _<br />& <span class="re0">$objProcess</span>.<span class="me1">PageFileUsage</span> & <span class="re0">@CRLF</span><span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Page Faults: "</span> & <span class="re0">$objProcess</span>.<span class="me1">PageFaults</span> & <span class="re0">@CRLF</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote></blockquote><p dir="ltr">No.5.磁盘和文件系统</p><blockquote dir="ltr" style="margin-right: 0px"><p dir="ltr">1.列出每个用户所占用的磁盘空间</p><p dir="ltr">使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394136.aspx"><strong>Win32_DiskQuota</strong></a> 类和 <strong>User</strong> 以及 <strong>DiskSpaceUsed</strong> 属性.</p><blockquote dir="ltr" style="margin-right: 0px"><p dir="ltr"><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colQuotas</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_DiskQuota"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objQuota</span> <span class="kw1">in</span> <span class="re0">$colQuotas</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Volume: "</span>& <span class="re0">@Tab</span> _<br />& <span class="re0">$objQuota</span>.<span class="me1">QuotaVolume</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"User: "</span>& <span class="re0">@Tab</span> & <span class="re0">$objQuota</span>.<span class="me1">User</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Disk Space Used: "</span> _<br />& <span class="re0">@Tab</span> & <span class="re0">$objQuota</span>.<span class="me1">DiskSpaceUsed</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote><p>2.检查软驱里是否有软盘</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx"><strong>Win32_LogicalDisk</strong></a> 类并检查 <strong>FreeSpace</strong> 属性</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span> _<br /><span class="st0">"winmgmts:\\"</span> & <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colItems</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * From Win32_LogicalDisk Where DeviceID = 'A:'"</span><span class="br0">)</span><br /><br /><span class="kw1">For</span> <span class="re0">$objItem</span> <span class="kw1">in</span> <span class="re0">$colItems</span><br /><span class="re0">$intFreeSpace</span> = <span class="re0">$objItem</span>.<span class="me1">FreeSpace</span><br /><span class="kw1">If</span> <span class="re0">$intFreeSpace</span> = <span class="st0">""</span> <span class="kw1">Then</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"There is no disk in the floppy drive."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Else</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"There is a disk in the floppy drive."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">EndIf</span><br /><span class="kw1">Next</span></p></blockquote><p>3.判断磁盘是否为可移动驱动器</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx"><strong>Win32_LogicalDisk</strong></a> 类并检查 <strong>DriveType</strong> 属性.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colDisks</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_LogicalDisk"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objDisk</span> <span class="kw1">in</span> <span class="re0">$colDisks</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DeviceID: "</span>& <span class="re0">@Tab</span> _<br />& <span class="re0">$objDisk</span>.<span class="me1">DeviceID</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Switch</span> <span class="re0">$objDisk</span>.<span class="me1">DriveType</span><br /><span class="kw1">Case</span> <span class="nu0">1</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"No root directory. "</span> _<br />& <span class="st0">"Drive type could not be "</span> _<br />& <span class="st0">"determined."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Case</span> <span class="nu0">2</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DriveType: "</span>& <span class="re0">@Tab</span> _<br />& <span class="st0">"Removable drive."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Case</span> <span class="nu0">3</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DriveType: "</span>& <span class="re0">@Tab</span> _<br />& <span class="st0">"Local hard disk."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Case</span> <span class="nu0">4</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DriveType: "</span>& <span class="re0">@Tab</span> _<br />& <span class="st0">"Network disk."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Case</span> <span class="nu0">5</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DriveType: "</span>& <span class="re0">@Tab</span> _<br />& <span class="st0">"Compact disk."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span> <br /><span class="kw1">Case</span> <span class="nu0">6</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DriveType: "</span>& <span class="re0">@Tab</span> _<br />& <span class="st0">"RAM disk."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span> <br /><span class="kw1">Case</span> <span class="kw1">Else</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Drive type could not be"</span> _<br />& <span class="st0">" determined."</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">EndSwitch</span><br /><span class="kw1">Next</span></p></blockquote><p>4.检查驱动器的文件系统类型</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx"><strong>Win32_LogicalDisk</strong></a> 类和 <strong>FileSystem</strong> 属性.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colDisks</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_LogicalDisk"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objDisk</span> <span class="kw1">in</span> <span class="re0">$colDisks</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DeviceID: "</span> & <span class="re0">$objDisk</span>.<span class="me1">DeviceID</span> & <span class="re0">@CRLF</span> <span class="br0">)</span> <br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"File System: "</span> _<br />& <span class="re0">$objDisk</span>.<span class="me1">FileSystem</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote><p>5.检查磁盘的可用空间</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394173.aspx"><strong>Win32_LogicalDisk</strong></a> 类和 <strong>FreeSpace</strong> 属性.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:"</span> _<br />& <span class="st0">"{impersonationLevel=impersonate}!\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colDisks</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * from Win32_LogicalDisk"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objDisk</span> <span class="kw1">in</span> <span class="re0">$colDisks</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"DeviceID: "</span> & <span class="re0">$objDisk</span>.<span class="me1">DeviceID</span> & <span class="re0">@CRLF</span> <span class="br0">)</span> <br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="st0">"Free Disk Space: "</span> _<br />& <span class="re0">$objDisk</span>.<span class="me1">FreeSpace</span> & <span class="re0">@CRLF</span> <span class="br0">)</span> <br /><span class="kw1">Next</span></p></blockquote><p>6.进行磁盘整理</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394515.aspx"><strong>Win32_Volume</strong></a> 类和 <a href="http://msdn2.microsoft.com/en-us/library/aa389832.aspx"><strong>Defrag</strong></a> 途径.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:\\"</span> _<br />& <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colVolumes</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> <span class="br0">(</span><span class="st0">"Select * from Win32_Volume Where Name = 'K:\\'"</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objVolume</span> <span class="kw1">in</span> <span class="re0">$colVolumes</span><br /><span class="re0">$errResult</span> = <span class="re0">$objVolume</span>.<span class="me1">Defrag</span><span class="br0">(</span><span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote></blockquote><p dir="ltr">No.6.网络</p><blockquote dir="ltr" style="margin-right: 0px"><p>1.禁用网络连接</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394217.aspx"><strong>Win32_NetworkAdapterConfiguration</strong></a> 和 <a href="http://msdn2.microsoft.com/en-us/library/aa393047.aspx"><strong>ReleaseDHCPLease</strong></a> 途径.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span><span class="st0">"winmgmts:\\"</span> & <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$colNetCards</span> = <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> _<br /><span class="br0">(</span><span class="st0">"Select * From Win32_NetworkAdapterConfiguration "</span> _<br />& <span class="st0">"Where IPEnabled = True";</span><span class="br0">)</span><br /><span class="kw1">For</span> <span class="re0">$objNetCard</span> <span class="kw1">in</span> <span class="re0">$colNetCards</span><br /><span class="re0">$objNetCard</span>.<span class="me1">ReleaseDHCPLease</span><span class="br0">(</span><span class="br0">)</span><br /><span class="kw1">Next</span></p></blockquote><p>2.得到适配器信息</p><p>使用 <a href="http://msdn2.microsoft.com/en-us/library/aa394217.aspx"><strong>Win32_NetworkAdapterConfiguration</strong></a> 类.</p><blockquote dir="ltr" style="margin-right: 0px"><p><span class="re0">$strComputer</span> = <span class="st0">"."</span><br /><span class="re0">$objWMIService</span> = <span class="kw3">ObjGet</span><span class="br0">(</span> _<br /><span class="st0">"winmgmts:\\"</span> & <span class="re0">$strComputer</span> & <span class="st0">"\root\cimv2"</span><span class="br0">)</span><br /><span class="re0">$IPConfigSet</span>= <span class="re0">$objWMIService</span>.<span class="me1">ExecQuery</span> <span class="br0">(</span><span class="st0">"Select IPAddress from Win32_NetworkAdapterConfiguration"</span> _<br />& <span class="st0">" where IPEnabled=TRUE"</span><span class="br0">)</span><br /><br /><span class="kw1">For</span> <span class="re0">$IPConfig</span> <span class="kw1">in</span> <span class="re0">$IPConfigSet</span><br /><span class="kw1">If</span> <span class="kw1">Not</span> <span class="re0">$IPConfig</span>.<span class="me1">IPAddress</span> <span class="kw1">Then</span> <br /><span class="kw1">For</span> <span class="re0">$i</span>=<span class="nu0">0</span> <span class="kw1">To</span> <span class="kw3">UBound</span><span class="br0">(</span><span class="re0">$IPConfig</span>.<span class="me1">IPAddress</span><span class="br0">)</span><br /><span class="kw3">ConsoleWrite</span><span class="br0">(</span> <span class="re0">$IPConfig</span>.<span class="me1">IPAddress</span><span class="br0">(</span> <span class="re0">$i</span><span class="br0">)</span> & <span class="re0">@CRLF</span> <span class="br0">)</span><br /><span class="kw1">Next</span><br /><span class="kw1">EndIf</span><br /><span class="kw1">Next</span></p></blockquote></blockquote> :face (34):这么好的文章没人顶 :face (29):搞定谢谢作者 收藏,不断学习中还有没有续集啊 学习了,谢谢LZ 收藏,不断学习中
还有没有续集啊 顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶 这个东西好
页:
[1]