{"id":54,"date":"2012-01-22T06:00:21","date_gmt":"2012-01-21T22:00:21","guid":{"rendered":"http:\/\/www.mossfly.com\/?p=1331"},"modified":"2012-01-22T06:00:21","modified_gmt":"2012-01-21T22:00:21","slug":"powershell-foreach-object-loop","status":"publish","type":"post","link":"https:\/\/www.pstips.net\/powershell-foreach-object-loop.html","title":{"rendered":"Powershell ForEach-Object \u5faa\u73af"},"content":{"rendered":"<div class=\"ggad\"><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-0030498886267248\"\n     data-ad-slot=\"3992944540\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/div><p>Powershell\u7ba1\u9053\u5c31\u50cf\u6d41\u6c34\u7ebf\uff0c\u5bf9\u4e8e\u6570\u636e\u7684\u5904\u7406\u662f\u4e00\u4e2a\u73af\u8282\u63a5\u7740\u4e00\u4e2a\u73af\u8282\uff0c\u5982\u679c\u4f60\u60f3\u5728\u67d0\u4e00\u73af\u8282\u5bf9\u6d41\u8fdb\u6765\u7684\u6570\u636e\u9010\u4e2a\u7ec6\u81f4\u5316\u7684\u5904\u7406\uff0c\u53ef\u662f\u4f7f\u7528ForEach-Object\uff0c$_ \u4ee3\u8868\u5f53\u524d\u7684\u6570\u636e\u3002<\/p>\n<h4>\u5bf9\u7ba1\u9053\u5bf9\u8c61\u9010\u4e2a\u5904\u7406<!--more--><\/h4>\n<p>\u5982\u679c\u4f7f\u7528Get-WmiObject \u83b7\u53d6\u7cfb\u7edf\u4e2d\u7684\u670d\u52a1\uff0c\u4e3a\u4e86\u6392\u7248\u53ef\u80fd\u4f1a\u4e5f\u4f1a\u4f7f\u7528Format-Table\u5bf9\u7ed3\u679c\u8fdb\u884c\u8868\u683c\u6392\u7248\u3002<\/p>\n<pre class=\"PSconsole\">PS C:Powershell&gt; Get-WmiObject Win32_Service | Format-Table status,DisplayName\n-AutoSize\n\nstatus DisplayName\n------ -----------\nOK     Adobe Acrobat Update Service\nOK     Application Experience\nOK     Application Layer Gateway Service\nOK     Application Host Helper Service\nOK     Application Identity\nOK     Application Information\nOK     Application Management\nOK     ASP.NET State Service<\/pre>\n<p>\u4f46\u662f\u5982\u679c\u60f3\u5bf9\u6bcf\u4e2a\u670d\u52a1\u8fdb\u884c\u66f4\u5b9a\u5236\u5316\u7684\u5904\u7406\u53ef\u662f\u4f7f\u7528ForEach-Object<\/p>\n<pre class=\"PSconsole\">PS C:Powershell&gt; Get-WmiObject Win32_Service | ForEach-Object {\"Name:\"+ $_.Disp\nlayName, \", Is ProcessId more than 100:\" + ($_.ProcessId -gt 100)}\nName:Adobe Acrobat Update Service , Is ProcessId more than 100:True\nName:Application Experience , Is ProcessId more than 100:False\nName:Application Layer Gateway Service , Is ProcessId more than 100:False\nName:Application Host Helper Service , Is ProcessId more than 100:True\nName:Application Identity , Is ProcessId more than 100:True\nName:Application Information , Is ProcessId more than 100:True\nName:Application Management , Is ProcessId more than 100:False\nName:ASP.NET State Service , Is ProcessId more than 100:False<\/pre>\n<h4>\u7ed3\u5408\u6761\u4ef6\u5904\u7406<\/h4>\n<p>ForEach-Object\u7684\u5904\u7406\u53ef\u4ee5\u5305\u542b\u4efb\u610fPowershell\u811a\u672c\uff0c\u5f53\u7136\u4e5f\u5305\u62ec\u6761\u4ef6\u8bed\u53e5<\/p>\n<pre class=\"brush:ps\">Get-WmiObject Win32_Service | ForEach-Object {\n    if ($_.ProcessId -gt 3000)\n    { \"{0}({1})\" -f $_.DisplayName,$_.ProcessID}\n}<\/pre>\n<pre class=\"PSconsole\">Windows Presentation Foundation Font Cache 3.0.0.0(5408)\nMicrosoft Network Inspection(5260)\nBranchCache(4112)\nWindows Modules Installer(7656)<\/pre>\n<h4>\u8c03\u7528\u65b9\u6cd5<\/h4>\n<p>\u5728ForEach-Object\u4e2d\uff0c$_\u4ee3\u8868\u5f53\u524d\u5bf9\u8c61\uff0c\u5f53\u7136\u4e5f\u5141\u8bb8\u901a\u8fc7$_,\u8c03\u7528\u8be5\u5bf9\u8c61\u652f\u6301\u7684\u65b9\u6cd5\u3002<br \/>\n\u4e0b\u9762\u7684\u4f8b\u5b50\u6740\u6b7b\u6240\u6709IE\u6d4f\u89c8\u5668\u8fdb\u7a0b\uff1a<\/p>\n<pre class=\"PSconsole\">PS C:Powershell&gt; Get-Process iexplore\n\nHandles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName\n-------  ------    -----      ----- -----   ------     -- -----------\n    883      29    14728      22432   181    34.26   4300 iexplore\n    771      28    55552     129152   425     8.56   5732 iexplore\n   1216      51   104324     143916   539   572.41   5912 iexplore\n    801      25    49200      25372   285     5.99   6252 iexplore\n    691      25    57564      95796   333     8.08   6388 iexplore\n   1256      38    85848     127012   379    20.37   7856 iexplore\n\nPS C:Powershell&gt; Get-Process iexplore | ForEach-Object {$_.kill()}\nPS C:Powershell&gt; Get-Process iexplore<\/pre>\n<p>Get-Process : \u627e\u4e0d\u5230\u540d\u4e3a\u201ciexplore\u201d\u7684\u8fdb\u7a0b\u3002\u8bf7\u9a8c\u8bc1\u8be5\u8fdb\u7a0b\u540d\u79f0\uff0c\u7136\u540e\u518d\u6b21\u8c03\u7528 cmdlet\u3002<br \/>\n\u6240\u5728\u4f4d\u7f6e \u884c:1 \u5b57\u7b26: 12<br \/>\n+ Get-Process &lt;&lt;&lt;&lt; iexplore<br \/>\n+ CategoryInfo : ObjectNotFound: (iexplore:String) [Get-Process],<br \/>\nProcessCommandException<br \/>\n+ FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.<br \/>\nCommands.GetProcessCommand<\/p>\n<div id=postcopy><strong>\u672c\u6587\u94fe\u63a5\uff1a<\/strong> <a rel=\u201dbookmark\u201d href=\"https:\/\/www.pstips.net\/powershell-foreach-object-loop.html\">https:\/\/www.pstips.net\/powershell-foreach-object-loop.html<\/a><br\/><strong><I>\u8bf7\u5c0a\u91cd\u539f\u4f5c\u8005\u548c\u7f16\u8f91\u7684\u8f9b\u52e4\u52b3\u52a8,\u6b22\u8fce\u8f6c\u8f7d,\u5e76\u6ce8\u660e\u51fa\u5904!<\/I><\/strong><\/div>","protected":false},"excerpt":{"rendered":"<p>Powershell\u7ba1\u9053\u5c31\u50cf\u6d41\u6c34\u7ebf\uff0c\u5bf9\u4e8e\u6570\u636e\u7684\u5904\u7406\u662f\u4e00\u4e2a\u73af\u8282\u63a5\u7740\u4e00\u4e2a\u73af\u8282\uff0c\u5982\u679c\u4f60\u60f3\u5728\u67d0\u4e00\u73af\u8282\u5bf9\u6d41\u8fdb\u6765\u7684\u6570\u636e\u9010\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[622,86,67,13],"class_list":{"0":"post-54","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-powershell","7":"tag-powershell","8":"tag-86","9":"tag-67","10":"tag-13","11":"czr-hentry"},"views":19325,"_links":{"self":[{"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/posts\/54","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":0,"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pstips.net\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}