首页 > 解决方案 > PowerShell脚本查找文件然后启动引用它的命令?

问题描述

我正在尝试在 PowerShell 中编写一个脚本,该脚本将在文件夹中搜索特定的 msi 文件(子文件夹位置会有所不同),然后运行引用该文件和其他位于同一目录中的文件的安装命令。这将是一个工具,我们的 L1 帮助台人员可以使用它来运行通过 SCCM 分配的软件的安装,可能使用与 SCCM 运行的不同的命令行变量。

尝试了不同的代码组合,无法安装应用程序。

这在查找软件时效果很好

Get-ChildItem -Path C:\Windows\ccmcache -Recurse -Filter softwarename.msi

ForEach 部分不起作用

$Path = Get-ChildItem -Path C:\Windows\CCMCache -Recurse -Filter Something.MSI

ForEach ( $Installer in ( Get-ChildItem -Path $Path.DirectoryName -Filter *.MSI ) ) {

Start-Process -Wait -FilePath C:\windows\system32\msiexec.exe -ArgumentList "/i $Installer.FullName"
}

我基本上想做这样的事情,使用找到 msi 文件的目录进行安装:

msiexec /i 软件名.msi /q /norestart

标签: powershell

解决方案


我看到的唯一错误是你通常不能在不添加 $() 的情况下引用字符串中对象的属性:

Start-Process -Wait -FilePath C:\windows\system32\msiexec.exe -ArgumentList "/i $($Installer.FullName)"

为什么不直接运行 msiexec?

msiexec /i $installer.fullname

推荐阅读