{"id":33358,"date":"2011-01-11T17:16:17","date_gmt":"2011-01-11T16:16:17","guid":{"rendered":"http:\/\/txj.lzuer.com\/?p=33358"},"modified":"2024-11-06T11:19:54","modified_gmt":"2024-11-06T03:19:54","slug":"powershell2-0%e4%b9%8b%e6%a1%8c%e9%9d%a2%e8%ae%a1%e7%ae%97%e6%9c%ba%e7%bb%b4%e6%8a%a4%e5%85%ad%e4%bf%ae%e6%94%b9%e7%94%b5%e6%ba%90%e8%ae%a1%e5%88%92","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33358","title":{"rendered":"PowerShell2.0\u4e4b\u684c\u9762\u8ba1\u7b97\u673a\u7ef4\u62a4(\u516d)\u4fee\u6539\u7535\u6e90\u8ba1\u5212"},"content":{"rendered":"<p>Windows Vista\u548cWindows Server 2008\u7684\u7535\u6e90\u8ba1\u5212\u6709\u5927\u91cf\u6539\u8fdb\uff0c\u53ef\u4ee5\u9488\u5bf9\u4f7f\u7528\u7535\u6c60\u6216\u4ea4\u6d41\u7535\u4f9b\u7535\u7684\u60c5\u51b5\u5206\u522b\u8bbe\u7f6e\u3002\u5982\u679c\u5f53\u524d\u8ba1\u7b97\u673a\u6b63\u5728\u4f7f\u7528\u7535\u6c60\uff0c\u90a3\u4e48\u7eed\u822a\u65f6\u95f4\u662f\u4e2a\u9700\u8981\u5173\u5fc3\u7684\u95ee\u9898\u3002\u800c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u8ba1\u7b97\u673a\u7684\u6027\u80fd\u624d\u662f\u6700\u91cd\u8981\u7684\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7535\u529b\u4f1a\u5728\u51e0\u5206\u949f\u4e4b\u540e\u6062\u590d\uff0c\u5219\u4e0d\u5fc5\u4e3a\u5ef6\u957f\u7535\u6c60\u4f7f\u7528\u65f6\u95f4\u800c\u964d\u4f4e\u6027\u80fd\u3002\u5728\u4e0d\u540c\u7684\u7535\u6e90\u8ba1\u5212\u4e0b\u663e\u793a\u5668\u3001\u78c1\u76d8\u53caCPU\u7684\u7535\u529b\u6d88\u8017\u4e5f\u4e0d\u540c\uff0c\u5728Windows Server 2008\u7cfb\u7edf\u4e2d\u53ef\u4ee5\u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u7535\u6e90\u8ba1\u5212\uff0c\u5982\u56fe1\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image22.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=\"427\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb22.png\" width=\"674\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u521b\u5efa\u81ea\u5b9a\u4e49\u7684\u7535\u6e90\u8ba1\u5212<\/p>\n<p>\u521b\u5efa\u540d\u4e3a\u201cSetPowerConfig.ps1\u201d\u7684\u811a\u672c\u7528\u4e8e\u8bbe\u7f6e\u7535\u6e90\u8ba1\u5212\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($c, $t, $q, $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 += &quot;=&quot; }<\/p>\n<p>Write-Host -ForegroundColor yellow `n$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: SetPowerConfig.ps1<\/p>\n<p>Sets power config on a local machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-c(hange) &lt;mp,mb,dp,db,sp,sb,hp,hb&gt;<\/p>\n<p>-q(uery) detailed query of current power plan <\/p>\n<p>-t(ime out) time out value for change. Required when using -c to change a value<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>SetPowerConfig.ps1<\/p>\n<p>Displays error message. Must supply a parameter<\/p>\n<p>SetPowerConfig.ps1 -c mp -t 10<\/p>\n<p>Sets time out value of monitor when on power to10 minutes<\/p>\n<p>SetPowerConfig.ps1 -c mb -t 5<\/p>\n<p>Sets time out value of monitor when on battery to 5 minutes<\/p>\n<p>SetPowerConfig.ps1 -c dp -t 15<\/p>\n<p>Sets time out value of disk when on power to 15 minutes<\/p>\n<p>SetPowerConfig.ps1 -c db -t 7<\/p>\n<p>Sets time out value of disk when on battery to 7 minutes<\/p>\n<p>SetPowerConfig.ps1 -c sp -t 30<\/p>\n<p>Sets time out value of standby when on power to 30 minutes<\/p>\n<p>SetPowerConfig.ps1 -c sb -t 10<\/p>\n<p>Sets time out value of standby when on battery to 10 minutes<\/p>\n<p>SetPowerConfig.ps1 -c hp -t 45<\/p>\n<p>Sets time out value of hibernate when on power to 45 minutes<\/p>\n<p>SetPowerConfig.ps1 -c hb -t 15<\/p>\n<p>Sets time out value of hibernate when on battery to 15 minutes<\/p>\n<p>SetPowerConfig.ps1 -q c<\/p>\n<p>Lists detailed configuration settings of the current power scheme<\/p>\n<p>SetPowerConfig.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){funline(&quot;Obtaining help &#8230;&quot;) ; funhelp }<\/p>\n<p>$computer = (New-Object -ComObject WScript.Network).computername<\/p>\n<p>if($q)<\/p>\n<p>{<\/p>\n<p>funline(&quot;Power configuration on: $($computer)&quot;)<\/p>\n<p>powercfg -query <\/p>\n<p>exit<\/p>\n<p>}<\/p>\n<p>if($c -and !$t) <\/p>\n<p>{<\/p>\n<p>$(Throw &#8216;A value for $t is required. <\/p>\n<p>Try this: SetPowerConfig.ps1 -help ?&#8217;)<\/p>\n<p>}<\/p>\n<p>switch($c)<\/p>\n<p>{<\/p>\n<p>&quot;mp&quot; { powercfg -CHANGE -monitor-timeout-ac $t }<\/p>\n<p>&quot;mb&quot; { powercfg -CHANGE -monitor-timeout-dc $t }<\/p>\n<p>&quot;dp&quot; { powercfg -CHANGE -disk-timeout-ac $t}<\/p>\n<p>&quot;db&quot; { powercfg -CHANGE -disk-timeout-dc $t }<\/p>\n<p>&quot;sp&quot; { powercfg -CHANGE -standby-timeout-ac $t }<\/p>\n<p>&quot;sb&quot; { powercfg -CHANGE -standby-timeout-dc $t }<\/p>\n<p>&quot;hp&quot; { powercfg -CHANGE -hibernate-timeout-ac $t }<\/p>\n<p>&quot;hb&quot; { powercfg -CHANGE -hibernate-timeout-dc $t }<\/p>\n<p>DEFAULT { <\/p>\n<p>&quot;$c is not allowed. Try the following:<\/p>\n<p>SetPowerConfig.ps1 -help ?&quot;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u8be5\u811a\u672c\u9996\u5148\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u4e864\u4e2a\u53c2\u6570\uff0c\u5373-c\u3001-t\u3001-q\u548c-help\u3002\u5176\u4e2d-q\u6307\u5b9a\u67e5\u8be2\uff0c-help\u6307\u5b9a\u8f93\u51fa\u5e2e\u52a9\u3002-c\u548c-t\u5fc5\u987b\u540c\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a-t\u503c\u6307\u5b9a\u4fee\u6539\u7535\u6e90\u8ba1\u5212\u7684\u53c2\u6570\u8d85\u65f6\u503c\uff0c\u5982\u679c\u8be5\u503c\u4e3a\u7a7a\uff0c\u811a\u672c\u6267\u884c\u5c06\u51fa\u9519\u3002\u8be5\u811a\u672c\u4f7f\u7528WScript.Network\u5bf9\u8c61\u83b7\u5f97\u672c\u673a\u7684\u8ba1\u7b97\u673a\u540d\uff0c\u56e0\u6b64\u53ef\u4ee5\u4f7f\u7528New-Object cmdlet\u914d\u5408-comobject\u53c2\u6570\u3002\u901a\u8fc7\u5c0f\u62ec\u53f7\u5c06\u8fd9\u4e9b\u5185\u5bb9\u62ec\u8d77\u4f5c\u4e3a\u4e00\u4e2a\u5bf9\u8c61\uff0c\u4f7f\u7528ComputerName\u5c5e\u6027\u5e76\u5c06\u7ed3\u679c\u4fdd\u5b58\u5728$computer\u53d8\u91cf\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u5728\u811a\u672c\u6267\u884c\u65f6\u63d0\u4f9b\u4e86\u53c2\u6570-q\uff0c\u90a3\u4e48\u4f1a\u51fa\u73b0$q\u53d8\u91cf\u3002\u8fd9\u6837\u53ef\u4f7f\u7528funline\u51fd\u6570\u5c06\u8ba1\u7b97\u673a\u540d\u4f5c\u4e3a\u6807\u9898\u8f93\u51fa\uff0c\u5e76\u4f7f\u7528Powercfg\u5de5\u5177\u63d0\u4f9b\u53c2\u6570-query\uff0c\u4ece\u800c\u5f97\u5230\u672c\u673a\u5f53\u524d\u7684\u8be6\u7ec6\u7535\u6e90\u8ba1\u5212\u3002<\/p>\n<p>\u5982\u679c\u63d0\u4f9b\u4e86\u53c2\u6570-c\uff0c\u5219\u5fc5\u987b\u4f7f\u7528\u53c2\u6570-t\u3002\u8fd9\u662f\u56e0\u4e3a$t\u53d8\u91cf\u4e2d\u5305\u62ec\u7684\u662f\u65f6\u95f4\u4fe1\u606f\uff0c\u9700\u8981\u4e3a-c\u53c2\u6570\u4e2d\u6307\u5b9a\u7684\u64cd\u4f5c\u6307\u5b9a\u6709\u9650\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u8fd9\u6837\u624d\u4e0d\u4f1a\u65e0\u9650\u671f\u5730\u7b49\u5f85\u3002\u5982\u679c\u4ec5\u6709$c\u53d8\u91cf\uff0c\u5219\u4f7f\u7528throw\u8bed\u53e5\u8f93\u51fa\u7ea2\u8272\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u505c\u6b62\u811a\u672c\u6267\u884c\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u811a\u672c\u6839\u636e-c\u53c2\u6570\u503c\u5339\u914dswitch\u8bed\u53e5\u4e2d\u7684\u5206\u652f\uff0c\u5982\u679c\u503c\u4e3amp\uff0c\u5219\u4f7f\u7528\u5305\u62ec\u5728$t\u53d8\u91cf\u503c\u4f5c\u4e3a\u4ea4\u6d41\u7535\u60c5\u51b5\u4e0b\u7684\u663e\u793a\u5668\u8d85\u65f6\u65f6\u95f4\u7684\u5206\u949f\u6570\uff1b\u5982\u679c\u503c\u4e3amb\uff0c\u5e76\u4e14\u8ba1\u7b97\u673a\u5728\u4f7f\u7528\u7535\u6c60\u4f9b\u7535\uff0c\u5219\u4f1a\u4f7f\u7528$t\u53d8\u91cf\u503c\u8bbe\u7f6e\u663e\u793a\u5668\u7684\u8d85\u65f6\u65f6\u95f4\uff1b\u5982\u679c\u503c\u4e3adb\uff0c\u5219\u914d\u7f6e\u5f53\u524d\u7535\u6e90\u8ba1\u5212\u5728\u8fbe\u5230$t\u53d8\u91cf\u8bbe\u5b9a\u7684\u5206\u949f\u6570\u540e\u5173\u95ed\u9a71\u52a8\u5668\uff1b\u5982\u679c\u503c\u4e3asp\uff0c\u5219\u5728\u4f7f\u7528\u4ea4\u6d41\u7535\u7684\u60c5\u51b5\u4e0b\u5f53\u7a7a\u95f2\u65f6\u95f4\u8d85\u8fc7$t\u53d8\u91cf\u8bbe\u5b9a\u7684\u5206\u949f\u6570\u540e\u8ba1\u7b97\u673a\u5c06\u4f1a\u8fdb\u5165\u5f85\u673a\u72b6\u6001\uff1b\u5982\u679c\u9700\u8981\u8ba1\u7b97\u673a\u4f11\u7720\uff0c\u5219\u4f7f\u7528hp\uff0c\u5e76\u4fee\u6539\u7528\u4e8e\u4ea4\u6d41\u7535\u4e0b\u4f11\u7720\u7684\u7535\u6e90\u8ba1\u5212\u503c\uff1b\u5982\u679c\u4f7f\u7528hb\uff0c\u5219\u4e3a\u7535\u6c60\u6a21\u5f0f\u4e0b\u7684\u4f11\u7720\u503c\u3002<\/p>\n<p>\u6b64\u811a\u672c\u5728Windows Server 2008\u548cWindows XP\u4e0b\u901a\u8fc7-q\u53c2\u6570\u67e5\u8be2\u7535\u6e90\u65b9\u6848\u7684\u7ed3\u679c\u5982\u56fe2\u548c\u56fe3\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/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=\"435\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb23.png\" width=\"672\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u5728Windows 2008\u4e2d\u5217\u51fa\u5f53\u524d\u7cfb\u7edf\u7535\u6e90\u8ba1\u5212\u7684\u8be6\u7ec6\u65b9\u6848<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/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\/2011\/01\/image_thumb24.png\" width=\"673\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe3 \u5728WindowsXP\u4e2d\u83b7\u53d6\u5230\u7684\u7535\u6e90\u8ba1\u5212\u7684\u4fe1\u606f<\/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 Vista\u548cWindows Server 2008\u7684\u7535\u6e90\u8ba1\u5212\u6709\u5927\u91cf\u6539\u8fdb\uff0c\u53ef\u4ee5\u9488\u5bf9\u4f7f\u7528\u7535\u6c60\u6216\u4ea4\u6d41 [&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-33358","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\/33358"}],"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=33358"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33358\/revisions"}],"predecessor-version":[{"id":33760,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33358\/revisions\/33760"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}