我正在设置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"但是提供的凭据不被接受。
有什么建议吗?
发布于 2019-08-01 07:59:32
首先,感谢大家的帮助。
通读你的建议和“相关”建议列表中的以下问题让我想到了另一个方向(Msi insaller passing paramenter from command prompt for Set Service Login)。
然后我找到了这篇文章:Change Service Account Username & Password–PowerShell Script
因此,我的新计划是通过代码在安装程序中默认服务帐户,然后在安装后使用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"
}
}希望这对人们有所帮助。
发布于 2019-07-31 13:31:53
试着用空格分隔你的参数:
$installArgs = "UserName=$username Password=$password" 之后,您可以通过以下方式调用msiexec:
$msiArgs = "/i ""{0}"" /qn /norestart /l*v ""C:\temp\msiInstall.log"" {1}" -f $msiLocation,$installArgs
$process = Start-Process -FilePath "$env:systemroot\system32\msiexec.exe" -ArgumentList $msiArgs -Wait -PassThru
if ($process.ExitCode -ne 0) {
Write-Error "Installation failed"
}https://stackoverflow.com/questions/57282932
复制相似问题