首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在运行window服务msi的powershell脚本中包含用户名和密码?

如何在运行window服务msi的powershell脚本中包含用户名和密码?
EN

Stack Overflow用户
提问于 2019-07-31 11:53:26
回答 2查看 2K关注 0票数 2

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

我已经做到了:

代码语言:javascript
复制
$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"

但是提供的凭据不被接受。

有什么建议吗?

EN

Stack Overflow用户

回答已采纳

发布于 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()方法,我看到以下几行:

代码语言:javascript
复制
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;

添加以下行可成功抑制安装期间对服务帐户凭据的任何请求:

代码语言:javascript
复制
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;

之后,我使用TechNet文章中的代码示例在安装后更改帐户。最终的脚本如下所示:

代码语言:javascript
复制
$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"
    } 

}

希望这对人们有所帮助。

票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57282932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档