{"id":207001,"date":"2025-07-08T08:39:47","date_gmt":"2025-07-08T00:39:47","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207001\/"},"modified":"2025-07-08T08:39:47","modified_gmt":"2025-07-08T00:39:47","slug":"powershell-%e8%84%9a%e6%9c%ac%e7%8b%ac%e7%ab%8b%e8%bf%90%e8%a1%8c%ef%bc%8c%e4%bd%86%e4%bd%9c%e4%b8%ba%e5%8f%af%e6%89%a7%e8%a1%8c%e6%96%87%e4%bb%b6%e8%bf%90%e8%a1%8c%e6%97%b6%e5%87%ba%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207001\/","title":{"rendered":"Powershell \u811a\u672c\u72ec\u7acb\u8fd0\u884c\uff0c\u4f46\u4f5c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u8fd0\u884c\u65f6\u51fa\u73b0\u201c\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\u201d\u9519\u8bef"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>Powershell \u811a\u672c\u72ec\u7acb\u8fd0\u884c\uff0c\u4f46\u4f5c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u8fd0\u884c\u65f6\u51fa\u73b0\u201c\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\u201d\u9519\u8bef<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 13:27:33<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5c0f\u4f19\u4f34\u4eec\u5bf9Golang\u7f16\u7a0b\u611f\u5174\u8da3\u5417\uff1f\u662f\u5426\u6b63\u5728\u5b66\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff1f\u5982\u679c\u662f\uff0c\u90a3\u4e48\u672c\u6587\u300aPowershell \u811a\u672c\u72ec\u7acb\u8fd0\u884c\uff0c\u4f46\u4f5c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u8fd0\u884c\u65f6\u51fa\u73b0\u201c\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\u201d\u9519\u8bef\u300b\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff0c\u672c\u7bc7\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\u3002\u5728\u4e4b\u540e\u7684\u6587\u7ae0\u4e2d\u4e5f\u4f1a\u591a\u591a\u5206\u4eab\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4ee5\u4e0b\u662f\u6211\u7684 powershell \u811a\u672c\u7247\u6bb5\uff0c\u7528\u4e8e\u83b7\u53d6\u6240\u6709\u5df2\u5b89\u88c5\u7684\u5e94\u7528\u7a0b\u5e8f\uff1a<\/p>\n<pre>$tPatchObject = @() #initialize array\n\n#invoking the native powershell(32 bit\/64 bit)\n$tPatchObject =  &amp;\"$env:systemroot\\sysnative\\windowspowershell\\v1.0\\powershell.exe\" -NonInteractive -NoProfile -Command {Get-Childitem \"HKLM:\/\/SOFTWARE\/\/Microsoft\/\/Windows\/\/CurrentVersion\/\/Uninstall\/\/*\" | Get-ItemProperty | Sort-Object DisplayName -Unique;};\n\n#appending applications from a different path to same array\n$tPatchObject += &amp;\"$env:systemroot\\sysnative\\windowspowershell\\v1.0\\powershell.exe\" -NonInteractive -NoProfile -Command {Get-Childitem \"HKLM:\/\/SOFTWARE\/\/Wow6432Node\/\/Microsoft\/\/Windows\/\/CurrentVersion\/\/Uninstall\/\/*\" | Get-ItemProperty | Sort-Object DisplayName -Unique}<\/pre>\n<p>\u5728 windows server 2008 r2 \u7cfb\u7edf\u4e0a\u8fdb\u884c\u6d4b\u8bd5\u65f6\uff0c\u5982\u679c\u76f4\u63a5\u4ece powershell \u6267\u884c\uff0c\u5b83\u4f1a\u7ed9\u51fa\u6b63\u786e\u7684\u8f93\u51fa\u3002\u4f46\u662f\uff0c\u5f53\u4f5c\u4e3a golang \u4e2d\u7f16\u8bd1\u7684\u53ef\u6267\u884c\u6587\u4ef6\u7684\u4e00\u90e8\u5206\u6267\u884c\u76f8\u540c\u7684\u811a\u672c\u65f6\uff0c\u4f1a\u51fa\u73b0\u9519\u8bef <\/p>\n<p> \u65b9\u6cd5\u8c03\u7528\u5931\u8d25\uff0c\u56e0\u4e3a [system.management.automation.psobject] \u4e0d\u5305\u542b\u540d\u4e3a\u201cop_addition\u201d\u7684\u65b9\u6cd5\u3002 <\/p>\n<p>\u4ec5\u5728 windows server 2008 \u7cfb\u7edf\u4e0a\u9047\u5230\u6b64\u95ee\u9898\u3002\u6211\u7684\u5047\u8bbe\u662f\u7531\u4e8e\u8c03\u7528 <code>&amp;\"powershell.exe\"<\/code> <\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bd5\u8bd5\u8fd9\u4e2a\uff0c<\/p>\n<pre>cmd := `Get-Childitem \"HKLM:\/\/SOFTWARE\/\/Microsoft\/\/...`\n\nps, _ := exec.LookPath(\"powershell.exe\")\nargs := append([]string{\"-NoProfile\", \"-NonInteractive\", cmd})\n\nout, _ := exec.Command(ps, args...).Output()\n<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPowershell \u811a\u672c\u72ec\u7acb\u8fd0\u884c\uff0c\u4f46\u4f5c\u4e3a\u53ef\u6267\u884c\u6587\u4ef6\u8fd0\u884c\u65f6\u51fa\u73b0\u201c\u65b9\u6cd5\u8c03\u7528\u5931\u8d25\u201d\u9519\u8bef\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207001","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207001","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207001"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207001\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}