{"id":33391,"date":"2011-01-17T17:00:27","date_gmt":"2011-01-17T16:00:27","guid":{"rendered":"http:\/\/txj.lzuer.com\/?p=33391"},"modified":"2024-11-06T11:19:54","modified_gmt":"2024-11-06T03:19:54","slug":"powershell2-0%e4%b9%8bwindows%e6%8e%92%e9%94%99%e4%ba%8c%e6%9f%a5%e7%9c%8b%e6%9c%8d%e5%8a%a1%e4%be%9d%e5%ad%98%e6%80%a7","status":"publish","type":"post","link":"http:\/\/www.fuhaijun.com\/?p=33391","title":{"rendered":"PowerShell2.0\u4e4bWindows\u6392\u9519(\u4e8c)\u67e5\u770b\u670d\u52a1\u4f9d\u5b58\u6027"},"content":{"rendered":"<p>Windows\u4e2d\u7684\u7cfb\u7edf\u670d\u52a1\u5177\u6709\u4f9d\u5b58\u6027\uff0c\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u672a\u542f\u52a8\uff0c\u53ef\u80fd\u5bfc\u81f4\u66f4\u591a\u4f9d\u8d56\u4e8e\u5b83\u7684\u670d\u52a1\u5747\u65e0\u6cd5\u542f\u52a8\u3002\u4ee5Base Filtering Engine\u670d\u52a1\u4e3a\u4f8b\uff0c\u5176\u57fa\u672c\u7b5b\u9009\u5f15\u64ce\uff08BFE\uff09\u662f\u4e00\u79cd\u7ba1\u7406\u9632\u706b\u5899\u548c Internet \u534f\u8bae\u5b89\u5168\uff08IPsec\uff09\u7b56\u7565\u53ca\u5b9e\u65bd\u7528\u6237\u6a21\u5f0f\u7b5b\u9009\u7684\u670d\u52a1\uff0c\u505c\u6b62\u6216\u7981\u7528BFE\u670d\u52a1\u5c06\u5927\u5927\u964d\u4f4e\u7cfb\u7edf\u7684\u5b89\u5168\uff0c\u5e76\u9020\u6210IPsec\u7ba1\u7406\u548c\u9632\u706b\u5899\u5e94\u7528\u7a0b\u5e8f\u4ea7\u751f\u4e0d\u53ef\u9884\u77e5\u7684\u884c\u4e3a\u3002\u5728Windows Server 2008\u4e2d\uff0c\u8fd9\u4e2a\u670d\u52a1\u4f9d\u8d56\u4e8eRemote Procedure Call\uff08RPC\uff09\u670d\u52a1\uff0c\u800cIKE and AuthIP IPsec Keying Modules\u3001Internet Connection Sharing\uff08ICS\uff09\u3001IPsec Policy Agent\u3001Routing and Remote Access\u548cWindows Firewall\u8fd95\u4e2a\u670d\u52a1\u4f9d\u8d56\u4e8e\u5b83\u3002\u4f7f\u7528\u670d\u52a1\u4f9d\u5b58\u6027\u6709\u5229\u4e8e\u5f00\u53d1\u4eba\u5458\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u4e2d\u7684\u4efb\u4f55\u529f\u80fd\uff0c\u964d\u4f4e\u5f00\u53d1\u5de5\u4f5c\u91cf\u3002\u4e0e\u6b64\u540c\u65f6\u5e26\u6765\u7684\u7f3a\u70b9\u662f\u5f88\u96be\u5224\u65ad\u4e00\u4e9b\u770b\u4f3c\u6ca1\u6709\u4efb\u4f55\u76f8\u4e92\u6ca1\u6709\u5173\u8054\u7684\u670d\u52a1\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u5728\u56fe\u5f62\u754c\u9762\u4e2d\u53ef\u4ee5\u901a\u8fc7\u670d\u52a1\u63a7\u5236\u53f0\u4e86\u89e3\u8fd9\u4e9b\u4fe1\u606f\u3002\u4e3a\u6b64\u53cc\u51fb\u670d\u52a1\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u670d\u52a1\uff0c\u7136\u540e\u6253\u5f00\u201c\u4f9d\u5b58\u5173\u7cfb\u201d\u9009\u9879\u5361\uff0c\u5982\u56fe1\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image34.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=\"433\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb34.png\" width=\"414\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u201c\u4f9d\u5b58\u5173\u7cfb\u201d\u9009\u9879\u5361<\/p>\n<p>\u4e3a\u5728\u975e\u56fe\u5f62\u754c\u9762\uff0c\u5982\u8fdc\u7a0b\u6216\u8005Windows Server 2008 Server Core\u4e0b\u67e5\u770b\u670d\u52a1\u7684\u4f9d\u5b58\u5173\u7cfb\uff0c\u4fbf\u4e8e\u8fdb\u4e00\u6b65\u67e5\u627e\u548c\u6392\u9664\u9519\u8bef\uff0c\u521b\u5efa\u540d\u4e3a\u201cServiceDependencies.ps1\u201d\u7684\u811a\u672c\u3002\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$erroractionpreference = &quot;SilentlyContinue&quot; # hides any cryptic error messages due to security<\/p>\n<p>Param($computer = &quot;localhost&quot;, [switch]$help)<\/p>\n<p>function funline ($strIN)<\/p>\n<p>{<\/p>\n<p>$num = $strIN.length<\/p>\n<p>for($i=1 ; $i -le $num ; $i++)<\/p>\n<p>{ $funline = $funline + &quot;=&quot; }<\/p>\n<p>Write-Host -ForegroundColor yellow $strIN <\/p>\n<p>Write-Host -ForegroundColor darkYellow $funline<\/p>\n<p>}<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: ServiceDependencies.ps1 <\/p>\n<p>Displays a listing of services and their dependencies<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computer The name of the computer<\/p>\n<p>-help prints help file<\/p>\n<p>YNTAX:<\/p>\n<p>ServiceDependencies.ps1 -computer WebServer<\/p>\n<p>Displays a listing of services and their dependencies on a computer named WebServer<\/p>\n<p>ServiceDependencies.ps1 <\/p>\n<p>Displays a listing of services and their dependencies on the local machine<\/p>\n<p>ServiceDependencies.ps1 -help ?<\/p>\n<p>Displays the help topic for the script<\/p>\n<p>&quot;@<\/p>\n<p>$helpText<\/p>\n<p>exit<\/p>\n<p>}<\/p>\n<p>if($help){ &quot;Obtaining help &#8230;&quot; ; funhelp }<\/p>\n<p>$dependentProperty = &quot;name&quot;, &quot;displayname&quot;, &quot;pathname&quot;, <\/p>\n<p>&quot;state&quot;, &quot;startmode&quot;, &quot;processID&quot;<\/p>\n<p>$antecedentProperty = &quot;name&quot;, &quot;displayname&quot;, <\/p>\n<p>&quot;state&quot;, &quot;processID&quot;<\/p>\n<p>if($computer = &quot;localhost&quot;) { $computer = $env:computername }<\/p>\n<p>funline(&quot;Service Dependencies on $($computer)&quot;)<\/p>\n<p>New-Variable -Name c_padline -value 14 -option constant # allows for length of <\/p>\n<p>displayname<\/p>\n<p>Get-WmiObject -Class Win32_DependentService -computername $computer |<\/p>\n<p>Foreach-object `<\/p>\n<p>{<\/p>\n<p>&quot;=&quot; * ((([wmi]$_.dependent).pathname).length + $c_padline)<\/p>\n<p>Write-Host -ForegroundColor blue &quot;This service:&quot;<\/p>\n<p>[wmi]$_.Dependent |<\/p>\n<p>format-list -Property $dependentProperty<\/p>\n<p>Write-Host -ForegroundColor cyan &quot;Depends on this service:&quot;<\/p>\n<p>[wmi]$_.Antecedent | <\/p>\n<p>format-list -Property $antecedentProperty<\/p>\n<p>&quot;=&quot; * ((([wmi]$_.dependent).pathname).length + $c_padline) + &quot;`n&quot;<\/p>\n<p>}<\/p>\n<p>\u56e0\u4e3a\u67d0\u4e9b\u670d\u52a1\u5373\u4f7f\u4f7f\u7528\u672c\u5730\u7ba1\u7406\u5458\u7ec4\u8d26\u6237\u767b\u5f55\u4e5f\u65e0\u6cd5\u8bbf\u95ee\uff0c\u800c\u4e14\u6709\u4e9b\u670d\u52a1\u662f\u7cfb\u7edf\u81ea\u8eab\u8fd0\u884c\u65f6\u4f7f\u7528\u7684\u670d\u52a1\uff0c\u4e0d\u5141\u8bb8\u7528\u6237\u64cd\u4f5c\uff0c\u6240\u4ee5\u4e3a\u4e86\u907f\u514d\u6267\u884c\u811a\u672c\u65f6\u51fa\u73b0\u9519\u8bef\uff0c\u8be5\u811a\u672c\u9996\u5148\u5c06$erroractionpreference\u81ea\u52a8\u53d8\u91cf\u8d4b\u503c\u4e3aSilentlyContinue\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u547d\u4ee4\u884c-computer\u53c2\u6570\u6307\u5b9a\u8fd0\u884c\u8fd9\u4e2a\u811a\u672c\u7684\u4e3b\u673a\u548c\u5728\u9700\u8981\u65f6\u663e\u793a\u5e2e\u52a9\u4fe1\u606f-help\u53c2\u6570\uff0c\u5e76\u4f7f\u7528\u4e86\u4e4b\u524d\u811a\u672c\u4e2d\u5b9a\u4e49\u8fc7\u7684funline\u51fd\u6570\u8f93\u51fa\u6807\u9898\u4fe1\u606f\u3002\u5728\u540e\u9762\u5b9a\u4e49\u4e86$dependentProperty\u548c$antecedentProperty\u53d8\u91cf\u5206\u522b\u4fdd\u5b58\u5c5e\u6027\u5217\u8868\uff0c\u5728\u6267\u884cWMI\u7c7b\u67e5\u8be2\u65f6\u53ef\u4ee5\u5f97\u5230\u4e0e\u8fd9\u4e24\u4e2a\u53d8\u91cf\u5bf9\u5e94\u7684\u5c5e\u6027\u503c\u3002\u5728param\u8bed\u53e5\u4e2d\u7684$computer\u53d8\u91cf\u503c\u662flocalhost\uff0c\u9ed8\u8ba4\u6307\u5411\u672c\u673a\u3002\u5982\u679c\u5728\u8c03\u7528\u6b64\u811a\u672c\u65f6\u4f20\u9012\u4e86-computer\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u8be5\u53c2\u6570\u3002\u5982\u679c\u7528\u6237\u6ca1\u6709\u63d0\u4f9b\u8ba1\u7b97\u673a\u540d\uff0c\u5219\u9700\u8981\u5c06localhost\u7ffb\u8bd1\u4e3a\u672c\u673a\u7684\u4e3b\u673a\u540d\uff0c\u4e3b\u673a\u540d\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2PS\u9a71\u52a8\u5668\u7684$env\u53d8\u91cf\u83b7\u53d6\u3002\u968f\u540e\u4f7f\u7528New-Variable cmdlet\u521b\u5efac_padline\u5e38\u91cf\uff0c\u5e76\u4f7f\u7528-option\u53c2\u6570\uff0cNew-Variable cmdlet\u7684-name\u53c2\u6570\u4e0d\u9700\u8981\u53d8\u91cf\u540d\u4ee5\u7f8e\u5143\u7b26\u5f00\u5934\u3002<\/p>\n<p>\u6700\u540e\u4f7f\u7528Get-WmiObject cmdlet\u67e5\u8be2WMI\u7c7bWin32_DependentService\uff0c\u8fd9\u4e2aWMI\u7c7b\u662f\u4e2a\u5173\u8054\u7c7b\uff0c\u4e0e\u53e6\u5916\u4e24\u4e2aWMI\u7c7b\uff0c\u5373Win32_BaseService\u548cWin32_BaseService\u6709\u5173\uff08\u6ce8\u610f\u8fd9\u4e0d\u662f\u9519\u8bef\uff0c\u8fd9\u4e2a\u7c7b\u53ef\u4ee5\u5173\u8054\u81ea\u8eab\uff0c\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u77e5\u9053\u670d\u52a1\u4e4b\u95f4\u7684\u4f9d\u5b58\u5173\u7cfb\uff09\u3002\u53ef\u4ee5\u4f7f\u7528Get-WmiObject cmdlet\u7684-computername\u53c2\u6570\u4f7f\u811a\u672c\u5177\u6709\u67e5\u8be2\u672c\u5730\u6216\u8fdc\u7a0bWMI\u7684\u80fd\u529b\uff0c\u547d\u4ee4\u7ed3\u5c3e\u4f7f\u7528\u7ba1\u9053\u5bf9\u8c61\u5c06\u5904\u7406\u540e\u7684\u7ed3\u679c\u5bf9\u8c61\u4f20\u9012\u7ed9ForEach-Object cmdlet\u3002\u968f\u540e\u4f7f\u7528[WMI]management\u5bf9\u8c61\u83b7\u5f97\u6709\u5173\u670d\u52a1\u4f9d\u5b58\u6027\u7684\u4fe1\u606f\uff0c\u5e76\u5c06\u7ed3\u679cmanagement\u5bf9\u8c61\u7528\u7ba1\u9053\u53d1\u9001\u7ed9Format-List cmdlet\u8f93\u51fa\u4fdd\u5b58\u5728$dependentProperty\u53d8\u91cf\u4e2d\u7684\u6240\u6709\u7ed3\u679c\u3002<\/p>\n<p>\u8be5\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe2\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image35.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=\"435\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb35.png\" width=\"672\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u6267\u884c\u7ed3\u679c<\/p>\n<\/p>\n<\/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.lzuer.com\/\">http:\/\/txj.lzuer.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Windows\u4e2d\u7684\u7cfb\u7edf\u670d\u52a1\u5177\u6709\u4f9d\u5b58\u6027\uff0c\u5982\u679c\u4e00\u4e2a\u670d\u52a1\u672a\u542f\u52a8\uff0c\u53ef\u80fd\u5bfc\u81f4\u66f4\u591a\u4f9d\u8d56\u4e8e\u5b83\u7684\u670d\u52a1\u5747\u65e0\u6cd5\u542f\u52a8\u3002\u4ee5Base F [&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,1],"tags":[],"class_list":["post-33391","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33391"}],"collection":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=33391"}],"version-history":[{"count":1,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33391\/revisions"}],"predecessor-version":[{"id":33755,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33391\/revisions\/33755"}],"wp:attachment":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33391"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}