Powershell-在foreach循环中排序和选择文件 [英] Powershell - Sorting and selecting files in foreach loop

查看:289
本文介绍了Powershell-在foreach循环中排序和选择文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个脚本,该脚本将从服务器目录中多个安装位置中找到最新的build_info文件,从每个文件中选择"version:"文本,然后进行比较以查看它们是否全部相同(这是我们希望的),或者某些安装位置的版本不同.另外,让每个路径的安装版本都有其自己的变量也将是一件很不错的事情,这样,如果我必须输出任何差异,就可以说出哪些特定路径具有哪些版本.例如,如果在Path1,Path2和Path3中安装了某些软件,那么我想说:所有路径都在3.5版上,或者" Path1是1.2版,Path2是3.5版,Path3是4.8版."

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. 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."

以下是我想要做的更整洁的清单:

Here's a neater list of what I'm trying to do:

  1. 浏览目录中的文件夹.
  2. 对于每个文件夹,按创建日期降序对在该路径中具有特定名称的txt文件进行排序,然后选择最新的文件.
  3. 一旦它具有来自每个路径的最新文件,请从每个文件中选择字符串作为特定短语.具体来说,是"version:".
  4. 比较每个路径的版本,看是否相同或不同,然后输出结果.

这是我到目前为止能够写的:

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.';
}

我不得不在最新的build_info文件中进行硬编码,因为我不确定如何将排序方面纳入其中.我也不确定如何有效地将每个路径的结果分配给变量,并在存在差异时将其输出.就排序方面而言,这就是我一直在搞的问题,但是我不知道如何将其合并,我甚至不确定这是否是解决此问题的正确方法:

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. 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
}

这篇关于Powershell-在foreach循环中排序和选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆