简体   繁体   English

将名称和文件版本保存到 csv (Powershell)

[英]Save names and file-versions to csv (Powershell)

I have folders with some.exe files, where i want to get the names and fileversions.我有一些包含一些.exe 文件的文件夹,我想在其中获取名称和文件版本。 I can get these, but i have problems to save them in a nice.csv format.我可以得到这些,但我无法将它们保存为 nice.csv 格式。

Now I have:我现在有:

n1.exe

18.4.0.0

n2.exe

18.4.15.1

n3.exe

18.4.15.1

n4.exe

18.4.15.1

But i want to save it as:但我想将其另存为:

n1.exe;18.4.0.0;

n2.exe;18.4.15.1;

n3.exe;18.4.15.1;

n4.exe;18.4.15.1;

Here is my code.这是我的代码。 I have no clue how to get it done with the semicolons我不知道如何用分号完成它

$folder1="D:\Service\Test";

$exeNames = Get-ChildItem -Filter *.exe -Path $folder1 -Name

#Programm Versionen
function efaFileVersionInfo ($efaprograms, $folder) {
    foreach ($efaprogram in $efaprograms) {
        echo $efaprogram
        [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$folder\$efaprogram").FileVersion
    }
}


efaFileVersionInfo $exeNames $folder1 | Out-File D:\Service\folder1.csv

So - using a build-in PowerShell functions you can achieve it by using something like this:所以 - 使用内置的 PowerShell 函数,您可以使用以下方法来实现它:

$folder1 = "D:\Service\Test";
$list = [System.Collections.Generic.List[PSObject]]@();
$exeNames = Get-ChildItem -Filter *.exe -Path $folder1 -Name;

#Programm Versionen
function efaFileVersionInfo ($efaprograms, $folder) {
    foreach ($efaprogram in $efaprograms) {
        $version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("$folder\$efaprogram").FileVersion;
        if (!([string]::IsNullOrEmpty($version))) { 
            $version = $version.Trim();
        }
        $obj = [PSCustomObject]@{
            FileName = $efaprogram
            FileVersion = $version
        }
        $list.Add($obj);
    }
}

efaFileVersionInfo $exeNames $folder1;
$list | Export-Csv -Path C:\Temp\asd.csv -NoTypeInformation -Delimiter ";";

This presents data in this way:这以这种方式呈现数据:

"FileName";"FileVersion"
"7z1900-x64.exe";"19.00"
"azuredatastudio-windows-user-setup-1.27.0.exe";"1.27.0"
"Docker Desktop Installer.exe";"3.2.2.61853"
"Git-2.31.1-64-bit.exe";"2.31.1.1"
"GitHubDesktopSetup.exe";"2.7.2"
"Greenshot-INSTALLER-1.2.10.6-RELEASE.exe";"1.2.10.6"
"ideaIC-2021.1.1.exe";
"kubectl.exe";

If it's a CSV you want, you need to have your function output objects instead of plain text.如果它是你想要的 CSV,你需要有你的 function output对象而不是纯文本。

Something like就像是

$folder1  = "D:\Service\Test"
$exeNames = Get-ChildItem -Path $folder1 -Filter '*.exe' -File

# Programm Versionen
function Get-efaFileVersionInfo ($efaprograms, $folder) {
    foreach ($efaprogram in $efaprograms) {
        # output an object
        [PsCustomObject]@{
            Name    = $efaprogram.Name
            Version = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($efaprogram.FullName).FileVersion
        }
    }
}

# export to CSV file
Get-efaFileVersionInfo $exeNames $folder1 | Export-Csv -Path 'D:\Service\folder1.csv' -Delimiter ';' -NoTypeInformation

Note: I have changed the function name so it complies with the default PowerShell Verb-Noun convention注意:我更改了 function 名称,使其符合默认的 PowerShell Verb-Noun约定

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

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