首页 > 解决方案 > Powershell脚本获取完整路径并获取文件版本

问题描述

我的最终目标是获取低于 1.0.0.0 版本的所有 exe,并通过将这些文件夹复制到这些文件夹中来更新这些文件夹。

他们驻留有多个目的地,即我们的终端服务器。让我们使用下面的示例。

$RDP1Path = "\\RDP01\C$\Program Files (x86)\Software\"
$RDP2Path = "\\RDP02\C$\Program Files (x86)\Software\"
$RDP3Path = "\\RDP03\C$\Program Files (x86)\Software\"
$RDP4Path = "\\RDP04\C$\Program Files (x86)\Software\"

“Software”中有 10 - 15 个不同的文件夹,每个文件夹都有相同的 exe 名称“Software.exe”,但它们对于每个客户端都不同,并且并非所有客户端都运行相同的版本。我们为所有运行版本 1.0.0.0 的客户端提供了更新,我们需要将它们更新到版本 1.0.0.1

不应更新运行 v 0.0.9.0 的任何人。

有什么想法我该怎么做?

现在我可以获得文件夹内所有 exe 的列表,但我不能以某种方式使用写主机来读取每个文件的文件版本

$GetFullPaths = get-childitem $RDP1Path-Recurse | where {$_.Name -eq "Software.exe"} | % {Write-Host $_.FullName}

这给了我以下输出:

\\RDP01\C$\Program Files (x86)\Software\Client1\Software.exe
\\RDP01\C$\Program Files (x86)\Software\Client2\Software.exe
\\RDP01\C$\Program Files (x86)\Software\Client3\Software.exe
\\RDP01\C$\Program Files (x86)\Software\Client4\Software.exe
\\RDP01\C$\Program Files (x86)\Software\Client5\Software.exe

我该如何继续前进。

标签: powershell

解决方案


如果您有权这样做并且Software.exe当前未运行,您应该能够使用以下代码执行此操作:

$serverNames     = 'RDP01','RDP02','RDP03','RDP04'   # an array of server names
$newSoftwareFile = 'D:\SoftwareNew\Software.exe'     # the full path and filename to the new version
$versionToUpdate = [version]'1.0.0.0'                # the version of the exe to be replaced

# loop through the servers list, find the exe with productversion 1.0.0.0
# and when found, copy the new file to that directory.
foreach($server in $serverNames) {
    $path = '\\{0}\C$\Program Files (x86)\Software' -f $server
    Get-ChildItem -Path $path -Filter 'Software.exe' -Recurse -File |
        Where-Object { $_.VersionInfo.ProductVersionRaw -eq $versionToUpdate } |
        ForEach-Object { 
            Write-Host "Updating '$($_.FullName)'"
            # you may need to first stop the process if software.exe is currently running
            # and after copying the new version start it up again...
            Copy-Item -Path $newSoftwareFile -Destination $_.DirectoryName -Force
        }
}

您也可以选择不让代码尝试替换文件,而只需返回可更新路径的列表。在这种情况下,请执行以下操作:

# loop through the servers list, find the exe with productversion 1.0.0.0
# and when found, output the file path and capture in variable $result
$result = foreach($server in $serverNames) {
    $path = '\\{0}\C$\Program Files (x86)\Software' -f $server
    Get-ChildItem -Path $path -Filter 'Software.exe' -Recurse -File |
        Where-Object { $_.VersionInfo.ProductVersionRaw -eq $versionToUpdate } |
        ForEach-Object { $_.FullName }
}

# output on screen
$result

# or write to text file
$result | Set-Content -Path 'D:\softwareToUpdate.txt'

如果您只想列出所有“Software.exe”文件及其版本号,请使用:

# loop through the servers list, find the exe and return FullName and versions
$result = foreach($server in $serverNames) {
    $path = '\\{0}\C$\Program Files (x86)\Software' -f $server
    Get-ChildItem -Path $path -Recurse -File |
    Select-Object @{Name = 'Path'; Expression = {$_.FullName}},
                  @{Name = 'ProductVersion'; Expression = {$_.VersionInfo.ProductVersionRaw}},
                  @{Name = 'FileVersion'; Expression = {$_.VersionInfo.FileVersionRaw}}
}

# output on screen
$result | Format-Table -AutoSize

# or write to csv file
$result |Export-Csv -Path 'D:\softwareVersions.csv' -NoTypeInformation

此外ProductVersionRaw,一个 exe 文件也会有一个FileVersionRaw属性。两者都是[version]对象,它们不一定相同,因此请确保您使用的是正确的版本。


推荐阅读