【问题标题】:how to add credentials in powershell script, so that when i run the script it wont ask for password, I do not want password to be seen in the script [duplicate]如何在powershell脚本中添加凭据,这样当我运行脚本时它不会要求输入密码,我不希望在脚本中看到密码[重复]
【发布时间】:2023-03-08 21:30:01
【问题描述】:

当我在服务器名称搅拌 ub vnfqdn.txt 上从本地计算机运行以下脚本时,脚本暂停并要求我输入用户名和凭据。我想以安全的方式在脚本中自动提供密码。你能帮我在下面的脚本中添加任何可以提供帮助的命令吗?

$hostname=get-content C:\temp\vmfqdn.txt
$patchetest=Invoke-Command -ComputerName $hostname -Credential domain\username -ScriptBlock {(Get-HotFix -id "KB4534271").HotFixID}
if($patchetest -eq "KB4534271"){
   write-host("Patch KB4534271 is installed")
} else {
   write-host("Patch is not installed")
}

'''
how to add credentials in power shell script, so that when i run the script it wont ask for password, I do not want password to be seen in the script.

note- Above script works fine when I provide password manually when window asking for password prompts up. 

【问题讨论】:

  • 使用您想要的正确用户名从调度程序运行脚本。
  • 链接的帖子显示了如何将凭据保存到文件并稍后解密。注意:仅在 Windows 上使用此技术。它仅适用于给定计算机上的给定用户帐户。

标签: powershell credentials powershell-remoting


【解决方案1】:

您可以使用 Export-CLIXML 导出凭证对象。您也可以提示输入安全密码,然后将其转换为字符串等。但这是快速的方法。

try { $creds = Import-Clixml -Path "credentials.xml" }catch { $creds = Get-Credential }
$creds | Export-Clixml -Path "credentials.xml" 

【讨论】:

  • 不错;值得一提的是,这不应该在类 Unix 平台(macOS、Linux)上使用,因为那里不会应用加密。
  • 你能帮我把它放在上面提到的脚本中吗? .xml 的格式应该是什么?
  • 将我的代码粘贴到您的代码上方并将-Credential domain\username 替换为-Credential $creds 这是加密的,仅适用于您计算机上的帐户,因此您需要以将要执行的用户身份运行脚本它在将执行它的系统上。
【解决方案2】:

【讨论】:

  • 虽然链接页面可能会回答问题,但它是better to include the essential parts here 并提供链接仅供参考/附加信息。如果链接页面消失或发生重大变化,则仅链接答案可能会失效。如果您只能提供一个链接,请考虑将其作为评论而不是作为答案发布。
  • 嗨,这一切都解决了。下面是解决方案 $a = Get-Content C:\users\pwd.txt $securePassword = $a | ConvertTo-SecureString $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "username", $securePassword 然后在脚本需要身份验证时使用 $mycredential。
猜你喜欢
  • 2018-07-05
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 2020-02-16
  • 2012-04-28
  • 1970-01-01
相关资源
最近更新 更多