{"id":33242,"date":"2010-12-26T19:00:35","date_gmt":"2010-12-26T18:00:35","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33242"},"modified":"2024-11-06T11:21:14","modified_gmt":"2024-11-06T03:21:14","slug":"powershell-2-0%e4%b9%8b%e4%bd%bf%e7%94%a8wmi%e7%ae%a1%e7%90%86windows%e4%ba%8cpowershell%e6%93%8d%e4%bd%9cwmi","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33242","title":{"rendered":"PowerShell 2.0\u4e4b\u4f7f\u7528WMI\u7ba1\u7406Windows(\u4e8c)PowerShell\u64cd\u4f5cWMI"},"content":{"rendered":"<p>\u5728PowerShell\u53d1\u5e03\u4e4b\u524d\uff0c\u6807\u51c6\u7684\u64cd\u4f5c\u65b9\u6cd5\u662f\u4f7f\u7528\u7f16\u7a0b\u811a\u672cAPI\u8c03\u7528\u4e00\u7cfb\u5217COM\u5bf9\u8c61\u3002\u8c03\u7528COM\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef\u4ee5\u5728\u5404\u79cd\u8bed\u8a00\u4e2d\u90fd\u80fd\u4f7f\u7528\uff0c\u53ea\u662f\u5178\u578b\u7684WMI\u5ba2\u6237\u7aef\u811a\u672c\u4ee3\u7801\u4f7f\u7528VBScript\u7f16\u5199\uff0c\u56e0\u4e3a\u8fd9\u6837\u7b80\u5355\u6613\u884c\u3002\u4ee3\u7801\u4f1a\u83b7\u53d6CIM\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u5e76\u901a\u8fc7WQL\u67e5\u8be2\u548c\u83b7\u53d6\u4fe1\u606f\u3002\u7ed3\u679c\u662f\u7528\u6807\u51c6\u7684foreach\u5faa\u73af\u83b7\u5f97\u7684\u96c6\u5408\u3002\u4e0b\u4f8b\u4e3a\u4f7f\u7528VBS\u83b7\u53d6winword.exe\u8fdb\u7a0bid\u7684winword_pid.vbs\u811a\u672c\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>strComputer = &quot;.&quot;<\/p>\n<p>classNamespace = &quot;winmgmts:\\\\&quot; &amp; strComputer &amp; &quot;\\root\\cimv2&quot;<\/p>\n<p>Set objWMIService = GetObject(classNamespace)<\/p>\n<p>query = &quot;SELECT * from Win32_Process WHERE Name = &#8216;winword.exe&#8217;&quot;<\/p>\n<p>Set results = objWMIService.ExecQuery(query)<\/p>\n<p>For Each process in results<\/p>\n<p>Wscript.Echo &quot;ProcessId: &quot; &amp; process.ProcessId<\/p>\n<p>Next<\/p>\n<p>\u4f7f\u7528Windows Script Host\u63a7\u5236\u53f0\u5e94\u7528\u5bbf\u4e3bcscript.exe\u6267\u884c\u811a\u672c\u7684\u7ed3\u679c\u5982\u56fe1\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image23.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image_thumb22.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u6267\u884c\u7ed3\u679c<\/p>\n<p><strong>1 WMI\u67e5\u8be2\u5de5\u5177Get-WmiObject <\/strong><\/p>\n<p>\u5728PowerShell\u4e2d\u9488\u5bf9WMI\u67e5\u627e\u7684\u547d\u4ee4\u662fGet-WmiObject cmdlet\uff0c\u7528\u4e8e\u5c06\u67e5\u8be2\u53d1\u9001\u5230CIM\u5bf9\u8c61\u3002\u901a\u8fc7WMI\u8fd4\u56de\u7ed3\u679c\u4f1a\u662f.NET\u4e3aCIM\u5bf9\u8c61\u5c01\u88c5\u7684PowerShell\u96c6\u5408\uff0c\u5176\u4e2d\u5305\u542b\u96f6\u5230\u591a\u4e2aSystem.Management.ManagementObject\u5bf9\u8c61\u3002<\/p>\n<p>\u4e0e\u524d\u9762\u5197\u957f\u7684VBS\u4ee3\u7801\u4e0d\u540c\u7684\u662fGet-WmiObject\u9009\u62e9\u6709\u610f\u4e49\u7684\u9ed8\u8ba4\u503c\uff0c\u4e0d\u9700\u8981\u6307\u5b9a\u7279\u5b9a\u7684\u4e3b\u673a\u540d\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u8ba1\u7b97\u673a\u3002\u540c\u6837\u4e5f\u4e0d\u9700\u8981\u547d\u540d\u7a7a\u95f4\uff0c\u9ed8\u8ba4\u4e3aroot\\cimv2\u547d\u540d\u7a7a\u95f4\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e2a\u547d\u540d\u7a7a\u95f4\u4fdd\u5b58\u5927\u591a\u6570\u9700\u8981\u7ba1\u7406\u7684\u5bf9\u8c61\uff0c\u5982\u6587\u4ef6\u3001\u8fdb\u7a0b\u3001\u670d\u52a1\u3001\u7f51\u7edc\u8bbe\u7f6e\u53ca\u8bbe\u5907\u914d\u7f6e\u3002\u4e0b\u9762\u4f7f\u7528Get-WmiObject\u91cd\u5199\u524d\u9762\u7528VBS\u83b7\u53d6word\u7684pid\u7684\u811a\u672c\uff0c\u547d\u540d\u4e3a\u201cwinword_pid.ps1\u201d\uff0c\u4ee3\u7801\u5982\u4e0b \uff1a<\/p>\n<p>$query = &quot;SELECT * from Win32_Process WHERE Name=&#8217;winword.exe&#8217;&quot;<\/p>\n<p>Get-WmiObject -Query $query | select ProcessId<\/p>\n<p>\u6267\u884c\u7ed3\u679c\u5982\u56fe2\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image24.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image_thumb23.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u6267\u884c\u7ed3\u679c<\/p>\n<p>\u5176\u4e2d\u7528-query\u53c2\u6570\u4f20\u9012\u4e00\u4e2a\u5b8c\u6574\u7684WQL\u67e5\u8be2\uff0cWQL\u8bed\u8a00\u7684SELECT\u8bed\u53e5\u80fd\u591f\u6307\u5b9a\u6240\u9700\u7684\u5c5e\u6027\uff0c\u53ef\u4ee5\u7528\u8fd9\u79cd\u65b9\u6cd5\u83b7\u53d6\u5bf9\u8c61\u5c5e\u6027\u7684\u5b50\u96c6\u3002\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u5927\u91cf\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\u9650\u5236\u65e0\u7528\u6570\u636e\u3002\u5728\u5b9e\u4f8b\u4e2d\u7531\u4e8e\u8fd0\u884c\u5728\u672c\u673a\u5e76\u4f7f\u7528\u4e86\u901a\u914d\u7b26*\u83b7\u53d6\u6240\u6709\u5c5e\u6027\u7684\u6e05\u5355\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u8003\u8651\u4f18\u5316\u7f51\u7edc\u6d41\u91cf\u3002\u5728\u8fd9\u91cc\u53ef\u4ee5\u4f7f\u7528\u7b80\u5316\u7684\u522b\u540dgwmi\u83b7\u53d6WMI\u4fe1\u606f\uff0c\u56fe3\u6240\u793a\u4f7f\u7528\u8be5\u547d\u4ee4\u83b7\u53d6winword.exe\u8fdb\u7a0b\u5b9e\u4f8b\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image25.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image_thumb24.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe3 \u4f7f\u7528gwmi\u522b\u540d\u67e5\u8be2\u5f53\u524d\u7cfb\u7edf\u4e2d\u7684word\u8fdb\u7a0bPID<\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528<a name=\"OLE_LINK1\">Get-WmiObject<\/a>\u67e5\u8be2\u5728\u8fdc\u7a0b\u4e3b\u673a\u8fd0\u884c\u7684\u5bf9\u8c61\uff0c\u4e3a\u6b64\u9700\u8981\u63d0\u4f9b-computer\u53c2\u6570\uff0c\u6267\u884c\u5982\u56fe4\u6240\u793a\u7684\u547d\u4ee4\u3002<\/p>\n<p>\u8bbf\u95ee\u5176\u4ed6\u4e3b\u673a\u6d89\u53ca\u5b89\u5168\u95ee\u9898\uff0ccmdlet\u9ed8\u8ba4\u4f7f\u7528Windows\u57df\u51ed\u636e\u8ba4\u8bc1\uff0c\u5728\u8fd9\u91cc\u901a\u8fc7-Credentials\u53c2\u6570\u4f20\u9012\u7279\u5b9a\u7684\u51ed\u636e\u3002\u4e5f\u53ef\u4ee5\u901a\u8fc7Get-Credential cmdlet\u83b7\u53d6\u771f\u5b9e\u7684PSCredential\u5bf9\u8c61\uff0c\u4e3aGet-WmiObject\u63d0\u4f9b\u51ed\u636e\u3002\u5728\u811a\u672c\u6267\u884c\u8fc7\u7a0b\u4e2d\u4f1a\u5f39\u51fa\u5bf9\u8bdd\u6846\u8981\u6c42\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5982\u56fe5\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image26.png\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"442\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/image_thumb25.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe4 \u6267\u884c\u7ed3\u679c <\/p>\n<p align=\"center\">&#160;<a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/clip_image0101.jpg\"><img loading=\"lazy\" decoding=\"async\" title=\"clip_image010\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" height=\"155\" alt=\"clip_image010\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2010\/12\/clip_image010_thumb1.jpg\" width=\"190\" border=\"0\" \/><\/a><\/p>\n<p align=\"center\">\u56fe5 \u8981\u6c42\u8f93\u5165\u7528\u6237\u540d\u548c\u5bc6\u7801<\/p>\n<p>\u4f5c\u8005: \u4ed8\u6d77\u519b   <br \/>\u7248\u6743\uff1a\u672c\u6587\u7248\u6743\u5f52\u4f5c\u8005\u6240\u6709    <br \/>\u8f6c\u8f7d\uff1a\u6b22\u8fce\u8f6c\u8f7d\uff0c\u4e3a\u4e86\u4fdd\u5b58\u4f5c\u8005\u7684\u521b\u4f5c\u70ed\u60c5\uff0c\u8bf7\u6309\u8981\u6c42\u3010\u8f6c\u8f7d\u3011\uff0c\u8c22\u8c22    <br \/>\u8981\u6c42\uff1a\u672a\u7ecf\u4f5c\u8005\u540c\u610f\uff0c\u5fc5\u987b\u4fdd\u7559\u6b64\u6bb5\u58f0\u660e\uff1b\u5fc5\u987b\u5728\u6587\u7ae0\u4e2d\u7ed9\u51fa\u539f\u6587\u8fde\u63a5\uff1b\u5426\u5219\u5fc5\u7a76\u6cd5\u5f8b\u8d23\u4efb     <br \/>\u4e2a\u4eba\u7f51\u7ad9: <a href=\"http:\/\/txj.shell.tor.hu\/\">http:\/\/txj.shell.tor.hu\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728PowerShell\u53d1\u5e03\u4e4b\u524d\uff0c\u6807\u51c6\u7684\u64cd\u4f5c\u65b9\u6cd5\u662f\u4f7f\u7528\u7f16\u7a0b\u811a\u672cAPI\u8c03\u7528\u4e00\u7cfb\u5217COM\u5bf9\u8c61\u3002\u8c03\u7528COM\u5ba2\u6237\u7aef\u4ee3\u7801\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-33242","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33242"}],"collection":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33242"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33242\/revisions"}],"predecessor-version":[{"id":33771,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33242\/revisions\/33771"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}