{"id":33430,"date":"2011-01-23T17:52:07","date_gmt":"2011-01-23T16:52:07","guid":{"rendered":"http:\/\/txj.lzuer.com\/?p=33430"},"modified":"2024-11-06T11:19:54","modified_gmt":"2024-11-06T03:19:54","slug":"powershell2-0%e4%b9%8b%e7%bb%b4%e6%8a%a4%e7%bd%91%e7%bb%9c%e4%b8%89%e8%ae%be%e7%bd%ae%e7%bd%91%e7%bb%9c%e9%80%82%e9%85%8d%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33430","title":{"rendered":"PowerShell2.0\u4e4b\u7ef4\u62a4\u7f51\u7edc(\u4e09)\u8bbe\u7f6e\u7f51\u7edc\u9002\u914d\u5668"},"content":{"rendered":"<p>\u5982\u679c\u8ba1\u7b97\u673a\u4e2d\u6709\u591a\u4e2a\u7f51\u7edc\u9002\u914d\u5668\uff0c\u5728Windows\u539f\u6709\u7684cmd\u4e0b\u914d\u7f6e\u5176\u5c5e\u6027\u5f88\u56f0\u96be\uff0c\u7528\u6237\u5fc5\u987b\u786e\u4fdd\u914d\u7f6e\u4e86\u9002\u5f53\u7684\u9002\u914d\u5668\u5e76\u786e\u8ba4\u8981\u7981\u7528\u7684\u4e0d\u662f\u6b63\u5728\u4f7f\u7528\u4e2d\u7684\u7f51\u7edc\u9002\u914d\u5668\u3002\u672c\u8282\u4ecb\u7ecd\u5728\u5904\u7406\u591a\u4e2a\u7f51\u7edc\u9002\u914d\u5668\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u95ee\u9898\u53ca\u5176\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\uff081\uff09\u68c0\u6d4b\u591a\u4e2a\u7f51\u7edc\u9002\u914d\u5668<\/p>\n<p>\u5bf9\u4e8eWindows\u7cfb\u7edf\u6765\u8bf4\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u5c06\u65e0\u7ebf\u7f51\u7edc\u653e\u5728\u6240\u6709\u7f51\u7edc\u8fde\u63a5\u4e2d\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u7f51\u7edc\u4f7f\u7528\u3002\u8fd9\u5bf9\u4e8e\u666e\u901a\u7528\u6237\u53ef\u80fd\u662f\u5f88\u65b9\u4fbf\u7684\uff0c\u4e00\u65e6\u5468\u56f4\u6709\u65e0\u7ebf\u7f51\u7edc\u5c31\u53ef\u4ee5\u81ea\u52a8\u8fde\u63a5\u5230\u65e0\u7ebf\u7f51\u7edc\u4e2d\uff0c\u4fbf\u6377\u800c\u6709\u6548\u3002\u4f46\u662f\u5bf9\u4e8e\u7f51\u7edc\u7ba1\u7406\u5458\u6765\u8bf4\u8fd9\u4e2a\u7279\u6027\u53ef\u80fd\u4f1a\u5e26\u6765\u9ebb\u70e6\uff0c\u751a\u81f3\u662f\u5b89\u5168\u95ee\u9898\u3002\u5982\u51fa\u5dee\u5728\u5916\u7684\u4eba\u5458\u65e0\u6cd5\u901a\u8fc7\u6240\u5728\u5730\u7684\u6709\u7ebf\u7f51\u7edc\u8bbf\u95eeInternet\u3002Windows Vista\u901a\u5e38\u4f1a\u5efa\u8bae\u542f\u7528\u65e0\u7ebf\u7f51\u7edc\u9002\u914d\u5668\u6765\u4e0a\u7f51\uff0c\u800c\u5982\u679c\u542c\u4ece\u4e86\u8fd9\u4e2a\u5efa\u8bae\uff0c\u5e76\u8fde\u63a5\u5230\u4e0d\u5b89\u5168\u7684\u7f51\u7edc\uff0c\u5982\u56fe1\u6240\u793a\uff0c\u8ba1\u7b97\u673a\u53ef\u80fd\u5b58\u5728\u5f88\u5927\u7684\u5b89\u5168\u5a01\u80c1\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image46.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=\"478\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb46.png\" width=\"627\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u8fde\u63a5\u5230\u4e0d\u5b89\u5168\u7684\u7f51\u7edc<\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cGetNetID.ps1\u201d\u7684\u811a\u672c\u663e\u793a\u8fde\u63a5\u5230\u672c\u5730\u8ba1\u7b97\u673a\u7684\u7f51\u7edc\u9002\u914d\u5668\u540d\u79f0\u3001\u63a5\u53e3\u7d22\u5f15\u7f16\u53f7\u3001\u9002\u914d\u5668\u4fe1\u606f\u53ca\u5176\u4ecb\u8d28\u5f62\u5f0f\uff0c\u8fd9\u4e9b\u5c5e\u6027\u5bf9\u4e8e\u521b\u5efa\u8d44\u4ea7\u6e05\u5355\u975e\u5e38\u6709\u7528\u3002\u8be5\u811a\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>Get-WmiObject -Class win32_networkadapter |<\/p>\n<p>format-table -Property name, interfaceIndex, `<\/p>\n<p>adapterType, macAddress \u2013autosize<\/p>\n<p>\u8be5\u811a\u672c\u4e2d\u901a\u8fc7\u4f7f\u7528Get-WmiObject cmdlet\u5e76\u68c0\u7d22Win32_NetWorkAdapter WMI\u7c7b\u4fe1\u606f\uff0c\u6700\u540e\u5c06\u8f93\u51fa\u7684\u4fe1\u606f\u901a\u8fc7\u7ba1\u9053\u4f20\u9012\u7ed9Format-Table cmdlet\u683c\u5f0f\u5316\u8f93\u51fa\u7684\u5185\u5bb9\uff0c\u800c\u4ec5\u9650\u5b9a\u8f93\u51faName\u3001InterfaceIndex\u3001AdapterType\u53caMacAddress\u5c5e\u6027\u3002-autosize\u53c2\u6570\u53ef\u4ee5\u914d\u5408Format-Table\u4f7f\u7528\uff0c\u4ee5\u8fdb\u4e00\u6b65\u6574\u7406\u8f93\u51fa\u5185\u5bb9\u5e76\u8c03\u6574\u663e\u793a\u65b9\u5f0f\uff0c\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\/image47.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_thumb47.png\" width=\"672\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u6267\u884c\u7ed3\u679c<\/p>\n<p>\uff082\uff09\u5c06\u7f51\u7edc\u9002\u914d\u5668\u4fe1\u606f\u5199\u5165Excel\u6587\u4ef6<\/p>\n<p>\u5f53\u7cfb\u7edf\u4e2d\u6709\u591a\u4e2a\u7f51\u7edc\u9002\u914d\u5668\u65f6\u901a\u8fc7\u63a7\u5236\u53f0\u67e5\u770b\u8fd9\u4e9b\u7f51\u7edc\u9002\u914d\u5668\u7684\u4fe1\u606f\u4f1a\u663e\u5f97\u4fe1\u606f\u91cf\u592a\u5927\uff0c\u65e0\u6cd5\u7ec6\u81f4\u5730\u67e5\u770b\u548c\u5904\u7406\uff0c\u8fd9\u65f6\u9700\u8981\u5c06\u4fe1\u606f\u5199\u5165\u5230Excel\u6587\u4ef6\u4e2d\u3002\u521b\u5efa\u540d\u4e3a\u201cWriteNetworkAdapterInfoToExcel.ps1\u201d\u7684\u811a\u672c\u83b7\u5f97\u5b89\u88c5\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u6240\u6709\u7f51\u7edc\u9002\u914d\u5668\u7684\u914d\u7f6e\u4fe1\u606f\uff0c\u5e76\u5199\u5165\u672c\u5730\u7684Excel\u6587\u4ef6\u4e2d\u4fbf\u4e8e\u540e\u671f\u5206\u6790\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$strPath=&quot;C:\\PowerShell\\CHAPTER18\\netAdapter.xls&quot;<\/p>\n<p>$objExcel=New-Object -ComObject Excel.Application<\/p>\n<p>$objExcel.Visible=-1 <\/p>\n<p>$WorkBook=$objExcel.Workbooks.Add()<\/p>\n<p>$sheet=$workbook.worksheets.item(1)<\/p>\n<p>$x=2<\/p>\n<p>$Computer = $env:computerName<\/p>\n<p>$objWMIService = Get-WmiObject -class win32_NetworkAdapter `<\/p>\n<p>-computer $Computer<\/p>\n<p>for($b=1 ; $b -le 10 ; $b++)<\/p>\n<p>{$sheet.Cells.item(1,$b).font.bold=$true}<\/p>\n<p>$sheet.Cells.item(1,1)=(&quot;Name of Adapter&quot;)<\/p>\n<p>$sheet.Cells.item(1,2)=(&quot;Interface Index&quot;)<\/p>\n<p>$sheet.Cells.item(1,3)=(&quot;Index&quot;)<\/p>\n<p>$sheet.Cells.item(1,4)=(&quot;DeviceID&quot;)<\/p>\n<p>$sheet.Cells.item(1,5)=(&quot;AdapterType&quot;)<\/p>\n<p>$sheet.Cells.item(1,6)=(&quot;MacAddress&quot;)<\/p>\n<p>$sheet.Cells.item(1,7)=(&quot;netconnectionid&quot;)<\/p>\n<p>$sheet.Cells.item(1,8)=(&quot;NetConnectionStatus&quot;)<\/p>\n<p>$sheet.Cells.item(1,9)=(&quot;NetworkAddresses&quot;)<\/p>\n<p>$sheet.Cells.item(1,10)=(&quot;PermanentAddress&quot;)<\/p>\n<p>ForEach ($objNet in $objWMIService)<\/p>\n<p>{<\/p>\n<p>$sheet.Cells.item($x, 1)=($objNet.Name)<\/p>\n<p>$sheet.Cells.item($x, 2)=($objNet.InterfaceIndex)<\/p>\n<p>$sheet.Cells.item($x, 3)=($objNet.index)<\/p>\n<p>$sheet.Cells.item($x, 4)=($objNet.DeviceID)<\/p>\n<p>$sheet.Cells.item($x, 5)=($objNet.adapterType)<\/p>\n<p>$sheet.Cells.item($x, 6)=($objNet.MacAddress)<\/p>\n<p>$sheet.Cells.item($x,7)=($objNet.netconnectionid)<\/p>\n<p>$sheet.Cells.item($x,8)=($objNet.NetConnectionStatus)<\/p>\n<p>$sheet.Cells.item($x,9)=($objNet.NetworkAddresses)<\/p>\n<p>$sheet.Cells.item($x,10)=($objNet.PermanentAddress)<\/p>\n<p>If($objNet.AdapterType -notMatch &#8216;ethernet&#8217;)<\/p>\n<p>{<\/p>\n<p>$sheet.Cells.item($x,5).font.colorIndex=3 # 32 is blue 16 silver\/gray 8 is Aqua, 4 is green, 3 is red<\/p>\n<p>$sheet.Cells.item($x,5).font.bold=$true<\/p>\n<p>}<\/p>\n<p>$x++<\/p>\n<p>}<\/p>\n<p>$range = $sheet.usedRange<\/p>\n<p>$range.EntireColumn.AutoFit()<\/p>\n<p>IF(Test-Path $strPath)<\/p>\n<p>{ <\/p>\n<p>Remove-Item $strPath<\/p>\n<p>$objExcel.ActiveWorkbook.SaveAs($strPath)<\/p>\n<p>}<\/p>\n<p>ELSE<\/p>\n<p>{<\/p>\n<p>$objExcel.ActiveWorkbook.SaveAs($strPath)<\/p>\n<p>}<\/p>\n<p>\u80fd\u591f\u770b\u5230\u4e0a\u8ff0\u811a\u672c\u4e2d\u7f51\u7edc\u9002\u914d\u5668\u4fe1\u606f\u7684\u4ee3\u7801\u91cf\u5f88\u5c11\uff0c\u5927\u591a\u6570\u4ee3\u7801\u4e3b\u8981\u662f\u5173\u4e8e\u5bf9Excel\u6587\u4ef6\u7684\u64cd\u4f5c\u65b9\u6cd5\u3002\u5728\u901a\u8fc7PowerShell\u64cd\u4f5cExcel\u4e4b\u524d\u9700\u8981\u6307\u5b9aExcel\u6587\u4ef6\u7684\u5b58\u653e\u8def\u5f84\uff0c\u5728\u8be5\u811a\u672c\u4e2d\u5c06\u5176\u653e\u5728$strPath\u53d8\u91cf\u4e2d\u3002\u968f\u540e\u521b\u5efa\u4e00\u4e2aExcel.Application COM\u5bf9\u8c61\u7684\u5b9e\u4f8b\uff0c\u7528\u4e8e\u521b\u5efa\u548c\u64cd\u4f5cExcel\u8868\u683c\u3002\u4e3a\u64cd\u4f5cExcel\u6a21\u5757\uff0c\u5c06\u4fdd\u5b58\u5728$objExcel\u53d8\u91cf\u4e2d\u7684Excel.Application\u5bf9\u8c61\u7684Visible\u5c5e\u6027\u8bbe\u7f6e\u4e3a-1\uff0c\u8868\u793a\u7a97\u53e3\u53ef\u89c1\u3002\u7136\u540e\u5728Excel\u4e2d\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5de5\u4f5c\u7c3f\uff0c\u6253\u5f00\u7b2c1\u4e2a\u5de5\u4f5c\u8868\u5e76\u5c06\u5f15\u7528\u4fe1\u606f\u4fdd\u5b58\u5728$sheet\u53d8\u91cf\u4e2d\u3002\u4e3a\u6b64\u9700\u8981\u5f15\u7528\u65b0\u521b\u5efa\u5e76\u4fdd\u5b58\u5728$workbook\u53d8\u91cf\u4e2d\u7684\u65b0\u5de5\u4f5c\u7c3f\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528item\u65b9\u6cd5\u8fd4\u56de\u7b2c1\u4e2a\u5de5\u4f5c\u8868\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u58f0\u660e\u53d8\u91cf$x\uff0c\u5e76\u8d4b\u503c\u4e3a2\uff0c\u6307\u5b9a\u4ece\u7b2c2\u884c\u5f00\u59cb\u5199\u3002\u968f\u540e\u4e3a\u4e86\u5728WMI\u67e5\u8be2\u4e2d\u80fd\u591f\u4f7f\u7528\u8ba1\u7b97\u673a\u540d\uff0c\u8fdb\u5165env:\\\u8fd9\u4e2aPS\u7684\u9a71\u52a8\u5668\u4e2d\u5e76\u83b7\u5f97\u7528\u4e8e\u4fdd\u5b58\u8ba1\u7b97\u673a\u540d\u7684\u73af\u5883\u53d8\u91cfcomputerName\u7684\u503c\u4fdd\u5b58\u5728$Computer\u53d8\u91cf\u4e2d\u3002\u5176\u540e\u4f7f\u7528Get-WmiObject cmdlet\u67e5\u8be2Win32_NetworkAdapter WMI\u7c7b\uff0c\u67e5\u8be2\u5f97\u5230\u7684Management\u5bf9\u8c61\u4fdd\u5b58\u5728$objwmiService\u53d8\u91cf\u4e2d\u3002<\/p>\n<p>\u4e0b\u4e00\u6bb5\u4ee3\u7801\u4e3a\u4eceWMI\u4e2d\u83b7\u5f97\u7684\u6bcf\u4e2a\u5c5e\u6027\u63d0\u4f9b\u6807\u9898\u884c\uff0c\u8fd9\u91cc\u4f7f\u7528for\u5faa\u73af\u6307\u5b9a\u9700\u8981\u7528\u7c97\u4f53\u663e\u793a\u7684\u884c\u3002\u4e3a\u4e86\u5c06\u8be5\u884c\u6807\u9898\u663e\u793a\u4e3a\u7c97\u4f53\uff0c\u5c06\u8be5\u5b57\u4f53\u7684Bold\u5c5e\u6027\u8bbe\u7f6e\u4e3aTrue\u3002\u8bbe\u7f6e\u6807\u9898\u540e\u4f7f\u7528Foreach\u8bed\u53e5\u5728WMI\u5bf9\u8c61\u4e4b\u95f4\u904d\u5386\uff0c\u4ee5\u627e\u51fa\u6240\u9700\u7684\u7279\u5b9a\u4fe1\u606f\u5e76\u5c06\u5176\u63d2\u5165\u5230\u76f8\u5e94\u7684\u5217\u4e2d\u3002\u8fd9\u91cc\u4f7f\u7528item\u65b9\u6cd5\u6765\u5f15\u7528\u76f8\u5e94\u7684\u5355\u5143\u683c\uff0c\u9700\u8981\u4e3a\u8be5\u65b9\u6cd5\u63d0\u4f9b\u6a2a\u8f74\u548c\u7eb5\u8f74\u7684\u5750\u6807\u4fe1\u606f\uff0c\u4ee5\u4f7f\u5149\u6807\u5728\u5355\u5143\u683c\u4e2d\u5b9a\u4f4d\u3002\u4e3a\u4e86\u8ba9\u64cd\u4f5c\u8fc7\u7a0b\u7b80\u5355\u800c\u4fbf\u6377\uff0c\u8fd9\u91cc\u4f7f\u7528\u53d8\u91cf$x\u8ddf\u8e2a\u8981\u5199\u5165\u7684\u884c\u3002\u901a\u8fc7\u6539\u53d8\u5176\u503c\u6765\u79fb\u52a8\u5149\u6807\uff0c\u7279\u5b9a\u7684\u7eb5\u8f74\u6307\u5b9a\u4fdd\u5b58\u7279\u5b9a\u6570\u636e\u7684\u5217\u3002\u68c0\u67e5\u7f51\u7edc\u9002\u914d\u5668\u4e3a\u4ee5\u592a\u7f51\u9002\u914d\u5668\u6216\u5176\u4ed6\u7c7b\u578b\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u662f\u68c0\u67e5Win32_NetworkAdapter WMI\u7c7b\u4e2d\u7684AdapterType\u5c5e\u6027\uff0c\u8be5\u811a\u672c\u7684\u76ee\u7684\u662f\u6807\u8bb0\u975e\u4ee5\u592a\u7f51\u9002\u914d\u5668\uff0c\u6240\u4ee5\u4f7f\u7528-notmatch\u64cd\u4f5c\u7b26\u6bd4\u8f83\u9002\u914d\u5668\u7c7b\u578b\u3002\u5982\u679c\u9002\u914d\u5668\u7c7b\u578b\u4e0d\u662f\u201cethernet\u201d\uff08\u4ee5\u592a\u7f51\uff09\uff0c\u5219\u6539\u53d8\u5b57\u4f53\u7684\u989c\u8272\u5e76\u7528\u7c97\u4f53\u663e\u793a\u3002\u5904\u7406\u6bcf\u4e00\u6761\u7f51\u7edc\u9002\u914d\u5668\u8bb0\u5f55\u540e\u4f7f\u7528\u9012\u589e\u8fd0\u7b97\u7b26++\u4f7f$x\u53d8\u91cf\u503c\u9012\u589e\uff0c\u8fd9\u6837\u968f\u540e\u7684\u4e00\u6761\u9002\u914d\u5668\u4fe1\u606f\u624d\u53ef\u5199\u5165\u8868\u683c\u7684\u4e0b\u4e00\u884c\u3002<\/p>\n<p>\u521b\u5efa\u5e76\u663e\u793a\u6574\u4e2a\u8868\u540e\uff0c\u901a\u5e38\u9700\u8981\u5fc5\u8981\u6539\u53d8\u5217\u5bbd\u4ee5\u663e\u793a\u5b8c\u6574\u7684\u4fe1\u606f\u3002\u4e3a\u6b64\u5728\u8be5\u811a\u672c\u4e2d\u4f7f\u7528AutoFit()\u65b9\u6cd5\u7528\u4e8e\u8868\u683c\u5185\u7279\u5b9a\u8303\u56f4\u5185\u7684\u5217\u5bf9\u8c61\uff0c\u5b9a\u4e49\u8303\u56f4\u7684\u7b80\u5355\u65b9\u6cd5\u662f\u4f7f\u7528\u5de5\u4f5c\u8868\u5bf9\u8c61\u7684UseRange\u5c5e\u6027\u3002<\/p>\n<p>\u89c4\u6574\u8868\u683c\u5185\u5bb9\u548c\u683c\u5f0f\u4e4b\u540e\uff0c\u52a1\u5fc5\u4fdd\u5b58\u5f53\u524d\u6587\u6863\u3002\u5982\u679c\u5de5\u4f5c\u7c3f\u5df2\u7ecf\u5b58\u5728\uff0c\u5219\u5c06\u5176\u5220\u9664\uff0c\u7136\u540e\u5c06\u5de5\u4f5c\u8868\u4fdd\u5b58\u5728\u65b0\u7684\u5de5\u4f5c\u7c3f\u4e2d\uff1b\u5426\u5219\u76f4\u63a5\u5c06\u5de5\u4f5c\u8868\u4fdd\u5b58\u4e3a\u65b0\u5de5\u4f5c\u7c3f\u3002\u8be5\u811a\u672c\u6267\u884c\u540e\u7684\u5de5\u4f5c\u7c3f\u5982\u56fe3\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image48.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=\"363\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb48.png\" width=\"644\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe3 \u811a\u672c\u6267\u884c\u540e\u7684\u5de5\u4f5c\u7c3f<\/p>\n<p>\uff083\uff09\u8bc6\u522b\u5df2\u8fde\u63a5\u7684\u7f51\u7edc\u9002\u914d\u5668<\/p>\n<p>\u591a\u91cd\u7f51\u7edc\u73af\u5883\u5728\u63d0\u4f9b\u4e86\u4fbf\u5229\u7684\u540c\u65f6\uff0c\u4e5f\u7ed9\u8ba1\u7b97\u673a\u5b89\u5168\u5e26\u6765\u6781\u5927\u7684\u5a01\u80c1\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u591a\u4e2a\u7f51\u7edc\u4e00\u65e6\u88ab\u6865\u63a5\uff0c\u800c\u7f51\u7edc\u4e2d\u53c8\u5b58\u5728\u5b89\u5168\u548c\u4e0d\u5b89\u5168\u7684\u7f51\u7edc\uff0c\u5219\u6076\u610f\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u7f51\u6865\u653b\u51fb\u5b89\u5168\u7f51\u7edc\u3002<\/p>\n<p>\u5bf9\u4e8e\u590d\u6742\u7f51\u7edc\u73af\u5883\u4e0b\u7684\u8ba1\u7b97\u673a\uff0c\u591a\u91cd\u7f51\u7edc\u73af\u5883\u5728\u63d0\u4f9b\u4fbf\u5229\u7684\u540c\u65f6\uff0c\u4e5f\u7ed9\u8ba1\u7b97\u673a\u5e26\u6765\u4e86\u6781\u5927\u7684\u5a01\u80c1\u3002\u8ba1\u7b97\u673a\u7cfb\u7edf\u4e2d\u591a\u4e2a\u7f51\u7edc\u4e00\u65e6\u88ab\u6865\u63a5\u8d77\u6765\uff0c\u800c\u7f51\u7edc\u4e2d\u53c8\u5b58\u5728\u5b89\u5168\u548c\u4e0d\u5b89\u53bb\u7684\u7f51\u7edc\uff0c\u6076\u610f\u7528\u6237\u53ef\u4ee5\u901a\u8fc7\u8fd9\u79cd\u7f51\u6865\u5bf9\u5b89\u5168\u7f51\u7edc\u8fdb\u884c\u653b\u51fb\u3002\u8fd9\u5c31\u4f1a\u7ed9\u5b89\u5168\u7f51\u7edc\u9020\u6210\u5f88\u5927\u7684\u5a01\u80c1\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u7531\u4e8e\u590d\u6742\u7f51\u7edc\u73af\u5883\u4e0b\u53ef\u80fd\u7531\u7f51\u7edc\u9002\u914d\u5668\u6865\u63a5\u9020\u6210\u7684\u5b89\u5168\u98ce\u9669\uff0c\u521b\u5efa\u540d\u4e3a\u201cFindConfigurationOfConnectedAdapters.ps1\u201d\u7684\u811a\u672c\u8bc6\u522b\u8fde\u901a\u591a\u4e2a\u8ba1\u7b97\u673a\u7684\u7f51\u7edc\u9002\u914d\u5668\u3002\u8be5\u811a\u672c\u4ec5\u8fd4\u56de\u5df2\u7ecf\u8fde\u901a\u7684\u7f51\u7edc\u9002\u914d\u5668\u7684\u6570\u636e\uff0c\u5982\u679c\u6ca1\u6709\u6d3b\u52a8\u8fde\u63a5\uff0c\u5219\u4e0d\u4f1a\u8fd4\u56de\u4efb\u4f55\u6570\u636e\u3002\u8be5\u811a\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>$computer=&quot;localhost&quot;<\/p>\n<p>$connected=2<\/p>\n<p>Get-WmiObject -Class win32_networkadapter -computername $computer `<\/p>\n<p>-filter &quot;netconnectionstatus = $connected&quot; |<\/p>\n<p>foreach-object `<\/p>\n<p>{<\/p>\n<p>Get-WmiObject -Class win32_networkadapterconfiguration `<\/p>\n<p>-computername $computer -filter &quot;Index = $($_.deviceID)&quot;<\/p>\n<p>}<\/p>\n<p>\u8be5\u811a\u672c\u4f7f\u7528\u4e86\u4e24\u4e2aWMI\u7c7b\uff0c\u5176\u4e2dWin32_NetworkAdapter WMI\u7c7b\u5177\u6709\u540d\u4e3a\u201cConnected\u201d\u7684\u5c5e\u6027\uff0c\u800cWin32_NetworkAdapterConfiguration WMI\u7c7b\u5219\u65e0\u3002<\/p>\n<p>\u8be5\u811a\u672c\u9996\u5148\u5b9a\u4e49\u53d8\u91cf$compute\u7528\u4e8e\u6267\u884cWMI\u67e5\u8be2\uff0c$connected\u7528\u4e8e\u663e\u793aNetConnectionStatus\u5c5e\u6027\u503c\u8868\u793a\u8ba1\u7b97\u673a\u662f\u5426\u5df2\u7ecf\u8fde\u63a5\u3002\u968f\u540e\u67e5\u8be2Win32_NetworkAdapter\u7c7b\uff0c\u8fd9\u6837\u53ef\u83b7\u5f97\u4e00\u4e2a\u53ef\u4ee5\u4ee3\u8868\u7f51\u7edc\u9002\u914d\u5668\u662f\u5426\u8fde\u63a5\u7684Management\u5bf9\u8c61\u3002\u5982\u679c\u53ea\u5e0c\u671b\u5df2\u7ecf\u8fde\u63a5\u7684\u7f51\u7edc\u9002\u914d\u5668\u7684\u4fe1\u606f\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528-filter\u53c2\u6570\u3002\u8fd9\u4e9b\u4fe1\u606f\u4f1a\u901a\u8fc7\u7ba1\u9053\u547d\u4ee4\u53d1\u9001\u5230Foreach-Object cmdlet\uff0c\u5728\u5176\u4e2d\u67e5\u8be2Win32_NetworkAdapterConfiguration\u7c7b\u3002\u5e76\u4f7f\u7528\u7b5b\u9009\u5668\u67e5\u8be2\u4e0a\u6587\u4e2d\u7b5b\u9009\u540e\u7684\u7f51\u7edc\u9002\u914d\u5668\uff0c\u9002\u914d\u5668\u53ef\u4ee5\u901a\u8fc7\u5f53\u524d\u7ba1\u9053\u5bf9\u8c61\u7684DeviceID\u8bc6\u522b\u3002<\/p>\n<p>\uff084\uff09\u8bbe\u7f6e\u9759\u6001IP\u5730\u5740<\/p>\n<p>\u5f88\u591a\u65f6\u5019\uff0c\u7f51\u7edc\u7ba1\u7406\u5458\u9700\u8981\u4e3a\u7f51\u7edc\u8bbe\u5907\u6307\u5b9a\u9759\u6001IP\uff0c\u4ee5\u4fbf\u6709\u6548\u5730\u7ba1\u7406\u670d\u52a1\u5668\u3002\u5c3d\u7ba1\u5728\u5982\u56fe4\u6240\u793a\u7684\u201cInternet\u534f\u8bae\uff08TCP\/IP\uff09\u5c5e\u6027\u201d\u5bf9\u8bdd\u6846\u4e2d\u5f88\u5bb9\u6613\u8bbe\u7f6e\u9759\u6001IP\u5730\u5740\uff0c\u4f46\u662f\u8bbe\u7f6e\u5927\u91cf\u670d\u52a1\u5668\u5219\u6bd4\u8f83\u9ebb\u70e6\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image49.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=\"454\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb49.png\" width=\"408\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe4 \u201cInternet\u534f\u8bae\uff08TCP\/IP\uff09\u5c5e\u6027\u201d\u5bf9\u8bdd\u6846<\/p>\n<p>PowerShell\u53ef\u4ee5\u4f7f\u7528Win32_NetworkAdapterConfiguration WMI\u7c7b\u4e2d\u5305\u62ec\u768414\u4e2a\u65b9\u6cd5\uff0c\u521b\u5efa\u540d\u4e3a\u201cSetStaticIP.ps1\u201d\u7684\u811a\u672c\u4e2d\u6f14\u793a\u5176\u4e2d\u76843\u4e2a\u65b9\u6cd5\u3002<\/p>\n<p>\u8be5\u811a\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer=&quot;localhost&quot;,$q,$ip,$sm,$dg,$dns,$help)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: SetStaticIP.ps1<\/p>\n<p>Sets a static IP address on a local or remote machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computerName Specifies the name of the computer upon which to run the script<\/p>\n<p>-q Queries all IP bound network adapters<\/p>\n<p>-ip IP address to use<\/p>\n<p>-sm Subnet mask to use<\/p>\n<p>-dg Default gateway to use<\/p>\n<p>-dns Dns server to use<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>SetStaticIP.ps1 -q &quot;yes&quot; -computer WebServer<\/p>\n<p>Lists all the network adapters bound to IP on a computer named WebServer<\/p>\n<p>SetStaticIP.ps1 <\/p>\n<p>Lists all the network adapters bound to IP on local computer<\/p>\n<p>SetStaticIP.ps1 -ip &quot;10.0.0.1&quot; -sm &quot;255.0.0.0&quot; -dg &quot;10.0.0.5&quot; -dns &quot;10.0.0.2&quot;<\/p>\n<p>Sets the Ip address to 10.0.0.1 and the subnet mask to 255.0.0.0 and the default<\/p>\n<p>Gateway to 10.0.0.5 with a dns server of 10.0.0.2 on the local machine<\/p>\n<p>SetStaticIP.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>function FunEvalRTN($rtn)<\/p>\n<p>{ <\/p>\n<p>Switch ($rtn.returnvalue) <\/p>\n<p>{ <\/p>\n<p>0 { Write-Host -foregroundcolor green &quot;No errors for $strCall&quot; }<\/p>\n<p>66 { Write-Host -foregroundcolor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; invalid subnetMask&quot; }<\/p>\n<p>70 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; invalid IP&quot; }<\/p>\n<p>71 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; invalid gateway&quot; }<\/p>\n<p>91 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; access denied&quot;}<\/p>\n<p>96 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; unable to contact dns server&quot;}<\/p>\n<p>DEFAULT { Write-Host -ForegroundColor red &quot;$strCall service reports&quot; `<\/p>\n<p>&quot; ERROR $($rtn.returnValue)&quot; }<\/p>\n<p>}<\/p>\n<p>$rtn=$strCall=$null<\/p>\n<p>}<\/p>\n<p>if($help) { funhelp }<\/p>\n<p>if($q)<\/p>\n<p>{ <\/p>\n<p>Get-WmiObject -Class win32_networkadapterconfiguration `<\/p>\n<p>-computer $computer -filter &quot;ipenabled = &#8216;true&#8217;&quot;<\/p>\n<p>exit<\/p>\n<p>}<\/p>\n<p>if(!$ip) { funhelp }<\/p>\n<p>if(!$sm) { funhelp }<\/p>\n<p>if(!$dg) { funhelp }<\/p>\n<p>if(!$dns) { funhelp }<\/p>\n<p>$global:RTN = $null<\/p>\n<p>$metric = [int32[]]1<\/p>\n<p>$objWMI = Get-WmiObject -Class win32_networkadapterconfiguration `<\/p>\n<p>-computer $computer -filter &quot;ipenabled = &#8216;true&#8217;&quot;<\/p>\n<p>$RTN=$objwmi.EnableStatic($ip, $sm)<\/p>\n<p>$strCall=&quot;enable static IP and subnet mask&quot;<\/p>\n<p>FunEvalRTN($rtn)<\/p>\n<p>$RTN=$objwmi.SetGateways($dg, $metric)<\/p>\n<p>$strCall=&quot;enable set default gateway and metric&quot;<\/p>\n<p>FunEvalRTN($rtn)<\/p>\n<p>$RTN=$objwmi.SetDNSServerSearchOrder($dns)<\/p>\n<p>$strCall=&quot;Set the dns server search order&quot;<\/p>\n<p>FunEvalRTN($rtn)<\/p>\n<p>\u5728\u811a\u672c\u4e2d\u5b9a\u4e49\u4e86FunEvalRTN\u51fd\u6570\uff0c\u7528\u4e8e\u5224\u65ad\u8c03\u7528\u4e0d\u540c\u7684WMI\u65b9\u6cd5\u4ee5\u914d\u7f6eIP\u5730\u5740\u540e\u8fd4\u56de\u7684\u4ee3\u7801\u3002\u5728\u8be5\u51fd\u6570\u5185\u90e8\u4f7f\u7528switch\u8bed\u53e5\u5224\u65ad\u8fd4\u56de\u4ee3\u7801\u7684ReturnValue\u5c5e\u6027\uff0c\u5982\u679cReturnValue\u7684\u503c\u662f0\uff0c\u8868\u793a\u6ca1\u6709\u51fa\u9519\u3002\u800c\u5176\u4ed6\u503c\u5747\u4ee3\u8868\u547d\u4ee4\u6ca1\u6709\u6210\u529f\u5b8c\u6210\u3002<\/p>\n<p>\u00d8 0\uff1a\u6ca1\u6709\u9519\u8bef\uff0c\u6b63\u5e38\u5b8c\u6210\u64cd\u4f5c\u3002<\/p>\n<p>\u00d8 66\uff1a\u9519\u8bef\u7684\u5b50\u7f51\u63a9\u7801\u3002<\/p>\n<p>\u00d8 70\uff1a\u975e\u6cd5IP\u3002<\/p>\n<p>\u00d8 71\uff1a\u975e\u6cd5\u7684\u7f51\u5173\u3002<\/p>\n<p>\u00d8 91\uff1a\u8bbf\u95ee\u88ab\u62d2\u7edd\u3002<\/p>\n<p>\u00d8 96\uff1a\u4e0d\u80fd\u8fde\u63a5\u5230DNS\u670d\u52a1\u5668\u3002<\/p>\n<p>\u00d8 default \u8868\u793a\u51fa\u73b0\u9519\u8bef\uff0c\u4f46\u9519\u8bef\u4e0d\u5728\u4e0a\u8ff0\u60c5\u51b5\u3002<\/p>\n<p>\u5982\u679c\u5e0c\u671b\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u5305\u62ec\u66f4\u591a\u4fe1\u606f\uff0c\u90a3\u4e48\u53ef\u4ee5\u5305\u62ec\u4e00\u4e2a\u53d8\u91cf$strCall\uff0c\u5176\u4e2d\u5305\u62ec\u751f\u6210ReturnValue\u8c03\u7528\u7684\u65b9\u6cd5\u540d\u79f0\u3002<\/p>\n<p>\u8be5\u811a\u672c\u4f1a\u68c0\u67e5-q\u53c2\u6570\uff0c\u5982\u679c\u5b58\u5728\u8be5\u53c2\u6570\u5806\u6808\u4e2d\u4f1a\u5b58\u5728$q\u53d8\u91cf\uff0c\u811a\u672c\u8fd0\u884cWMI\u67e5\u8be2\u5e76\u663e\u793a\u6240\u6709\u542f\u7528IP\u5730\u5740\u7684\u7f51\u7edc\u9002\u914d\u5668\u4fe1\u606f\u3002\u5728\u8bbe\u7f6e\u76f8\u5173\u7f51\u7edc\u53c2\u6570\u65f6\u8981\u6c42\u76f8\u5173\u53c2\u6570\u4e0d\u80fd\u4e3a\u7a7a\uff1b\u5426\u5219\u5c06\u65e0\u6cd5\u8bbe\u7f6eIP\u4fe1\u606f\uff0c\u5e76\u8c03\u7528funhelp\u51fd\u6570\u83b7\u5f97\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<p>\u968f\u540e\u4f7f\u7528$global\u6807\u8bb0\u751f\u6210\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4f7f\u5176\u540d\u79f0\u6210\u4e3a\u5168\u5c40\u6027\u7684\u3002\u4e3a\u4e86\u4ee5\u6570\u7ec4\u5f62\u5f0f\u4fdd\u5b58\u5b58\u5728\u7684\u7f51\u5173\u5730\u5740\uff0c\u5728\u811a\u672c\u4e2d\u4f7f\u7528[int32]\u7c7b\u578b\u7ea6\u675f\u7b26\uff0c\u4ee5\u4fdd\u8bc1\u8f93\u5165\u7684\u6570\u5b57\u662fint32\u6570\u636e\u7c7b\u578b\u3002\u7136\u540e\u5728\u7c7b\u578b\u7ea6\u675f\u7b26\u5185\u90e8\u63d2\u5165\u4e00\u5bf9\u7a7a\u7684\u65b9\u62ec\u53f7\u6807\u793a\u5bf9\u7ea6\u675f\u7b26\u7684\u8c03\u7528\uff0c\u63a5\u4e0b\u6765\u5c06\u6570\u7ec4\u6307\u5b9a\u7ed9\u65b9\u6cd5\u8c03\u7528\u4e2d\u4f7f\u7528\u7684\u53d8\u91cf$metric\u3002\u6b64\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe5\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image50.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=\"459\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb50.png\" width=\"808\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe5 \u6267\u884c\u7ed3\u679c<\/p>\n<p>\u7531\u4e8eWindows Vista\u548cWindows Server2008\u4e0eWindows XP\u4e4b\u95f4\u7684\u7f51\u7edcWMI\u6a21\u578b\u5df2\u7ecf\u5b58\u5728\u5f88\u5927\u7684\u533a\u522b\uff0c\u5e76\u4e14\u8be5\u811a\u672c\u4e2d\u4f7f\u7528\u7684EnableStatic()\u3001SetGateways()\u548cSetDNSServerSearchOrder()\u7b49\u65b9\u6cd5\u5747\u662fVista\u4ee5\u540e\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u65b0\u589e\u65b9\u6cd5\uff0c\u6240\u4ee5\u8fd9\u4e2a\u811a\u672c\u4ec5\u9002\u7528\u4e8eVista\u4ee5\u4e0a\u7248\u672c\u7684Windows\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684PowerShell\u3002<\/p>\n<p>\uff085\uff09\u542f\u7528DHCP<\/p>\n<p>\u9664\u4e86\u53ef\u4ee5\u5bf9\u7f51\u7edc\u9002\u914d\u5668\u7ed1\u5b9aIP\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528DHCP\uff08Dynamic Host Configuration Protocol\uff09\u670d\u52a1\u5668\u83b7\u53d6IP\u5730\u5740\uff0c\u542f\u7528DHCP\u7684\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u9632\u6b62\u7531\u4e8e\u9759\u6001\u5730\u5740\u7ed1\u5b9a\u800c\u9020\u6210\u5bf9\u7f51\u7edc\u5730\u5740\u7684\u5927\u91cf\u5360\u7528\u3002DHCP\u7684\u5de5\u4f5c\u539f\u7406\u4e00\u822c\u5206\u4e3a4\u6b65\uff0c\u5373\u5ba2\u6237\u7aef\u67e5\u627eDHCP Server\u3001Server\u63d0\u4f9bIP\u79df\u7528\u5730\u5740\u3001\u5ba2\u6237\u7aef\u63a5\u53d7IP\u79df\u7ea6\u548c\u79df\u7ea6\u786e\u8ba4\u3002\u670d\u52a1\u5668\u4e4b\u7c7b\u7684\u8ba1\u7b97\u673a\u9700\u8981\u6307\u5b9aIP\u5730\u5740\uff1b\u5426\u5219\u4e00\u65e6IP\u5730\u5740\u88ab\u66f4\u6362\uff0c\u7528\u6237\u5c06\u65e0\u6cd5\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002\u6bd4\u8f83\u597d\u7684\u7b56\u7565\u5c31\u662f\u4e3a\u670d\u52a1\u5668\u9884\u7559\u4e00\u5b9a\u5f97\u5730\u5740\u6bb5\uff0c\u4e3a\u670d\u52a1\u5668\u7ed1\u5b9aIP\u5730\u5740\uff0c\u800cPC\u5219\u53ef\u4ee5\u4f7f\u7528DHCP\u83b7\u53d6IP\u5730\u5740\u3002<\/p>\n<p>\u521b\u5efa\u540d\u4e3a\u201cWorkWithDHCP.ps1\u201d\u7684\u811a\u672c\u7528\u4e8e\u62a5\u544aDHCP\u72b6\u6001\u3001\u542f\u7528DHCP\u3001\u91ca\u653e\u7531DHCP\u5206\u914d\u7684IP\u5730\u5740\uff0c\u5e76\u66f4\u65b0\u7531DHCP\u5206\u914d\u7684IP\u5730\u5740\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer=&quot;localhost&quot;,$action,$help)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: WorkWithDHCP.ps1<\/p>\n<p>Works with DHCP settings on a local or remote machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computerName Specifies the name of the computer upon which to run the script<\/p>\n<p>-action &lt;q(uery) e(nable) r(elease) rr(release\/renew) action to perform<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>WorkWithDHCP.ps1 -q &quot;yes&quot; -computer WebServer<\/p>\n<p>Queries DHCP settings on a computer named WebServer<\/p>\n<p>WorkWithDHCP.ps1 -action e<\/p>\n<p>enables DHCP on local computer<\/p>\n<p>WorkWithDHCP.ps1 -action r<\/p>\n<p>Releases the DHCP address on the local machine<\/p>\n<p>WorkWithDHCP.ps1 -action rr<\/p>\n<p>Releases and then renews the DHCP address on the local machine<\/p>\n<p>WorkWithDHCP.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>function FunEvalRTN($rtn)<\/p>\n<p>{ <\/p>\n<p>Switch ($rtn.returnvalue) <\/p>\n<p>{ <\/p>\n<p>0 { Write-Host -foregroundcolor green &quot;No errors for $strCall&quot; }<\/p>\n<p>82 { Write-Host -foregroundcolor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; Unable to renew DHCP lease&quot; }<\/p>\n<p>83 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; Unable to release DHCP lease&quot; }<\/p>\n<p>91 { Write-Host -ForegroundColor red &quot;$strCall reports&quot; `<\/p>\n<p>&quot; access denied&quot;}<\/p>\n<p>DEFAULT { Write-Host -ForegroundColor red &quot;$strCall service reports&quot; `<\/p>\n<p>&quot; ERROR $($rtn.returnValue)&quot; }<\/p>\n<p>}<\/p>\n<p>$rtn=$strCall=$null<\/p>\n<p>}<\/p>\n<p>if($help) { funhelp }<\/p>\n<p>$global:RTN = $null<\/p>\n<p>if(!$action) { $action=&quot;q&quot; }<\/p>\n<p>$objWMI = Get-WmiObject -Class win32_networkadapterconfiguration `<\/p>\n<p>-computer $computer -filter &quot;ipenabled = &#8216;true&#8217;&quot;<\/p>\n<p>Switch($action)<\/p>\n<p>{ <\/p>\n<p>&quot;e&quot; { <\/p>\n<p>$rtn = $objWMI.EnableDHCP() ; <\/p>\n<p>$strCall = &quot;Enable DHCP&quot; ; <\/p>\n<p>FunEvalRTN($rtn) <\/p>\n<p>}<\/p>\n<p>&quot;r&quot; { <\/p>\n<p>$rtn = $objWMI.ReleaseDHCPLease() ; <\/p>\n<p>$strCall = &quot;Release DHCP address&quot; ; <\/p>\n<p>FunEvalRTN($rtn) <\/p>\n<p>}<\/p>\n<p>&quot;rr&quot; { <\/p>\n<p>$rtn = $objWMI.RenewDHCPLease() ; <\/p>\n<p>$strCall = &quot;Release and Renew DHCP address&quot; ; <\/p>\n<p>FunEvalRTN($rtn) <\/p>\n<p>}<\/p>\n<p>&quot;q&quot; {<\/p>\n<p>&quot;DHCP Server: $($objWMI.dhcpserver)&quot;<\/p>\n<p>&quot;Lease obtained: &quot; + [Management.ManagementDatetimeConverter]::`<\/p>\n<p>todatetime($objWMI.DHCPleaseObtained)<\/p>\n<p>&quot;Lease expires: &quot; + [Management.ManagementDatetimeConverter]::`<\/p>\n<p>todatetime($objWMI.DHCPleaseExpires)<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u8be5\u811a\u672c\u9996\u5148\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e493\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d-computer\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u4e3alocalhost\u3002\u7136\u540e\u58f0\u660e\u5168\u5c40\u53d8\u91cfRTN\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a$null\uff0c\u7528\u4e8e\u5728\u4ee3\u7801\u6bb5\u4e4b\u95f4\u4f20\u9012\u53c2\u6570\u3002\u5982\u679c\u6ca1\u6709\u63d0\u4f9b\u4efb\u4f55\u53c2\u6570\uff0c\u5219\u663e\u793a\u5f53\u524dDHCP\u7684\u914d\u7f6e\u4fe1\u606f\u3002\u5982\u679c$action\u53d8\u91cf\u6ca1\u6709\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u4e3aq\u53c2\u6570\uff0c\u7528\u4e8e\u68c0\u7d22\u73b0\u6709DHCP\u4fe1\u606f\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u4f7f\u7528switch\u8bed\u53e5\u5bf9\u5224\u65ad$action\u53d8\u91cf\u503c\uff0c\u5982\u679c\u4e3ae\uff0c\u5219\u5728\u76ee\u6807\u8ba1\u7b97\u673a\u4e0a\u542f\u7528DHCP\u3002\u8fd9\u91cc\u8c03\u7528enableDHCP()\u65b9\u6cd5\u4e3a$strCall\u53d8\u91cf\u6307\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9FunEvalRTN\u51fd\u6570\u5224\u65adenableDHCP()\u65b9\u6cd5\u662f\u5426\u6210\u529f\u6267\u884c\uff1b\u5982\u679c\u4e3ar\uff0c\u5219\u8c03\u7528releaseDHCP()\u65b9\u6cd5\u91ca\u653eDHCP\u5730\u5740\uff0c\u5e76\u8fd4\u56de\u503c$strCall\u4f20\u9012\u7ed9FunEvalRTN\u51fd\u6570\u5224\u65ad\u662f\u5426\u6267\u884c\u6210\u529f\uff1b\u5982\u679c\u4e3arr\uff0c\u5219\u66f4\u65b0DHCP\u5730\u5740\u4e3a$strCall\u53d8\u91cf\u6307\u5b9a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5224\u65ad\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u5728switch\u8bed\u53e5\u7684\u6700\u540e\u9700\u8981\u663e\u793a\u63d0\u4f9b\u5f53\u524dIP\u5730\u5740\u7684DHCP\u670d\u52a1\u5668\u7684\u5730\u5740\uff0c\u5e76\u67e5\u8be2\u83b7\u5f97\u79df\u7ea6\u7684\u65f6\u95f4\u548c\u8fc7\u671f\u65f6\u95f4\u3002\u9700\u8981\u5c06UTC\u4e8b\u4ef6\u5bf9\u8c61\u8f6c\u6362\u4e3a\u5ba2\u6237\u7aef\u6240\u9700\u7684\u6b63\u5e38\u65f6\u95f4\uff0c\u4e3a\u6b64\u53ef\u4ee5\u4f7f\u7528Manangement.ManangementDateTimeConverter\u8fd9\u4e2a.NET\u6846\u67b6\u7684\u7c7b\uff0c\u5e76\u8c03\u7528\u5176\u4e2d\u7684toDateTime\u9759\u6001\u65b9\u6cd5\u5c06UTC\u683c\u5f0f\u7684\u65e5\u671f\u65f6\u95f4\u5bf9\u8c61\u4f20\u9012\u7ed9\u8be5\u65b9\u6cd5\u3002\u8be5\u811a\u672c\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe13\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image51.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=\"471\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb51.png\" width=\"792\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe6 \u67e5\u8be2\u548c\u66f4\u65b0DHCP\u4fe1\u606f<\/p>\n<p>\u4e0a\u9762\u811a\u672c\u4e2d\u4f7f\u7528\u7684EnableDHCP()\u3001ReleaseDHCPLease()\u548cRenewDHCPLease()\u7b49\u65b9\u6cd5\u5747\u662fVista\u4ee5\u540e\u7684\u64cd\u4f5c\u7cfb\u7edf\u65b0\u589e\u7684\u65b9\u6cd5\uff0c\u8be5\u811a\u672c\u4ec5\u9002\u7528\u4e8eWindows Vista\u53ca\u5176\u4ee5\u4e0a\u7248\u672c\u3002<\/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: <a href=\"http:\/\/txj.lzuer.com\/\">http:\/\/txj.lzuer.com\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u679c\u8ba1\u7b97\u673a\u4e2d\u6709\u591a\u4e2a\u7f51\u7edc\u9002\u914d\u5668\uff0c\u5728Windows\u539f\u6709\u7684cmd\u4e0b\u914d\u7f6e\u5176\u5c5e\u6027\u5f88\u56f0\u96be\uff0c\u7528\u6237\u5fc5\u987b\u786e\u4fdd\u914d\u7f6e\u4e86\u9002\u5f53\u7684\u9002\u914d\u5668\u5e76 [&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-33430","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\/33430"}],"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=33430"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33430\/revisions"}],"predecessor-version":[{"id":33748,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33430\/revisions\/33748"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}