powershell - 如何在运行窗口服务 msi 的 powershell 脚本中包含用户名和密码
问题描述
我正在设置 powershell 脚本来自动化各种系统的环境安装。我需要编写 MSI 安装程序文件的运行脚本来设置 Windows 服务。如何包含所需的用户名和密码以使安装完全“安静”
我已经到了:
$installFile = "C:\[path]\Installer.msi"
$username = "[ad user account]"
$password = convertto-securestring -String "[secure password]" -AsPlainText -Force
msiexec /i $installFile /quiet "UserName=$username,Password=$password"
但不接受提供的凭据。
建议?
解决方案
首先,感谢大家的帮助。
阅读您的建议和“相关”建议列表中的以下问题让我想到了另一个方向(Msi insaller 从命令提示符传递参数以设置服务登录)。
多打几下,我发现了这篇文章: 更改服务帐户用户名和密码–PowerShell脚本
因此,我的新计划是通过代码默认安装程序中的服务帐户,然后在安装后使用 PowerShell 更改它。在 Windows 服务的源代码中,我有一个 ProjectInstaller.cs 文件。打开 ProjectInstaller.Designer.cs 代码并查看 InitializeComponent() 方法,我看到以下几行:
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
在下面添加以下行成功地抑制了安装期间对服务帐户凭据的任何请求:
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
之后,我使用 TechNet 文章中的代码示例在安装后更改帐户。最终脚本如下所示:
$serviceName = "Service"
$oldInstallFile = "C:\Old_Installer.msi" #Only required if upgrading a previous installation
$installFile = "C:\New_Installer.msi"
$serviceConfigSource = "C:\Config"
$serviceConfigSourceFile = "C:\Config\Service.exe.config"
$serviceConfigDestinationFile = "C:\Program Files (x86)\Service\Service.exe.config"
$username = "[UserName]"
$password = "[Password]"
#Checking for existing service installation and uninstalling it
$existingService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
if($existingService)
{
Write-Host "$serviceName found. Begining the uninstall process..."
if($existingService.Started)
{
$existingService.StopService()
Start-Sleep -Seconds 5
}
msiexec /uninstall $oldInstallFile /quiet
Start-Sleep -Seconds 5
Write-Host "$serviceName Uninstalled."
}
#Install New service
Write-Host "$serviceName installation starting."
msiexec /i $newInstallFile /quiet
Start-Sleep -Seconds 5
Write-Host "$serviceName installation completed."
#copy config file
if(Test-Path $serviceConfigSource)
{
Copy-Item -Path $serviceConfigSourceFile -Destination $serviceConfigDestinationFile -Force
}
#Final service setup and start up
$newService = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
$changeStatus = $newService.Change($null,$null,$null,$null,$null,$null,$userName,$password,$null,$null,$null)
if ($changeStatus.ReturnValue -eq "0")
{
Write-Host "$serviceName -> Sucessfully Changed User Name"
}
if(!$newService.Started)
{
$startStatus = $newService.StartService()
if ($startStatus.ReturnValue -eq "0")
{
Write-Host "$serviceName -> Service Started Successfully"
}
}
希望这对人们有所帮助。
推荐阅读
- javascript - 如何让 console.log() 打印在范围输入上选择的值?
- email - 条纹支付链接
- python - 如何将用户操作从服务器中继到外部设备
- spring-boot - Spring Boot - 发布者无法在 Kafka 上发布消息
- jdbc - H2 SQL 语句中是否有语法错误?
- python - KivyMD 文件管理器功能
- javascript - 基于异步代码循环遍历数组和更新变量
- python - 使用通用全局对象将逻辑分成不同的模块
- javascript - 续集 TypeScript getter 和 setter
- javascript - 删除从零开始的 URL 参数