{"id":33405,"date":"2011-01-21T16:40:53","date_gmt":"2011-01-21T15:40:53","guid":{"rendered":"http:\/\/txj.lzuer.com\/?p=33405"},"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%e5%85%ad%e6%a3%80%e6%9f%a5%e7%bd%91%e7%bb%9c%e6%95%85%e9%9a%9c","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33405","title":{"rendered":"PowerShell2.0\u4e4bWindows\u6392\u9519(\u516d)\u68c0\u67e5\u7f51\u7edc\u6545\u969c"},"content":{"rendered":"<p>\u7f51\u7edc\u6545\u969c\u5bf9\u4e8e\u7528\u6237\u6765\u8bf4\u662f\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u6d89\u53ca\u5f88\u591a\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u4e0d\u5bb9\u6613\u67e5\u627e\u548c\u89e3\u51b3\u3002<\/p>\n<p>\u4e3a\u68c0\u67e5\u7f51\u7edc\u6545\u969c\uff0c\u521b\u5efa\u540d\u4e3a\u201cGetActiveNicAndConfig.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer = $env:computername, [switch]$full, [switch]$help)<\/p>\n<p>function funline ($strIN)<\/p>\n<p>{<\/p>\n<p>$strLine= &quot;=&quot; * $strIn.length<\/p>\n<p>Write-Host -ForegroundColor yellow $strIN <\/p>\n<p>Write-Host -ForegroundColor darkYellow $strLine<\/p>\n<p>}<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: GetActiveNicAndConfig.ps1 <\/p>\n<p>Displays <\/p>\n<p>PARAMETERS: <\/p>\n<p>-computer the name of the computer<\/p>\n<p>-full prints complete information<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>GetActiveNicAndConfig.ps1 -computer WebServer<\/p>\n<p>Displays network adapter info and network adapter configuration info on a computer<\/p>\n<p>named WebServer<\/p>\n<p>GetActiveNicAndConfig.ps1 <\/p>\n<p>Displays network adapter info and network adapter configuration info on the local machine<\/p>\n<p>GetActiveNicAndConfig.ps1 -computer WebServer -full<\/p>\n<p>Displays full network adapter info and full network adapter configuration info on a computer named WebServer<\/p>\n<p>GetActiveNicAndConfig.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>New-Variable -Name c_netConnected -value 2 -option constant<\/p>\n<p>$nic = Get-WmiObject -Class win32_networkadapter -computername $computer `<\/p>\n<p>-filter &quot;NetConnectionStatus = $c_netConnected&quot;<\/p>\n<p>$nicConfig = Get-WmiObject -Class win32_networkadapterconfiguration `<\/p>\n<p>-filter &quot;interfaceindex = $($nic.interfaceindex)&quot;<\/p>\n<p>if($full)<\/p>\n<p>{<\/p>\n<p>funline(&quot;Full Network adapter information for $($computer)&quot;)<\/p>\n<p>format-list -InputObject $nic -property [a-z]*<\/p>\n<p>funline(&quot;Full Network adapter configuration for $($computer)&quot;)<\/p>\n<p>format-list -InputObject $nicConfig -property [a-z]*<\/p>\n<p>}<\/p>\n<p>ELSE<\/p>\n<p>{<\/p>\n<p>funline(&quot;Basic Network adapter information for $($computer)&quot;)<\/p>\n<p>format-list -InputObject $nic <\/p>\n<p>funline(&quot;Basic Network adapter configuration for $($computer)&quot;)<\/p>\n<p>format-list -InputObject $nicConfig <\/p>\n<p>}<\/p>\n<p>\u8be5\u811a\u672c\u4e2d\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u4e863\u4e2a\u53c2\u6570\uff0c\u5373-computer\u3001-full\u548c-help\uff0c\u5176\u4e2d\u5c06-computer\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8ba1\u7b97\u673a\u540d\u79f0\u3002<\/p>\n<p>funline\u51fd\u6570\u83b7\u5f97\u8f93\u5165\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u5e76\u7528\u8fd9\u4e2a\u957f\u5ea6\u6765\u751f\u6210\u5206\u9694\u7ebf\u3002\u7ed3\u679c\u4fdd\u5b58\u5728$strLine\u53d8\u91cf\u4e2d\uff0c\u7136\u540e\u8f93\u51fa\u5e26\u4e0b\u753b\u7ebf\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u968f\u540e\u7684\u4ee3\u7801\u201cNew-Variable \u2013Name c_netConnected \u2013value 2 \u2013option constant\u201d\u521b\u5efa\u4fdd\u5b58\u5df2\u7ecf\u8fde\u63a5\u7f51\u7edc\u7684\u7f51\u7edc\u9002\u914d\u5668\u6570\u91cf\u7684\u53d8\u91cfc_netConnected\uff0c\u5176\u4e2d\u7684\u6570\u503c\u6765\u81eaWindows\u8f6f\u4ef6\u5f00\u53d1\u5305\uff08SDK\uff09\u3002\u4f7f\u7528Get-WmiObject cmdlet\u5e76\u9009\u62e9Win32_NetworkAdapter WMI\u7c7b\u8fde\u63a5\u5230$computer\u53d8\u91cf\u6307\u5b9a\u7684\u8ba1\u7b97\u673a\uff0c\u7136\u540e\u67e5\u627e\u5f53\u524d\u5df2\u7ecf\u8fde\u63a5\u7684\u7f51\u7edc\u9002\u914d\u5668\u5e76\u5c06\u7ed3\u679cManagement\u5bf9\u8c61\u4fdd\u5b58\u5728$nic\u53d8\u91cf\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528\u8be5\u5bf9\u8c61\u67e5\u627e\u76f8\u5173\u7684\u7f51\u7edc\u9002\u914d\u5668\u914d\u7f6e\u5bf9\u8c61\uff0c\u8fd9\u91cc\u4f7f\u7528$nic.InterfaceIndex\u5c5e\u6027\u3002\u56e0\u4e3a\u8be5\u5c5e\u6027\u4e5f\u53ef\u4ee5\u7528\u4e8eWin32_NetworkAdapterConfiguration WMI\u7c7b\uff0c\u5c06\u7ed3\u679cManagement\u5bf9\u8c61\u4fdd\u5b58\u5728$nicConfig\u53d8\u91cf\u4e2d\uff1b\u53e6\u5916\u8fd8\u9700\u8981\u5b9a\u4e49\u53cd\u9988\u4fe1\u606f\u6570\u91cf\u7684\u53c2\u6570\uff0c\u5728\u8c03\u7528\u8be5\u811a\u672c\u65f6\u63d0\u4f9b\u4e86-full\u53c2\u6570\u83b7\u5f97\u5b8c\u6574\u7684\u7f51\u7edc\u9002\u914d\u5668\u53ca\u5176\u914d\u7f6e\u4fe1\u606f\u3002\u5982\u679c\u6ca1\u6709-full\u53c2\u6570\uff0c\u5219\u4ec5\u8f93\u51fa\u6bcf\u4e2aWMI\u7c7b\u7684\u9ed8\u8ba4\u503c\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528Format-List cmdlet\u548c-InputObject\u53c2\u6570\u8f93\u51fa\u4fe1\u606f\u3002\u8be5\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe1\u6240\u793a\u3002<\/p>\n<p><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image40.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=\"467\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb40.png\" width=\"672\" border=\"0\" \/><\/a> <\/p>\n<p>\u56fe1 \u6267\u884c\u6548\u679c<\/p>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662f\u7531\u4e8e\u7ba1\u7406\u7f51\u7edc\u9002\u914d\u5668\u7684WMI\u7ba1\u7406\u7c7b\u662f\u5b58\u5728\u5dee\u5f02\uff0c\u6240\u4ee5\u8be5\u811a\u672c\u4ec5\u9002\u7528\u4e8eWindows Vista\u548cWindows Server 2008\u7cfb\u7edf\u3002<\/p>\n<\/p>\n<p>&#160;<\/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: http:\/\/txj.lzuer.com\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7f51\u7edc\u6545\u969c\u5bf9\u4e8e\u7528\u6237\u6765\u8bf4\u662f\u5f88\u590d\u6742\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u5b83\u53ef\u80fd\u6d89\u53ca\u5f88\u591a\u65b9\u9762\u7684\u77e5\u8bc6\uff0c\u4e0d\u5bb9\u6613\u67e5\u627e\u548c\u89e3\u51b3\u3002 \u4e3a\u68c0\u67e5\u7f51\u7edc\u6545\u969c\uff0c\u521b\u5efa\u540d\u4e3a [&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-33405","post","type-post","status-publish","format-standard","hentry","category-powershell","category-default"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33405"}],"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=33405"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33405\/revisions"}],"predecessor-version":[{"id":33751,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33405\/revisions\/33751"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33405"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33405"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33405"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}