2013-07-24 69 views
11

我对PowerShell非常陌生,理解起来有些困难。
我想在PowerShell脚本中安装.MSI
可以请解释我如何做到这一点或为我提供初学者级别的教程。如何使用PowerShell安装.MSI

$wiObject = New-Object -ComObject WindowsInstaller.Installer 
????? 

回答

12

为什么会这么喜欢它?只需调用.msi文件:

​​

Start-Process <path>\filename.msi 

编辑:开始处理的全部列表参数

https://ss64.com/ps/start-process.html

+0

你可以简单地添加开关和其他参数呢? – Brettski

4

您可以使用:

msiexec /i "c:\package.msi" 

您还可以添加更多可选参数。有一些常见的msi参数和参数是您的安装程序特有的。对于常见参数,请致电msiexec

4

@ Adi的答案中的<path>部分可能会让一个完全陌生于PowerShell或其他命令shell的人感到困惑。

使用windows GUI查找路径的值,右键单击.msi文件,选择'属性',然后选择'细节'。在“文件夹路径”下,您将看到需要写入的内容。

所以你的命令看起来像(例如)

& C:\Users\YourName\YourDirectory\filename.msi 

这是有目共睹的谁使用StackOverflow的大多数人,而是一个真正的新手可以很容易误入歧途。

+0

我重写了这个,以避免使用相对答案的位置,他们可以随时更改 – neontapir

+0

谢谢!我现在明白了。 – ngks

+0

像这样的说明应作为评论或建议的修改发布,而不是以不同的方式表达同一事物的新答案。无论如何,我相信任何精通计算机的人都可以首先使用PowerShell,而不会有任何理解这一点的麻烦。我还没有看到任何人有这种困惑的表示,你会一直看到他们的答案。就我个人而言,我认为它比你拥有的方式更好。此外,如果.msi不在C:\ Users中,为什么不能像您的表示一样混淆假设的noob? –

6
#Variables 
$computername = Get-Content 'M:\Applications\Powershell\comp list\Test.txt' 
$sourcefile = "\\server\Apps\LanSchool 7.7\Windows\Student.msi" 
#This section will install the software 
foreach ($computer in $computername) 
{ 
    $destinationFolder = "\\$computer\C$\download\LanSchool" 
    #This section will copy the $sourcefile to the $destinationfolder. If the Folder does not exist it will create it. 
    if (!(Test-Path -path $destinationFolder)) 
    { 
     New-Item $destinationFolder -Type Directory 
    } 
    Copy-Item -Path $sourcefile -Destination $destinationFolder 
    Invoke-Command -ComputerName $computer -ScriptBlock { & cmd /c "msiexec.exe /i c:\download\LanSchool\Student.msi" /qn ADVANCED_OPTIONS=1 CHANNEL=100} 
} 

我已经为自己找遍了所有东西,想出了zilch,但终于拼凑起了这个工作脚本。它工作得很好!以为我会在这里发布希望其他人可以受益。它提取计算机列表,将文件复制到本地计算机并运行。 :) 派对!

1
#$computerList = "Server Name" 
#$regVar = "Name of the package " 
#$packageName = "Packe name " 
$computerList = $args[0] 
$regVar = $args[1] 
$packageName = $args[2] 
foreach ($computer in $computerList) 
{ 
    Write-Host "Connecting to $computer...." 
    Invoke-Command -ComputerName $computer -Authentication Kerberos -ScriptBlock { 
    param(
     $computer, 
     $regVar, 
     $packageName 
     ) 

     Write-Host "Connected to $computer" 
     if ([IntPtr]::Size -eq 4) 
     { 
      $registryLocation = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\" 
      Write-Host "Connected to 32bit Architecture" 
     } 
     else 
     { 
      $registryLocation = Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\" 
      Write-Host "Connected to 64bit Architecture" 
     } 
     Write-Host "Finding previous version of `enter code here`$regVar...." 
     foreach ($registryItem in $registryLocation) 
     { 
      if((Get-itemproperty $registryItem.PSPath).DisplayName -match $regVar) 
      { 
       Write-Host "Found $regVar" (Get-itemproperty $registryItem.PSPath).DisplayName 
       $UninstallString = (Get-itemproperty $registryItem.PSPath).UninstallString 
        $match = [RegEx]::Match($uninstallString, "{.*?}") 
        $args = "/x $($match.Value) /qb" 
        Write-Host "Uninstalling $regVar...." 
        [diagnostics.process]::start("msiexec", $args).WaitForExit() 
        Write-Host "Uninstalled $regVar" 
      } 
     } 

     $path = "\\$computer\Msi\$packageName" 
     Write-Host "Installaing $path...." 
     $args = " /i $path /qb" 
     [diagnostics.process]::start("msiexec", $args).WaitForExit() 
     Write-Host "Installed $path" 
    } -ArgumentList $computer, $regVar, $packageName 
Write-Host "Deployment Complete" 

} 
+1

你必须给你的答案一些意见 – ohlmar

1

一些考验和磨难之后,我能找到的所有的.msi文件在给定的目录,并安装它们。

foreach($_msiFiles in 
($_msiFiles = Get-ChildItem $_Source -Recurse | Where{$_.Extension -eq ".msi"} | 
Where-Object {!($_.psiscontainter)} | Select-Object -ExpandProperty FullName)) 
{ 
    msiexec /i $_msiFiles /passive 
}