{"id":33331,"date":"2011-01-07T17:32:52","date_gmt":"2011-01-07T16:32:52","guid":{"rendered":"https:\/\/www.fuhaijun.com\/?p=33331"},"modified":"2024-11-06T11:21:14","modified_gmt":"2024-11-06T03:21:14","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%e4%ba%8c%e7%94%a8%e6%88%b7%e7%ae%a1%e7%90%86","status":"publish","type":"post","link":"https:\/\/www.fuhaijun.com\/?p=33331","title":{"rendered":"PowerShell2.0\u4e4b\u684c\u9762\u8ba1\u7b97\u673a\u7ef4\u62a4(\u4e8c)\u7528\u6237\u7ba1\u7406"},"content":{"rendered":"<p>\u7cfb\u7edf\u7ba1\u7406\u5458\u7684\u91cd\u8981\u4efb\u52a1\u4e4b\u4e00\u662f\u7ba1\u7406\u7528\u6237\uff0c\u65e2\u5141\u8bb8\u7528\u6237\u505a\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u53c8\u8981\u9632\u6b62\u56e0\u6743\u9650\u8bbe\u7f6e\u4e0d\u5408\u7406\u800c\u6f5c\u5728\u7684\u7528\u6237\u8d8a\u6743\u64cd\u4f5c\u3002\u5982\u5728\u865a\u62df\u4e3b\u673a\u4e2d\u8bbe\u7f6e\u7684\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u7684\u6743\u9650\u6709\u95ee\u9898\uff0c\u5176\u4ed6\u7528\u6237\u53ef\u4ee5\u91c7\u7528..\u53cd\u6eaf\u5230\u4e0a\u7ea7\u76ee\u5f55\u3002\u8fdb\u800c\u8bbf\u95ee\u5176\u4ed6\u7528\u6237\u7684\u6587\u4ef6\uff0c\u76f4\u63a5\u9020\u6210\u8fd9\u4e9b\u6587\u4ef6\u7684\u6cc4\u9732\uff0c\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6cd5\u662f\u5c06\u6bcf\u4e2a\u7528\u6237\u7684\u4e3b\u76ee\u5f55\u6743\u9650\u8bbe\u7f6e\u4e3a\u4ec5\u6709\u7ba1\u7406\u5458\u548c\u7528\u6237\u672c\u4eba\u53ef\u4ee5\u8bbf\u95ee\uff0c\u800c\u62d2\u7edd\u5176\u4ed6\u4eba\u7684\u8bbf\u95ee\u3002\u5404\u7528\u6237\u7684\u6587\u4ef6\u53ea\u662f\u72ec\u7acb\u5728\u81ea\u5df1\u7684\u6743\u9650\u8303\u56f4\u5185\uff0c\u65e0\u6cd5\u8bbf\u95ee\u4ed6\u4eba\u7684\u6587\u4ef6\uff0c\u4ed6\u4eba\u4e5f\u65e0\u6cd5\u8bbf\u95ee\u81ea\u5df1\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>\uff081\uff09\u521b\u5efa\u672c\u5730\u7528\u6237<\/strong><\/p>\n<p>\u521b\u5efa\u672c\u5730\u7528\u6237\u7684\u65b9\u6cd5\u901a\u5e38\u4f7f\u7528net user\u548cADSI\u547d\u4ee4\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5982\u56fe1\u6240\u793a\u7684\u201c\u65b0\u7528\u6237\u201d\u5bf9\u8bdd\u6846\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image11.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_thumb11.png\" width=\"388\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe1 \u201c\u65b0\u7528\u6237\u201d\u5bf9\u8bdd\u6846<\/p>\n<p>\u5728cmd\u4e0b\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528net user\u547d\u4ee4\u521b\u5efa\u7528\u6237\u3002\u5728PowerShell\u4e2d\u4f7f\u7528ADSI\u65b9\u4fbf\u800c\u6709\u6548\u7684\u521b\u5efa\u672c\u5730\u7528\u6237\u548c\u7ec4\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528WinNT ADSI Providernet user\u547d\u4ee4\u3002\u672c\u5730\u8d26\u6237\u6ca1\u6709\u57df\u8d26\u6237\u7684\u591a\u4e2a\u5c5e\u6027\uff0c\u56e0\u800c\u521b\u5efa\u8fc7\u7a0b\u5f88\u7b80\u5355\u3002\u521b\u5efa\u540d\u4e3a\u201cCreateLocalUser.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer=&quot;localhost&quot;, $group, $help)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: CreateLocalGroup.ps1 <\/p>\n<p>Creates a local group on either a local or remote machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computer Specifies the name of the computer upon which to run the script<\/p>\n<p>-group Name of group to create<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>CreateLocalGroup.ps1<\/p>\n<p>Generates an error. You must supply a group name<\/p>\n<p>CreateLocalGroup.ps1 -computer WebServer -group MyGroup<\/p>\n<p>Creates a local group called MyGroup on a computer named WebServer<\/p>\n<p>CreateLocalGroup.ps1 -group Mygroup<\/p>\n<p>Creates a local group called MyGroup on local computer<\/p>\n<p>CreateLocalGroup.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>if(!$group) <\/p>\n<p>{<\/p>\n<p>$(Throw &#8216;A value for $group is required. <\/p>\n<p>Try this: CreateLocalGroup.ps1 -help ?&#8217;)<\/p>\n<p>}<\/p>\n<p>$OBJou = [ADSI]&quot;WinNT:\/\/$computer&quot;<\/p>\n<p>$objUser = $objOU.Create(&quot;Group&quot;, $group)<\/p>\n<p>$objUser.SetInfo()<\/p>\n<p>$objUser.description = &quot;Test Group&quot;<\/p>\n<p>$objUser.SetInfo()<\/p>\n<p>\u5176\u4e2d\u9996\u5148\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u4e864\u4e2a\u53c2\u6570\uff0c\u5373-computer\u3001-user\u3001-password\u548c-help\uff0c\u968f\u540e\u5b9a\u4e49\u4e86funhelp\u51fd\u6570\u8f93\u51fa\u5e2e\u52a9\u4fe1\u606f\u3002\u5982\u679c$help\u53d8\u91cf\u5b58\u5728\uff0c\u5219\u663e\u793a\u4e00\u6761\u4fe1\u606f\u63d0\u793a\u6b63\u5728\u83b7\u53d6\u5e2e\u52a9\u6587\u4ef6\uff0c\u7136\u540e\u8c03\u7528funhelp\u51fd\u6570\u3002\u63a5\u4e0b\u6765\u68c0\u67e5\u8f93\u5165\u5185\u5bb9\u4e2d\u7684-user\u548c-password\u53c2\u6570\u662f\u5426\u88ab\u8d4b\u503c\uff0c\u5982\u679c\u503c\u4e0d\u5b58\u5728\uff0c\u5219\u76f4\u63a5\u8c03\u7528throw\u8bed\u53e5\u4ea7\u751f\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u505c\u6b62\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u7528\u6237\u540d\u548c\u5bc6\u7801\u5747\u5b58\u5728\u7684\u524d\u63d0\u4e0b\uff0c\u811a\u672c\u8c03\u7528[ADSI]\u7c7b\u578b\u7ea6\u675f\u8fde\u63a5\u5230\u672c\u5730\u8ba1\u7b97\u673a\u7684\u8d26\u6237\u6570\u636e\u5e93\uff0c\u5e76\u4f7f\u7528Create()\u65b9\u6cd5\u5229\u7528$user\u53d8\u91cf\u4e2d\u63d0\u4f9b\u7684\u7528\u6237\u540d\u521b\u5efa\u8d26\u6237\u3002\u968f\u540e\u8c03\u7528SetPasswod()\u65b9\u6cd5\u8bbe\u7f6e\u5bc6\u7801\uff0c\u5e76\u8c03\u7528SetInfo()\u65b9\u6cd5\u5c06\u5176\u5199\u5165\u6570\u636e\u5e93\u3002\u6700\u540e\u8bbe\u7f6eDescription\u5c5e\u6027\uff0c\u5e76\u518d\u6b21\u8c03\u7528SetInfo()\u3002<\/p>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662f\u8fd9\u4e2a\u811a\u672c\u4e2d\u4f7f\u7528\u7684SetInfo()\u65b9\u6cd5\u4ec5\u5728Windows Vista\u548cWindows 2008\u4ee5\u540e\u7248\u672c\u7684WMI\u6a21\u578b\u4e2d\u624d\u51fa\u73b0\uff0c\u5728Windows XP\u4e0b\u5b89\u88c5\u7684PowerShell\u4e2d\u6267\u884c\u5219\u62a5\u9519\u3002\u4f7f\u7528\u8fd9\u4e2a\u811a\u672c\u521b\u5efa\u7528\u6237\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>.\\CreateLocalUser.ps1 \u2013computer localhost -user UserForTest -password Passw0rd#!<\/p>\n<p>\u521b\u5efa\u7684\u7528\u6237\u80fd\u591f\u5728\u7ba1\u7406\u5de5\u5177\u4e2d\u770b\u5230\u3002<\/p>\n<p>\uff082\uff09\u521b\u5efa\u7528\u6237\u7ec4<\/p>\n<p>\u4e3a\u4e86\u6709\u6548\u5730\u63a7\u5236\u8bbf\u95ee\u672c\u5730\u8d44\u6e90\uff08\u5982\u5171\u4eab\u6587\u4ef6\u5939\u3001\u626b\u63cf\u4eea\u6216\u6253\u5370\u673a)\uff0c\u9700\u8981\u5728Windows Vista\u6216Windows 2008\u7cfb\u7edf\u4e2d\u521b\u5efa\u672c\u5730\u7528\u6237\u7ec4\u3002\u8fd9\u4e9b\u672c\u5730\u7528\u6237\u7ec4\u5982\u56fe2\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image12.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=\"367\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb12.png\" width=\"644\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe2 \u672c\u5730\u7528\u6237\u7ec4<\/p>\n<p>\u672c\u5730\u7528\u6237\u7ec4\u540c\u6837\u4f7f\u7528\u5de5\u4f5c\u7ec4\u8bbe\u7f6e\uff0c\u4e0e\u65b0\u5efa\u8d26\u6237\u5de5\u5177\u7c7b\u4f3c\u3002\u8ba1\u7b97\u673a\u7ba1\u7406\u63a7\u5236\u53f0\u4e2d\u4e5f\u63d0\u4f9b\u4e86\u201c\u65b0\u5efa\u7ec4\u201d\u5bf9\u8bdd\u6846\uff0c\u5982\u56fe3\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image13.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=\"356\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb13.png\" width=\"388\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe3 \u201c\u8ba1\u7b97\u673a\u7ba1\u7406\u201d\u63a7\u5236\u53f0\u4e2d\u7684\u201c\u65b0\u5efa\u7ec4\u201d\u5bf9\u8bdd\u6846<\/p>\n<p>\u4e3a\u4e86\u521b\u5efa\u672c\u5730\u7528\u6237\u7ec4\uff0c\u521b\u5efa\u540d\u4e3a\u201cCreateLocalGroup.ps1\u201d\u7684\u811a\u672c\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer=&quot;localhost&quot;, $group, $help)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: CreateLocalGroup.ps1 <\/p>\n<p>Creates a local group on either a local or remote machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computer Specifies the name of the computer upon which to run the script<\/p>\n<p>-group Name of group to create<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>CreateLocalGroup.ps1<\/p>\n<p>Generates an error. You must supply a group name<\/p>\n<p>CreateLocalGroup.ps1 -computer WebServer -group MyGroup<\/p>\n<p>Creates a local group called MyGroup on a computer named WebServer<\/p>\n<p>CreateLocalGroup.ps1 -group Mygroup<\/p>\n<p>Creates a local group called MyGroup on local computer<\/p>\n<p>CreateLocalGroup.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>if(!$group) <\/p>\n<p>{<\/p>\n<p>$(Throw &#8216;A value for $group is required. <\/p>\n<p>Try this: CreateLocalGroup.ps1 -help ?&#8217;)<\/p>\n<p>}<\/p>\n<p>$OBJou = [ADSI]&quot;WinNT:\/\/$computer&quot;<\/p>\n<p>$objUser = $objOU.Create(&quot;Group&quot;, $group)<\/p>\n<p>$objUser.SetInfo()<\/p>\n<p>$objUser.description = &quot;Test Group&quot;<\/p>\n<p>$objUser.SetInfo()<\/p>\n<p>\u5176\u4e2d\u9996\u5148\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u4e863\u4e2a\u53c2\u6570\uff0c\u5373-computer\u3001-group\u53ca-help\uff0c\u5e76\u5c06-computer\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u4e3alocalhost\u3002\u7136\u540e\u5b9a\u4e49funhelp\u51fd\u6570\u8f93\u51fa\u5e2e\u52a9\u4fe1\u606f\uff0c\u5f53\u7528\u6237\u63d0\u4f9b-help\u53c2\u6570\u65f6\u5c06\u8f93\u51fa\u5e2e\u52a9\u4fe1\u606f\u3002<\/p>\n<p>\u5728\u8fd0\u884c\u811a\u672c\u65f6\u9700\u8981\u63d0\u4f9b\u65b0\u5efa\u7ec4\u7684\u540d\u79f0\uff0c\u5982\u679c\u672a\u63d0\u4f9b$group\u53d8\u91cf\uff0c\u5219\u663e\u793a\u901a\u8fc7throw\u8bed\u53e5\u4ea7\u751f\u7684\u9519\u8bef\u4fe1\u606f\u3002\u521b\u5efa\u7528\u6237\u7ec4\u4e0d\u9700\u8981\u63d0\u4f9b\u5bc6\u7801\uff0c\u4f7f\u7528\u8be5\u811a\u672c\u521b\u5efa\u7528\u6237\u7ec4\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p>.\\CreateLocalGroup.ps1 -group NewGroups<\/p>\n<p>\u521b\u5efa\u7684\u7528\u6237\u7ec4\u5728\u8ba1\u7b97\u673a\u7ba1\u7406\u63a7\u5236\u53f0\u4e2d\u7684\u672c\u5730\u7ec4\u4fe1\u606f\u4e2d\u663e\u793a\uff0c\u8be5\u811a\u672c\u4e5f\u4f7f\u7528\u4e86\u4ec5\u9002\u7528\u4e8eWindows Vista\u548cWindows 2008\u4ee5\u4e0a\u7cfb\u7edf\u7684SetInfo()\u65b9\u6cd5\u3002<\/p>\n<p>\uff083\uff09\u7981\u7528\u6216\u542f\u7528\u7528\u6237\u8d26\u53f7<\/p>\n<p>\u672c\u5730\u7528\u6237\u8d26\u53f7\u4e3b\u8981\u7528\u4e8e\u8bbf\u95ee\u672c\u5730\u8d44\u6e90\u6216\u672c\u5730\u670d\u52a1\u7684\u8d26\u53f7\uff0c\u7cfb\u7edf\u7ba1\u7406\u5458\u53ef\u4ee5\u7981\u7528\u6216\u542f\u7528\u7279\u5b9a\u8d26\u6237\u6765\u63a7\u5236\u8d26\u6237\u7684\u53ef\u7528\u6027\u3002<\/p>\n<p>\u521b\u5efa\u540d\u4e3a\u201cEnableDisableUser.ps1\u201d\u7684\u811a\u672c\u7981\u7528\u6216\u542f\u7528\u7279\u5b9a\u8d26\u6237\u7684\u6743\u9650\uff0c\u5176\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>param($computer=&quot;localhost&quot;, $a, $user, $password, $help)<\/p>\n<p>function funHelp()<\/p>\n<p>{<\/p>\n<p>$helpText=@&quot;<\/p>\n<p>DESCRIPTION:<\/p>\n<p>NAME: EnableDisableUser.ps1 <\/p>\n<p>Enables or Disables a local user on either a local or remote machine.<\/p>\n<p>PARAMETERS: <\/p>\n<p>-computer Specifies the name of the computer upon which to run the script<\/p>\n<p>-a(ction) Action to perform &lt; e(nable) d(isable) &gt;<\/p>\n<p>-user Name of user to modify<\/p>\n<p>-help prints help file<\/p>\n<p>SYNTAX:<\/p>\n<p>EnableDisableUser.ps1<\/p>\n<p>Generates an error. You must supply a user name<\/p>\n<p>EnableDisableUser.ps1 -computer WebServer -user myUser <\/p>\n<p>-password Passw0rd^&amp;! -a e<\/p>\n<p>Enables a local user called myUser on a computer named WebServer<\/p>\n<p>with a password of Passw0rd^&amp;!<\/p>\n<p>EnableDisableUser.ps1 -user myUser -a d<\/p>\n<p>Disables a local user called myUser on the local machine<\/p>\n<p>EnableDisableUser.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>$EnableUser = 512 # ADS_USER_FLAG_ENUM enumeration value from SDK<\/p>\n<p>$DisableUser = 2 # ADS_USER_FLAG_ENUM enumeration value from SDK<\/p>\n<p>if($help){ &quot;Obtaining help &#8230;&quot; ; funhelp }<\/p>\n<p>if(!$user) <\/p>\n<p>{<\/p>\n<p>$(Throw &#8216;A value for $user is required. <\/p>\n<p>Try this: EnableDisableUser.ps1 -help ?&#8217;)<\/p>\n<p>}<\/p>\n<p>$ObjUser = [ADSI]&quot;WinNT:\/\/$computer\/$user&quot;<\/p>\n<p>switch($a)<\/p>\n<p>{<\/p>\n<p>&quot;e&quot; {<\/p>\n<p>if(!$password) <\/p>\n<p>{ <\/p>\n<p>$(Throw &#8216;a value for $password is required.<\/p>\n<p>Try this: EnableDisableUser.ps1 -help ?&#8217;)<\/p>\n<p>}<\/p>\n<p>$objUser.setpassword($password)<\/p>\n<p>$objUser.description = &quot;Enabled Account&quot;<\/p>\n<p>$objUser.userflags = $EnableUser<\/p>\n<p>$objUser.setinfo()<\/p>\n<p>}<\/p>\n<p>&quot;d&quot; {<\/p>\n<p>$objUser.description = &quot;Disabled Account&quot;<\/p>\n<p>$objUser.userflags = $DisableUser<\/p>\n<p>$objUser.setinfo()<\/p>\n<p>}<\/p>\n<p>DEFAULT <\/p>\n<p>{<\/p>\n<p>&quot;You must supply a value for the action.<\/p>\n<p>Try this: EnableDisableUser.ps1 -help ?&quot;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\u4f7f\u7528\u8be5\u811a\u672c\u53ef\u4ee5\u542f\u7528\u6216\u7981\u7528\u7279\u5b9a\u8d26\u6237\uff0c\u5e76\u66f4\u6539\u7279\u5b9a\u8d26\u53f7\u7684\u5bc6\u7801\u3002\u5728\u811a\u672c\u4e2d\u9996\u5148\u4f7f\u7528param\u8bed\u53e5\u5b9a\u4e49\u4e865\u4e2a\u53c2\u6570\uff0c\u5176\u4e2d-compute\u6307\u5b9a\u6267\u884c\u8be5\u811a\u672c\u7684\u4e3b\u673a\uff0c\u9ed8\u8ba4\u4e3a\u672c\u673alocalhost\uff1b-a\u6307\u5b9a\u8fd0\u884c\u811a\u672c\u6267\u884c\u7684\u64cd\u4f5c\uff1b-user\u548c-password\u6307\u5b9a\u5bc6\u7801\uff1b-help\u8c03\u7528funhelp\u51fd\u6570\u663e\u793a\u5e2e\u52a9\u7684\u5185\u5bb9\u3002\u5728funhelp\u51fd\u6570\u4e4b\u540e\u58f0\u660e\u4e86\u4e24\u4e2a\u53d8\u91cf\uff0c\u5176\u4e2d\u5305\u62ecADS_USER_FLAG_ENUM\u679a\u4e3e\u503c\uff0c\u76f8\u5173\u5177\u4f53\u7684\u5185\u5bb9\u53ef\u4ee5\u53c2\u8003Windows SDK\uff0c\u8fd9\u4e9b\u503c\u53ef\u4ee5\u7528\u4e8e\u542f\u52a8\u6216\u7981\u7528\u7528\u6237\u8d26\u53f7\u3002<\/p>\n<p>\u9700\u8981\u5f3a\u8c03\u7684\u662f\u867d\u7136ADS_USER_FLAG_ENUM\u679a\u4e3e\u503c\u5728Windows SDK\u4e2d\u6709\u8be6\u7ec6\u8bf4\u660e\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u4ecb\u7ecd\u5728PowerShell\u4e2d\u7684\u4f7f\u7528\u3002\u7531\u4e8e\u65e0\u6cd5\u83b7\u5f97PowerShell\u4e2dIadsUser\u7684\u76f4\u63a5\u652f\u6301\uff0c\u6240\u4ee5\u5728VBScript\u4e2d\u7528\u4e8e\u7981\u7528\u8d26\u6237\u7684AccountDisabled\u5e03\u5c14\u5c5e\u6027\u5728PowerShell\u4e2d\u65e0\u6cd5\u7ee7\u7eed\u4f7f\u7528\u3002\u8fd9\u6837\u4e4b\u524d\u7528\u4e8e\u7981\u7528\u8d26\u6237\u7684VBScript\u811a\u672c\u4e0d\u80fd\u79fb\u690d\u5230PowerShell\u4e2d\uff0c\u800cEnableDisableUser.ps1\u811a\u672c\u5e76\u4e0d\u80fd\u7531VBScript\u8f6c\u6362\u800c\u6765\u3002<\/p>\n<p>\u5b9a\u4e49\u8fd9\u4e24\u4e2a\u53d8\u91cf\u540e\u9700\u8981\u6d4b\u8bd5help\u53c2\u6570\u4ee5\u786e\u5b9a\u662f\u5426\u663e\u793a\u5e2e\u52a9\u4fe1\u606f\uff0c\u5728\u8fd9\u4e2a\u811a\u672c\u4e2d\u5fc5\u987b\u8f93\u5165\u53c2\u6570-user\uff0c\u5373\u7981\u7528\u6216\u542f\u7528\u7684\u7528\u6237\u540d\u3002\u5982\u679c\u8be5\u53c2\u6570\u4e3a\u7a7a\uff0c\u7cfb\u7edf\u5c06\u4f1a\u629b\u51fa\u9519\u8bef\u5e76\u505c\u6b62\u540e\u7eed\u8bed\u53e5\u7684\u6267\u884c\u3002\u68c0\u6d4b\u5230\u7528\u6237\u540d\u540e\uff0c\u4f7f\u7528[ADSI]\u7c7b\u578b\u7ea6\u675f\u7b26\u53caWinNT Active Directory\u670d\u52a1\u63a5\u53e3\uff08ADSI\uff09Provider\u8fde\u63a5\u5230\u672c\u5730\u8ba1\u7b97\u673a\u7684SAM\u8d26\u6237\u6570\u636e\u5e93\u5e76\u83b7\u53d6\u7528\u6237\u5bf9\u8c61\u3002<\/p>\n<p>\u83b7\u53d6\u7528\u6237\u5bf9\u8c61\u4e4b\u540e\u4f7f\u7528switch\u8bed\u53e5\u5224\u65ad$a\u53d8\u91cf\u7684\u503c\uff0c\u8be5\u53d8\u91cf\u7528\u4e8e\u6307\u5b9a\u811a\u672c\u4e2d\u6267\u884c\u7684\u76f8\u5e94\u64cd\u4f5c\u3002\u5982\u679c\u8981\u542f\u7528\u8d26\u6237\uff0c\u90a3\u4e48\u9700\u8981\u4e3a\u8be5\u8d26\u6237\u8bbe\u7f6e\u5bc6\u7801\uff0c\u5373\u4e3a\u53c2\u6570-a\u63d0\u4f9be\uff08enable\uff0c\u542f\u7528\uff09\u503c\u3002\u5f53-password\u53c2\u6570\u672a\u63a5\u6536\u5230\u5bc6\u7801\u65f6\u629b\u51fa\u5f02\u5e38\u63d0\u793a\u7528\u6237\u67e5\u770b\u5e2e\u52a9\u4fe1\u606f\u3002\u5982\u679c\u5bc6\u7801\u5b58\u5728\uff0c\u5219\u8c03\u7528SetPasswod\u65b9\u6cd5\u4e3a\u7528\u6237\u8bbe\u7f6e\u5bc6\u7801\u3002\u5e76\u5c06\u7528\u6237Description\uff08\u63cf\u8ff0\uff09\u5c5e\u6027\u4fee\u6539\u4e3a\u201cEnable Account\u201d\uff0c\u4f7f\u7528UserFlags\u5c5e\u6027\u503c\u542f\u7528\u8d26\u6237\uff0c\u6700\u540e\u8c03\u7528SetInfo()\u65b9\u6cd5\u5c06\u66f4\u6539\u63d0\u4ea4\u5230SAM\u8d26\u6237\u6570\u636e\u5e93\u3002\u542f\u7528\u8d26\u6237\u64cd\u4f5c\u547d\u4ee4\u7684\u6807\u51c6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>.\\EnableDisableUser.ps1 -user UserForTest -password Passw0rd#! -a e<\/p>\n<p>\u8981\u7981\u7528\u8d26\u6237\uff0c\u4e3a\u53c2\u6570-a\u4f20\u9012d\uff08disable\uff0c\u7981\u7528\uff09\u503c\u3002\u5e76\u4e3aUserFlags\u53c2\u6570\u8bbe\u7f6e\u9002\u5f53\u503c\uff0c\u8c03\u7528SetInfo()\u65b9\u6cd5\u5c06\u66f4\u6539\u63d0\u4ea4\u7ed9SAM\u8d26\u6237\u6570\u636e\u5e93\u3002\u5728\u8fd9\u91cc\u4e3a\u4e86\u4f7f\u6f14\u793a\u6548\u679c\u66f4\u4e3a\u660e\u663e\uff0c\u5728\u8c03\u7528SetInfo()\u65b9\u6cd5\u4e4b\u524d\u5c06\u7528\u6237\u7684Description\uff08\u63cf\u8ff0\uff09\u5c5e\u6027\u6539\u4e3adisable Account\u3002\u7981\u7528\u8d26\u6237\u64cd\u4f5c\u547d\u4ee4\u7684\u6807\u51c6\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>.\\EnableDisableUser.ps1 -user UserForTest -a d<\/p>\n<p>\u5982\u679c\u7528\u6237\u672a\u6307\u5b9a-a\u7684\u53c2\u6570\u4e3ae\u6216d\uff0c\u5219\u811a\u672c\u6267\u884c\u9ed8\u8ba4\u64cd\u4f5c\uff0c\u5728\u672c\u4f8b\u4e2d\u4f1a\u8f93\u51fa\u5b57\u7b26\u4e32\u63d0\u793a\u7528\u6237\u67e5\u770b\u5e2e\u52a9\u6587\u4ef6\u3002\u542f\u7528\u6216\u7981\u7528\u7528\u6237\u7684\u6267\u884c\u7ed3\u679c\u5982\u56fe4\u6240\u793a\u3002<\/p>\n<p align=\"center\"><a href=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image14.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=\"378\" alt=\"image\" src=\"http:\/\/txj.shell.tor.hu\/wp-content\/uploads\/2011\/01\/image_thumb14.png\" width=\"644\" border=\"0\" \/><\/a> <\/p>\n<p align=\"center\">\u56fe4 \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: http:\/\/txj.lzuer.com\/<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7cfb\u7edf\u7ba1\u7406\u5458\u7684\u91cd\u8981\u4efb\u52a1\u4e4b\u4e00\u662f\u7ba1\u7406\u7528\u6237\uff0c\u65e2\u5141\u8bb8\u7528\u6237\u505a\u6267\u884c\u76f8\u5e94\u7684\u64cd\u4f5c\uff0c\u53c8\u8981\u9632\u6b62\u56e0\u6743\u9650\u8bbe\u7f6e\u4e0d\u5408\u7406\u800c\u6f5c\u5728\u7684\u7528\u6237\u8d8a\u6743\u64cd\u4f5c\u3002 [&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],"tags":[],"class_list":["post-33331","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33331"}],"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=33331"}],"version-history":[{"count":1,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33331\/revisions"}],"predecessor-version":[{"id":33764,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=\/wp\/v2\/posts\/33331\/revisions\/33764"}],"wp:attachment":[{"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=33331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=33331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fuhaijun.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=33331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}