powershell - 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
我该如何继续前进。
解决方案
如果您有权这样做并且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]
对象,它们不一定相同,因此请确保您使用的是正确的版本。
推荐阅读
- unity3d - 未检测到统一碰撞
- node.js - 如何修复 octokit.authenticate() 已弃用
- php - 如何使用 PHP 格式化包含数据库中时间的变量?
- r - ggplot2 热图没有均匀地分布在背景主题上
- swift - Swift 浮点计算不同于其他语言
- python - 如何在异步 Python 函数中指定返回类型?
- spring - Spring Boot 测试:等待微服务调度器任务
- javascript - 如何在 javascript/Ajax 中添加重试
- node.js - 使用 mongoDB 客户端连接 mongoDB 时遇到问题
- c# - 使用 Dapper 映射多个复杂类型属性