{"id":33370,"date":"2011-01-14T16:53:21","date_gmt":"2011-01-14T15:53:21","guid":{"rendered":"http:\/\/txj.lzuer.com\/?p=33370"},"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%ab%e5%85%b3%e9%97%ad%e6%88%96%e9%87%8d%e5%90%af%e8%bf%9c%e7%a8%8b%e8%ae%a1%e7%ae%97%e6%9c%ba","status":"publish","type":"post","link":"http:\/\/www.fuhaijun.com\/?p=33370","title":{"rendered":"PowerShell2.0\u4e4b\u684c\u9762\u8ba1\u7b97\u673a\u7ef4\u62a4(\u516b)\u5173\u95ed\u6216\u91cd\u542f\u8fdc\u7a0b\u8ba1\u7b97\u673a"},"content":{"rendered":"<p>\u5728\u6267\u884c\u66f4\u540d\u4e3b\u673a\u6216\u6dfb\u52a0\u57df\u64cd\u4f5c\u540e\uff0c\u4e3a\u4e86\u4f7f\u8bbe\u7f6e\u751f\u6548\u9700\u8981\u91cd\u542f\u8ba1\u7b97\u673a\u3002\u4e3a\u6b64\u9700\u8981\u4f7f\u7528Win32_OperatingSystem WMI\u7c7b\u7684shutdown()\u548creboot()\u65b9\u6cd5\uff0c\u8981\u6267\u884c\u7684\u64cd\u4f5c\u7531\u5411\u811a\u672c\u4f20\u9012\u7684\u53c2\u6570-a\u786e\u5b9a\uff0c\u503c\u4e3as\u5219\u5173\u673a\uff1b\u4e3ar\u5219\u91cd\u542f\u3002\u4e3a\u4e86\u987a\u5229\u5730\u5173\u673a\u6216\u91cd\u542f\u6240\u7528\u8d26\u6237\u5fc5\u987b\u5177\u6709\u76f8\u5e94\u7684\u6743\u9650\uff0c\u5c06EnablePrivileges\u7684\u5c5e\u6027\u8bbe\u7f6e\u4e3a$true\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\u5982\u679c\u6267\u884c\u5173\u673a\u548c\u91cd\u542f\u64cd\u4f5c\u7684\u4e3b\u673a\u662f\u672c\u673a\uff0c\u5219\u9700\u8981\u4e24\u6b21\u5b9a\u4e49Get-WmiObject\uff0c\u5206\u522b\u4e3a\u51fa\u793a\u51ed\u636e\u548c\u4e0d\u9700\u8981\u4f7f\u7528\u51ed\u636e\u7684\u60c5\u51b5\u3002\u5982\u679c\u64cd\u4f5c\u7684\u4e3b\u673a\u4e0d\u662f\u672c\u673a\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5907\u7528\u51ed\u636e\uff0c\u6b64\u811a\u672c\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param(<\/p>\n<p>$computer=&quot;localhost&quot;, <\/p>\n<p>$user = &quot;administrator&quot;, <\/p>\n<p>$password,<\/p>\n<p>$a,<\/p>\n<p>$help<\/p>\n<p>)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: ShutdownRebootComputer.ps1<\/p>\n<p>Shutdown or reboot a local or remote machine.<\/p>\n<p>ARAMETERS: <\/p>\n<p>-computer Specifies the name of the computer upon which to run the script<\/p>\n<p>-user user credentials <\/p>\n<p>-password password of the user<\/p>\n<p>-a(ction) action to perform &lt; s(hutdown), r(eboot) &gt;<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>ShutdownRebootComputer.ps1 -computer WebServer -a s<\/p>\n<p>Shutdown a remote computer named WebServer<\/p>\n<p>ShutdownRebootComputer.ps1 -computer WebServer -a r<\/p>\n<p>-user WebServer\\admin -password MyPassword<\/p>\n<p>Reboots a computer named WebServer. Uses the credentials <\/p>\n<p>of the WebServer admin, with password of MyPassword<\/p>\n<p>ShutdownRebootComputer.ps1<\/p>\n<p>Displays message pointing to help<\/p>\n<p>ShutdownRebootComputer.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>switch($a)<\/p>\n<p>{<\/p>\n<p>&quot;s&quot; {<\/p>\n<p>if($computer -ne &quot;localhost&quot;)<\/p>\n<p>{<\/p>\n<p>$objWMI = Get-WmiObject -Class Win32_operatingsystem `<\/p>\n<p>-computername $computer -credential $user<\/p>\n<p>$objWMI.psbase.Scope.Options.EnablePrivileges = $true<\/p>\n<p>$objWMI.shutdown()<\/p>\n<p>} <\/p>\n<p>ELSE<\/p>\n<p>{<\/p>\n<p>$objWMI = Get-WmiObject -Class Win32_operatingsystem `<\/p>\n<p>-computername $computer <\/p>\n<p>$objWMI.psbase.Scope.Options.EnablePrivileges = $true<\/p>\n<p>$objWMI.shutdown()<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&quot;r&quot; {<\/p>\n<p>if($computer -ne &quot;localhost&quot;)<\/p>\n<p>{<\/p>\n<p>$objWMI = Get-WmiObject -Class Win32_operatingsystem `<\/p>\n<p>-computername $computer -credential $user<\/p>\n<p>$objWMI.psbase.Scope.Options.EnablePrivileges = $true<\/p>\n<p>$objWMI.reboot()<\/p>\n<p>} <\/p>\n<p>ELSE<\/p>\n<p>{<\/p>\n<p>$objWMI = Get-WmiObject -Class Win32_operatingsystem `<\/p>\n<p>-computername $computer <\/p>\n<p>$objWMI.psbase.Scope.Options.EnablePrivileges = $true<\/p>\n<p>$objWMI.reboot()<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>DEFAULT { &quot;You must supply an action. Try this&quot;<\/p>\n<p>&quot;ShutdownRebootComputer.ps1 -help ?&quot; }<\/p>\n<p>}<\/p>\n<p>\u6267\u884c\u6b64\u811a\u672c\u8c03\u7528\u547d\u4ee4.\\ShutdownRebootComputer.ps1 \u2013a s\u548c.\\ShutdownRebootComputer.ps1 \u2013a r\uff0c\u5206\u522b\u5bf9\u5e94\u5f53\u524d\u7cfb\u7edf\u7684\u5173\u95ed\u548c\u91cd\u542f\u3002\u5173\u673a\u548c\u91cd\u542f\u64cd\u4f5c\u5728\u672c\u5730\u8ba1\u7b97\u673a\u4e0a\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\uff0c\u800c\u8fdc\u7a0b\u8ba1\u7b97\u673a\u9700\u8981\u6307\u5b9a\u76f8\u5e94\u7684\u53c2\u6570\uff0c\u5176\u4e2d-computer\u6307\u5b9a\u4e3b\u673a\u540d\uff1b-user\u6307\u5b9a\u7528\u6237\uff1b-password\u6307\u5b9a\u7528\u6237\u5bc6\u7801\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8bef\u8fd0\u884c\u8be5\u811a\u672c\u5173\u95ed\u6216\u91cd\u542f\u8ba1\u7b97\u673a\uff0c\u5c06\u9ed8\u8ba4\u64cd\u4f5c\u8bbe\u7f6e\u4e3a\u663e\u793a\u5e2e\u52a9\u5b57\u7b26\u4e32\uff0c\u5e76\u8981\u6c42\u7528\u6237\u5728\u63d0\u4f9b-help\u53c2\u6570\u7684\u60c5\u51b5\u4e0b\u624d\u80fd\u8fd0\u884c\u8be5\u811a\u672c\u7684\u5e2e\u52a9\u4fe1\u606f\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>\u5728\u6267\u884c\u66f4\u540d\u4e3b\u673a\u6216\u6dfb\u52a0\u57df\u64cd\u4f5c\u540e\uff0c\u4e3a\u4e86\u4f7f\u8bbe\u7f6e\u751f\u6548\u9700\u8981\u91cd\u542f\u8ba1\u7b97\u673a\u3002\u4e3a\u6b64\u9700\u8981\u4f7f\u7528Win32_OperatingSyst [&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-33370","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\/33370"}],"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=33370"}],"version-history":[{"count":1,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33370\/revisions"}],"predecessor-version":[{"id":33758,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33370\/revisions\/33758"}],"wp:attachment":[{"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33370"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}