PowerShell 脚本 - 检查多台 PC 的文件是否存在,然后获取该文件版本

dle*_*ley 3 powershell

我的 PowerShell 技能还处于起步阶段,所以请耐心等待。我需要做的是从文本文件中获取 PC 列表并检查文件是否存在。一旦确定,我需要使用那些拥有该文件的 PC 并检查该文件的 FileVersion。最后,将其输出到 CSV 文件。

这是我所拥有的,我不确定这是否是我应该做的:

ForEach ($system in (Get-Content C:\scripts\systems.txt))

if  ($exists in (Test-Path \\$system\c$\Windows\System32\file.dll))
{
    Get-Command $exists | fl Path,FileVersion | Out-File c:\scripts\results.csv -Append
}   
Run Code Online (Sandbox Code Playgroud)

von*_*ryz 5

对于入门脚本来说还不错,您几乎是对的。让我们稍微修改一下。要获取版本信息,我们只需从另一个 an answer获取工作代码。

ForEach ($system in (Get-Content C:\scripts\systems.txt)) {
    # It's easier to have file path in a variable
    $dll = "\\$system\c`$\Windows\System32\file.dll"

    # Is the DLL there?    
    if  ( Test-Path  $dll){
        # Yup, get the version info
        $ver = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($dll).FileVersion
        # Write file path and version into a file.  
        Add-Content -path c:\scripts\results.csv "$dll,$ver"
    }
}
Run Code Online (Sandbox Code Playgroud)