简体   繁体   English

Powershell-在foreach循环中排序和选择文件

[英]Powershell - Sorting and selecting files in foreach loop

I'm trying to create a script that will find the most recent build_info files from multiple install locations in a server's directory, select the "version: " text from each file, and compare them to see if they're all the same (which is what we hope for), or if certain install locations have different versions. 我正在尝试创建一个脚本,该脚本将在服务器目录中的多个安装位置中找到最新的build_info文件,从每个文件中选择“ version:”文本,然后进行比较以查看它们是否相同(是我们希望的),或者某些安装位置的版本不同。 As a bonus, it would also be nice to have each path's install version have its own variable so that if I have to output any differences, I can say which specific paths have which versions. 另外,让每个路径的安装版本都有其自己的变量也将是一件很不错的事情,这样,如果我必须输出任何差异,就可以说出哪些特定路径具有哪些版本。 For example, if something is installed in Path1, Path2, and Path3, I want to be able to say, "all paths are on version 3.5," or "Path1 is version 1.2, Path2 is version 3.5, Path3 is version 4.8." 例如,如果在Path1,Path2和Path3中安装了某些软件,那么我想说:“所有路径都在3.5版上,或者” Path1是1.2版,Path2是3.5版,Path3是4.8版。”

Here's a neater list of what I'm trying to do: 这是我想要做的更整洁的清单:

  1. Loop through folders in a directory. 循环浏览目录中的文件夹。
  2. For each folder, sort the txt files with a specific name in that path by Creation Date descending and select the most recent. 对于每个文件夹,请按创建日期降序对在该路径中具有特定名称的txt文件进行排序,然后选择最新的。
  3. Once it has the most recent files from each path, Select-String a specific phrase from each of them. 一旦它具有来自每个路径的最新文件,请从每个文件中选择字符串作为特定短语。 Specifically, "version: ". 具体来说,是“ version:”。
  4. Compare the version from each path and see if all are the same or there are differences, then output the result. 比较每个路径的版本,看是否相同或不同,然后输出结果。

This is what I've been able to write so far: 到目前为止,这是我能够写的:

$Directory = dir D:\Directory\Path* | ?{$_.PSISContainer};
$Version = @();

foreach ($d in $Directory) {
    $Version = (Select-String -Path D:\Directory\Path*\build_info_v12.txt -Pattern "Version: " | Select-Object -ExpandProperty Line) -replace "Version: ";
}

if (@($Version | Select -Unique).Count -eq 1) {
    Write-Host 'The middle tiers are all on version' ($Version | Select -Unique);
}
else {
    Write-Host 'One or more middle tiers has a different version.';
}

I've had to hard code in the most recent build_info files because I'm not sure how to incorporate the sorting aspect into this. 我不得不在最新的build_info文件中进行硬编码,因为我不确定如何将排序方面纳入其中。 I'm also not sure how to effectively assign each path's result to a variable and output them if there are differences. 我也不确定如何有效地将每个路径的结果分配给变量并在存在差异时将其输出。 This is what I've been messing around with as far as the sorting aspect, but I don't know how to incorporate it and I'm not even sure if it's the right way to approach this: 就排序方面而言,这就是我一直在搞的,但我不知道如何将其合并,我甚至不确定这是否是解决此问题的正确方法:

$Recent = Get-ChildItem -Path D:\Directory\Path*\build_info*.txt | Sort-Object CreationTime -Descending | Select-Object -Index 0;

You can use Sort-Object and Select-Object to determine the most recent file. 您可以使用“排序对象”和“选择对象”来确定最新文件。 Here is a function that you can give a collection of files to and it will return the most recent one: 这是一个您可以提供文件集合的函数,它将返回最新的文件:

function Get-MostRecentFile{
    param(
        $fileList
    )
    $mostRecent = $fileList | Sort-Object LastWriteTime | Select-Object -Last 1
    $mostRecent
}

Here is one possible solution: 这是一种可能的解决方案:

Get-ChildItem "D:\Directory\Path" -Include "build_info*.txt" -File -Recurse |
    Group-Object -Property DirectoryName |
        ForEach-Object {
            $_.Group |
                Sort-Object LastWriteTime -Descending |
                    Select-Object -First 1 |
                        ForEach-Object {
                            New-Object -TypeName PsCustomObject |
                                Add-Member -MemberType NoteProperty -Name Directory -Value $_.DirectoryName -PassThru |
                                Add-Member -MemberType NoteProperty -Name FileName -Value $_.Name -PassThru |
                                Add-Member -MemberType NoteProperty -Name MaxVersion -Value ((Select-String -Path $_.FullName -Pattern "Version: ").Line.Replace("Version: ","")) -PassThru 
                        }
    }

This will produce a collection of objects, one for each directory in the tree, with properties for the directory name, most recent version and the file we found the version number in. You can pipe these to further cmdlets for filtering, etc. 这将产生一组对象,每个对象用于树中的每个目录,并具有目录名称,最新版本和我们在其中找到版本号的文件的属性。您可以将它们传送到其他cmdlet进行过滤,等等。

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

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