{"id":505,"date":"2024-07-08T17:24:01","date_gmt":"2024-07-08T09:24:01","guid":{"rendered":"http:\/\/xiyu12.top\/?p=505"},"modified":"2024-07-08T17:24:01","modified_gmt":"2024-07-08T09:24:01","slug":"%e5%87%ad%e8%af%81%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"http:\/\/www.xiyu12.top\/?p=505","title":{"rendered":"\u51ed\u8bc1\u5bf9\u8c61"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u51ed\u8bc1\u5bf9\u8c61\uff08PSCredential\uff09\u662f\u4e00\u4e2a PowerShell \u7c7b\u578b\uff0c\u7528\u4e8e\u5b89\u5168\u5730\u5b58\u50a8\u548c\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u51ed\u8bc1\u4fe1\u606f\u3002\u5b83\u5728\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u573a\u666f\u4e2d\u5e7f\u6cdb\u4f7f\u7528\uff0c\u7279\u522b\u662f\u5728\u811a\u672c\u81ea\u52a8\u5316\u548c\u8fdc\u7a0b\u64cd\u4f5c\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">PSCredential \u5bf9\u8c61\u5305\u542b\u4e24\u4e2a\u4e3b\u8981\u90e8\u5206\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u7528\u6237\u540d<\/strong>\uff1a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u7528\u6237\u7684\u540d\u79f0\u6216\u8d26\u6237\u3002<\/li>\n\n\n\n<li><strong>\u5bc6\u7801<\/strong>\uff1a\u4e00\u4e2a SecureString \u5bf9\u8c61\uff0c\u7528\u4e8e\u5b89\u5168\u5b58\u50a8\u7528\u6237\u7684\u5bc6\u7801\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 PSCredential \u5bf9\u8c61\u7684\u573a\u666f<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8fdc\u7a0b\u8fde\u63a5<\/strong>\uff1a\u5728\u4f7f\u7528 PowerShell \u8fdc\u7a0b\u7ba1\u7406\u529f\u80fd\uff08\u5982 Enter-PSSession \u6216 Invoke-Command\uff09\u65f6\u63d0\u4f9b\u51ed\u8bc1\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u5b89\u5168\u8d44\u6e90<\/strong>\uff1a\u5982\u8bbf\u95ee\u5171\u4eab\u6587\u4ef6\u3001\u6570\u636e\u5e93\u6216\u5176\u4ed6\u9700\u8981\u8ba4\u8bc1\u7684\u670d\u52a1\u3002<\/li>\n\n\n\n<li><strong>\u81ea\u52a8\u5316\u811a\u672c<\/strong>\uff1a\u5728\u811a\u672c\u4e2d\u81ea\u52a8\u6267\u884c\u9700\u8981\u8eab\u4efd\u9a8c\u8bc1\u7684\u4efb\u52a1\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u6bcf\u6b21\u8f93\u5165\u51ed\u8bc1\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>SecureString<\/strong>\uff1aPSCredential \u5bf9\u8c61\u7684\u5bc6\u7801\u90e8\u5206\u4f7f\u7528 SecureString \u7c7b\u578b\uff0c\u907f\u514d\u660e\u6587\u5bc6\u7801 \u786e\u4fdd\u5bc6\u7801\u5728\u5185\u5b58\u4e2d\u662f\u52a0\u5bc6\u7684\uff0c\u51cf\u5c11\u4e86\u6cc4\u9732\u7684\u98ce\u9669\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u51ed\u8bc1\u5bf9\u8c61\u4e0e\u8bbf\u95ee\u4ee4\u724c\u7684\u5173\u7cfb\uff1f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u51ed\u8bc1\u5bf9\u8c61<\/strong>\uff1a\u7528\u4e8e\u6700\u521d\u7684\u8eab\u4efd\u9a8c\u8bc1\u6b65\u9aa4\uff0c\u4f8b\u5982\u5728\u811a\u672c\u4e2d\u81ea\u52a8\u767b\u5f55\u5230\u67d0\u4e2a\u670d\u52a1\u6216\u7cfb\u7edf\u3002<\/li>\n\n\n\n<li><strong>\u8bbf\u95ee\u4ee4\u724c<\/strong>\uff1a\u901a\u5e38\u5728\u8eab\u4efd\u9a8c\u8bc1\u6210\u529f\u540e\uff0c\u7531\u8eab\u4efd\u63d0\u4f9b\u8005\u751f\u6210\u5e76\u9881\u53d1\uff0c\u968f\u540e\u7528\u4e8e\u6388\u6743\u8bbf\u95ee\u7279\u5b9a\u8d44\u6e90\u3002<br>\u901a\u8fc7 PowerShell \u7684 <code>New-Object System.Management.Automation.PSCredential<\/code> \u521b\u5efa\uff0c\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u51ed\u8bc1\u5bf9\u8c61\uff1a\u7528\u4e8e\u9700\u8981\u7528\u6237\u8ba4\u8bc1\u7684\u573a\u666f\uff0c\u4f8b\u5982\u8fde\u63a5\u8fdc\u7a0b\u8ba1\u7b97\u673a\u3001\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u3001\u6267\u884c\u9700\u8981\u6743\u9650\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bbf\u95ee\u4ee4\u724c\uff1a\u7528\u4e8e\u6388\u6743\u7528\u6237\u8bbf\u95eeAPI\u3001Web\u670d\u52a1\u6216\u5176\u4ed6\u53d7\u4fdd\u62a4\u8d44\u6e90\u3002\u4ee4\u724c\u5728\u6bcf\u6b21\u8bf7\u6c42\u65f6\u4f20\u9012\u7ed9\u8d44\u6e90\u670d\u52a1\u5668\u4ee5\u9a8c\u8bc1\u7528\u6237\u7684\u8bbf\u95ee\u6743\u9650<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u521b\u5efa\u51ed\u8bc1\u5bf9\u8c61<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u51ed\u8bc1\u5bf9\u8c61,\u901a\u8fc7 PowerShell \u7684 <code>New-Object System.Management.Automation.PSCredential<\/code> \u521b\u5efa\uff0c\u4f7f\u7528\u7528\u6237\u540d\u548c\u5bc6\u7801\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$pass = ConvertTo-SecureString &#8220;W3_4R3_th3_f0rce.&#8221; -AsPlainText -Force<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">$cred = New-Object System.Management.Automation.PSCredential(&#8220;ACUTE\\imonks&#8221;, $pass)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enter-PSSession -ComputerName ATSSERVER -Credential $cred  #\u8fde\u63a5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 PSCredential \u5bf9\u8c61\u8fdb\u884c\u8fdc\u7a0b\u8fde\u63a5<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u4f7f\u7528 Enter-PSSession<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Enter-PSSession<\/code> \u547d\u4ee4\u7528\u4e8e\u5efa\u7acb\u4e0e\u8fdc\u7a0b\u8ba1\u7b97\u673a\u7684\u4ea4\u4e92\u5f0f\u4f1a\u8bdd\uff0c\u7c7b\u4f3c\u4e8e SSH \u7684\u6548\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">powershell\u590d\u5236\u4ee3\u7801<code>Enter-PSSession -ComputerName RemoteServer01 -Credential $credential\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u8be5\u547d\u4ee4\u540e\uff0cPowerShell \u4f1a\u8bdd\u5c06\u5207\u6362\u5230\u8fdc\u7a0b\u8ba1\u7b97\u673a\uff0c\u53ef\u4ee5\u5728\u5176\u4e2d\u4ea4\u4e92\u5f0f\u5730\u6267\u884c\u547d\u4ee4\u3002\u8981\u9000\u51fa\u8fdc\u7a0b\u4f1a\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>Exit-PSSession<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.\u4f7f\u7528 Invoke-Command<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Invoke-Command<\/code> \u547d\u4ee4\u7528\u4e8e\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u6267\u884c\u547d\u4ee4\u6216\u811a\u672c\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Invoke-Command -ComputerName RemoteServer01 -Credential $credential -ScriptBlock {<br>    # \u5728\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u6267\u884c\u7684\u547d\u4ee4  Get-Process}<\/code><br><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u8bbf\u95ee\u8fdc\u7a0b\u6587\u4ef6\u5171\u4eab<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u51ed\u8bc1\u5bf9\u8c61\u8bbf\u95ee\u8fdc\u7a0b\u6587\u4ef6\u5171\u4eab\u6216\u5176\u4ed6\u8d44\u6e90\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>$remotePath = \"\\\\RemoteServer01\\SharedFolder\"<br>New-PSDrive -Name Z -PSProvider FileSystem -Root $remotePath -Credential $credential<br><br># \u8bbf\u95ee\u6620\u5c04\u7684\u9a71\u52a8\u5668<br>Get-ChildItem Z:<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u6574\u4e2a\u6d41\u7a0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\n# \u5c06\u660e\u6587\u5bc6\u7801\u8f6c\u6362\u4e3a SecureString\n$password = ConvertTo-SecureString \"W3_4R3_th3_f0rce.\" -AsPlainText -Force\n\n# \u521b\u5efa PSCredential \u5bf9\u8c61\n$credential = New-Object System.Management.Automation.PSCredential(\"ACUTE\\imonks\", $password)\n\n# \u4f7f\u7528 Enter-PSSession \u8fdb\u884c\u8fdc\u7a0b\u8fde\u63a5\nEnter-PSSession -ComputerName RemoteServer01 -Credential $credential\n\n# \u5728\u8fdc\u7a0b\u4f1a\u8bdd\u4e2d\u6267\u884c\u547d\u4ee4\nGet-Process\n\n# \u9000\u51fa\u8fdc\u7a0b\u4f1a\u8bdd\nExit-PSSession\n\n# \u4f7f\u7528 Invoke-Command \u5728\u8fdc\u7a0b\u8ba1\u7b97\u673a\u4e0a\u6267\u884c\u547d\u4ee4\nInvoke-Command -ComputerName RemoteServer01 -Credential $credential -ScriptBlock {\n    Get-Service\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u51ed\u8bc1\u5bf9\u8c61\uff08PSCredential\uff09\u662f\u4e00\u4e2a PowerShell \u7c7b\u578b\uff0c\u7528\u4e8e\u5b89\u5168\u5730\u5b58\u50a8\u548c\u4f20\u9012\u7528\u6237\u540d\u548c\u5bc6\u7801\u7b49\u51ed\u8bc1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[64,67,103],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-windows","tag-powershell","tag-pwershell","tag-103"],"_links":{"self":[{"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=505"}],"version-history":[{"count":0,"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=\/wp\/v2\/posts\/505\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.xiyu12.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}