问题描述
|
下面是我的代码:
$s = Get-WmiObject -computer 10.10.zz.zz Win32_Service -Filter \"Name=\'XXX\'\" -credential (Get-Credential XXXXXX\\fanwx)
$s.stopservice()
copy-item D:\\.....\\aaa.exe -destination \\\\10.10.zz.zz\\c$\\vvv\\
copy-item D:\\.....\\aaa.pdb -destination \\\\10.10.zz.zz\\c$\\vvv\\
$ s.startservice()
每次执行时,都会提示输入远程服务器的密码。有没有一种方法可以让我仅在Powershell中输入一次或在Credential Manager中读取凭证?
谢谢。
解决方法
刚开始
$cred = Get-Credential \"XXXXXX\\fanwx\"
之后 :
$s = Get-WmiObject -computer 10.10.zz.zz Win32_Service -Filter \"Name=\'XXX\'\" -credential $cred
您可以将密码放在磁盘上:
PS > $cred.Password | ConvertFrom-SecureString | Set-Content c:\\temp\\password.txt
并通过以下方式恢复:
$password = Get-Content c:\\temp\\password.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PsCredential \"UserName\",$password