如何在 powershell 脚本 运行 a window service msi 中包含用户名和密码

How do I include the username and password in a powershell script running a window service msi

我正在设置 powershell 脚本来自动为各种系统安装环境。 我需要编写 MSI 安装程序文件的 运行 脚本来设置 Windows 服务。 如何包含所需的用户名和密码以完成安装 'quiet'

我已经做到了:

$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"

但提供的凭据未被接受。

建议?

尝试用空格分隔您的参数:

 $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"
 }

首先感谢大家的帮助。

阅读您的建议和“相关”建议列表中的以下问题让我开始思考另一个方向 (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 文章中的代码示例更改帐户 post-安装。 最终脚本如下所示:

$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"
    } 

}

希望这对人们有所帮助。