简体   繁体   English

Powershell 脚本获取完整路径并获取文件版本

[英]Powershell script to get full path and get file version

My end goal here is to get all exe that are lower than version 1.0.0.0 and update those folders with new updated files by copying them to it.我的最终目标是获取低于 1.0.0.0 版本的所有 exe,并通过将这些文件夹复制到这些文件夹中来更新这些文件夹。

there are multiple destinations where they reside, namely our terminal servers.他们驻留有多个目的地,即我们的终端服务器。 lets use the below example.让我们使用下面的示例。

$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\"

There are 10 - 15 different folders in "Software" and each folder has the same exe name "Software.exe" but they are different for each client and not all clients are running the same version. “Software”中有 10 - 15 个不同的文件夹,每个文件夹都有相同的 exe 名称“Software.exe”,但它们对于每个客户端都不同,并且并非所有客户端都运行相同的版本。 We have an update for all clients that are running version 1.0.0.0 and we need to update them to version 1.0.0.1我们为所有运行版本 1.0.0.0 的客户端提供了更新,我们需要将它们更新到版本 1.0.0.1

Anyone running v 0.0.9.0 should not be updated.不应更新运行 v 0.0.9.0 的任何人。

Any ideas how i go about this?关于这个我有什么想法吗?

Right now i can get a list of all exe's inside the folder but i cannot use the write-host somehow to read the file version of each现在我可以获得文件夹内所有 exe 的列表,但我不能以某种方式使用写主机来读取每个文件的文件版本

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

This give me the following output:这给了我以下 output:

\\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

How should I proceed ahead.我该如何继续前进。

Provided you have permissions to do so and the Software.exe is not currently running, you should be able to do that with below code:如果您有权这样做并且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
        }
}

You can also opt for not having the code try and replace the file, but simply return a list of updatable paths.您也可以选择不让代码尝试替换文件,而只需返回可更新路径的列表。 In that case, do:在这种情况下,请执行以下操作:

# 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'

If you only want a listing of all 'Software.exe' files together with their version numbers, use:如果您只想列出所有“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

Besides ProductVersionRaw , an exe file will also have a FileVersionRaw property.除了ProductVersionRaw之外,exe 文件还将具有FileVersionRaw属性。 Both are [version] objects and they are not necessarily the same, so make sure you are using the correct verion.两者都是[version]对象,它们不一定相同,因此请确保您使用的是正确的版本。

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

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