简体   繁体   English

为什么这个 PS1 脚本会检查某个 arguments 是否可以在 setup.exe 上运行?

[英]Why this PS1 script lies to check if some arguments are possible on a setup.exe?

I have a PowerShell script that lies on the result, anyone know why?我有一个基于结果的 PowerShell 脚本,有人知道为什么吗?

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{ 
    InitialDirectory = [Environment]::GetFolderPath('Desktop') 
}
$null = $FileBrowser.ShowDialog()
$FilePath = $FileBrowser.FileName
 
$paramList = @("/S", "/s", "-s", "-S", "/silent", "/SILENT", "-silent", "-SILENT", "/quiet", "/QUIET", "-quiet", "-QUIET", "/q", "/Q", "-q", "-Q", "/qn", "/QN", "-qn", "-QN", "/passive", "/PASSIVE", "-passive", "-PASSIVE", "/qb", "/QB", "-qb", "-QB", "/qr", "/QR", "-qr", "-QR", "/norestart", "-norestart", "/NORESTART", "/VERYSILENT", "/verysilent", "-verysilent", "-VERYSILENT")
$validArgs = @()
foreach ($arg in $paramList) {
    $command = Get-Command $FilePath
    if ($command.parameters.keys -contains $arg) {
        $validArgs += $arg
    }
}
 
if ($validArgs) {
    Write-Host "Silent arguments available are : $validArgs"
} else {
    Write-Host "No silent arguments detected"
}
 
Start-Sleep -Seconds 10

It's to detect if these silent arguments are usable on a setup.exe这是为了检测这些无声的 arguments 是否可以在 setup.exe 上使用

Testable on the vcredist installer for example, which contains several of these possible arguments https://aka.ms/vs/17/release/vc_redist.x64.exe例如,可在 vcredist 安装程序上测试,其中包含其中几个可能的 arguments https://aka.ms/vs/17/release/vc_redist.x64.exe

The script is made with chatgpt, I tried some variants but no one is working.该脚本是用 chatgpt 制作的,我尝试了一些变体,但没有人在工作。

While Get-Command is capable of discovering external programs ( *.exe ) and reporting basic file-system metadata about them, it knows nothing about what parameters they support - that only works for PowerShell-native commands (that have formally declared parameters).虽然Get-Command能够发现外部程序( *.exe ) 并报告有关它们的基本文件系统元数据,但它对它们支持的参数一无所知——这仅适用于PowerShell 原生命令(具有正式声明的参数)。

Short of invoking an executable with -?缺少使用-? , -h , --help in order to invoke the command-line help - whatever parameter a given executable requires - and searching through the output, there is no general mechanism for discovering an executable's command-line parameters. , -h , --help以调用命令行帮助 - 给定可执行文件需要的任何参数 - 并搜索 output,没有用于发现可执行文件的命令行参数的通用机制。

As an aside: PowerShell is case- insensitive by default, so there's no need for matching case variations with operators such as -contains ;顺便说一句: -contains默认情况下不区分大小写,因此无需使用 -contains 等运算符来匹配大小写变体 eg, the following succeeds:例如,以下成功:

@('silent') -contains 'SiLenT' # -> true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM