我使用 Powershell 将 vSphere 环境的凭据保存到文本文件中ConvertFrom-SecureString。我想使用以下命令在我的 powershell 脚本中使用此凭据:
$password4host = get-content C:\shutdown\HostsCred.txt | convertto-securestring
如果我想通过服务执行 powershell 脚本,我会收到无法从文本文件中读取凭据的错误。这似乎属于这样的事实:将安全字符串保存在文本文件中与将这些凭据保存到文本文件的用户绑定在一起。只有用户自己才能解密凭证。
代码片段:
$time = ( get-date ).ToString('HH-mm-ss')
$date = ( get-date ).ToString('dd-MM-yyyy')
$logfile = New-Item -type file "C:\shutdown\ShutdownLog-$date-$time.txt" -Force
Import-Module -Name VMware.*
# Some variables
$vcenter = "10.10.10.10"
$username = "fakeuser@vsphere.local"
$username4host = "fakeuser"
$cluster = "ESXi-Cluster"
$datacenter = "Datacenter"
$vCenterVMName = "VMware vCenter Server" #Name of vCenter VM
$StarWindVM1 = "Starwind-VM" #Name of first StarWind VM
$StarWindVM2 = "Starwind-VM (1)" #Name of …Run Code Online (Sandbox Code Playgroud)