【发布时间】:2015-11-27 13:20:21
【问题描述】:
我正在开发一个小型软件部署脚本,该脚本使用 powershell 远程卸载和安装 Check_MK 的新版本。
一切都很好——我能够本地化旧服务,停止它并确定它的安装路径。问题是运行服务的“uninstall.exe”。
这可以通过在使用凭据参数的服务器上使用 Powershell 会话记录来轻松完成。
$passwd = convertto-securestring -AsPlainText -Force -String "password"
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "domain\user",$passwd
$session = new-pssession -computername "192.xxx.xxx.xxx" -credential $cred
Enter-PSSession $session
问题是您不能在脚本中使用 PSSession - 它是供个人使用而不是自动化的。
所以我尝试使用 Powershells Invoke-Command cmdlet。
Invoke-Command -Computername "192.xxx.xxx.xxx" -credential $cred -argumentlist $cred -ScriptBlock {
# Lots of stuff and enter uninstall directory
uninstall.exe /S
}
调用 exe 文件以查询管理员凭据结束。
我还尝试远程启动提升的 Powershell 会话并将其用于卸载..
Start-Process powershell -Credential $cred
使用提升的 Powershell 会话执行位于服务器上以启动 uninstall.exe 的脚本以 UAC 查询结束(不是查询凭据,而是要求执行)。
还有其他解决方案可以解决这个问题吗?我尝试了更多,但没有任何效果。
【问题讨论】:
-
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist "domain\user",$passwd你在“argumentlist”中有错字。也许这是错误... -
看不到任何错字? $cred 对象也可以工作,因为我可以通过 PSSession 成功登录计算机。
-
现在我明白了。粘贴在这里只是失败 - 脚本中的代码是可以的。
-
invoke-command将pssession作为参数,因此无需重新输入计算机名、凭据等 -
问题是我必须为 40 到 50 台机器这样做,我不想为每台机器启动 PSSession。
标签: powershell credentials runas invoke-command start-process