【问题标题】:Trying to Use a ForEach Loop to get a product version off of an .exe, then based on that version, execute a batch of functions尝试使用 ForEach 循环从 .exe 中获取产品版本,然后基于该版本执行一批功能
【发布时间】:2021-12-21 19:07:18
【问题描述】:

提前感谢您查看。我只是想看看其他人可能会做些什么来让它正常工作。对 PowerShell 和 Python 来说非常陌生,所以我现在只是在工作时尝试使用它。

这个脚本是一个小项目。

    function EXEversion {

    $prod1 = (Get-item "Path of exe").VersionInfo.ProductVersion
    $prod2 = (Get-item "Path of exe").VersionInfo.ProductVersion
    $prod3 = (Get-item "Path of exe").VersionInfo.ProductVersion
    $prod3 = (Get-item "Path of exe").VersionInfo.ProductVersion
    $MyArray = @($prod1, $prod2, $prod3, $prod4)

在下面查看我的 Foreach 循环:

    foreach ($item in $MyArray){
        if ($item.VersionInfo.ProductVersion -eq 15.9...)
    {
    # Here is where my other list of functions would go that delete certain items #
    }

        }

    elseif ($item.VersionInfo.ProductVersion -eq 16.0)
        {
         # Other list of functions dedicated for if this specific version is found)
        }
    }
    

不幸的是,我知道我的缩进和格式都是错误的,我对 PS 非常陌生,并且仍在学习正确的格式。我想我可能很接近,它在某种程度上确实有效,但不是正确的,也不是它需要的方式。只是想看看其他人可能会做什么。

【问题讨论】:

  • 它是如何“不按它需要的方式工作”的?请在您的问题中添加您获得的内容和想要获得的内容,以澄清您的意思。 ///// 我想你想看看[version] 类型而不是比较字符串。
  • 我同意@Lee_Dailey 的观点,您应该将(Get-item "Path of exe").VersionInfo.ProductVersion 的结果转换为Version 类型以作为一个整体比较-eq [Version]'16.0' 或执行[version]$prod1.Major -eq 15 -and [version]$prod1.Minor -gt 8 之类的操作
  • 我很抱歉没有更清楚。我的方法和 Abraham 发布的方法的主要问题是,它尝试为每个 exe 路径运行每个“if”语句 4 次,导致我在之后输入的代码执行不正确。我将稍后发布带有路径和产品版本的完整代码。我使用不同版本的 Visual Studio 作为 exe,并在“if”语句之后使用 Write-host 来告诉我是否找到了该产品版本。我会在一天结束之前发布完整的代码。

标签: arrays powershell loops if-statement foreach


【解决方案1】:

幸运的是,与 Python 不同,PowerShell 并不真正关心缩进。至于将脚本块的打开和关闭放置在哪里,这取决于您是与条件在同一行还是在新行。

if ($that -eq $this)
{
    # This method is quite common as it helps the reader -
    # see where the chunk of code begins, and ends.
}

if ($this -eq $that) {
    # This method is common as well
}

缩进可以大大帮助自己,因为它使代码更容易理解。一个好的规则是缩进属于前一个表达式的代码,例如:

if ($that -eq $this) {
    $Variable = CodeTo-Execute -Goes 'Here'
        if ($Variable -eq 'something') {
            'executeMe!'
        }
}

# Nothing is really stopping you from doing this either,
# as it becomes harder to read/manage:
if ($this -eq $that)
        {
            # Code here
                #  more code here
        # some code here
    }

注意:请记住,缩进本身仅适用于 powershell 与 Python 进行比较时。这些并不是您真正需要遵守的规则,而是您应该考虑养成的良好做法。

。 . .回到您的代码 sn-p,这是我将采取的一种方法:

$exePaths = @(
    "C:\Path\To\.exe1", 
    "C:\Path\To\.exe2",
    "C:\Path\To\.exe3",
    "C:\Path\To\.exe4",
    "C:\Path\To\.exe5"
)
    foreach ($Path in $exePaths)
    {
        $productVersion = (Get-Item -Path $Path).VersionInfo.ProductVersion
        if ($productVersion -eq 15.9) {
            # code here
        } 
        elseif ($productVersion -eq 16.0) {
            # code here
        }
        else {
            # code here
        }
    }

这也可以通过 Switch 语句轻松完成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多