Running get-childitem on remote server to check file versions

This topic contains 8 replies, has 4 voices, and was last updated by  William Ellison 1 year, 1 month ago.

  • Author
    Posts
  • #14465

    Rob Jaudon
    Participant

    All,

    I am trying to develop a script that queries a list of server and runs the below command to get the file versions. It looks in the dir listed and searches all subdirectories for anything with *.exe and reports the file version. The command below works and I am trying to figure out how to run this against multiple servers (txt file). I know how to run get-content to access the server list but I am confused at how to query the remote servers because Get-ChildItem does not have -ComputerName.

    Can I use invoke-command? What other options do I have?


    Get-ChildItem "C:\Program Files\ccleaner\" -Include *.exe -Recurse |Select-Object -ExpandProperty VersionInfo | select-object FileDescription, OriginalFilename, FileVersion | ft -AutoSize

    OUTPUT from above command

    FileDescription OriginalFilename FileVersion
    ————— —————- ———–
    CCleaner ccleaner.exe 4, 10, 00, 4570
    CCleaner ccleaner.exe 4, 10, 00, 4570
    CCleaner Installer 2.0.0.0

  • #14466

    Don Jones
    Keymaster

    You have two options.

    Invoke-Command is one.

    The other is to map a PSDrive to the server, and then run the command locally against that drive. This will likely be far, far slower.

  • #14467

    Rob Jaudon
    Participant

    Don,

    Thanks for the help. You rock.

    I looked into this further and have this working fine. Just need help with formatting the output to include the servername.


    $servers = Get-Content -Path C:\Aventura\PowershellScripts\RodServers.txt
    ForEach ($server in $servers) { invoke-command -computername $server {Get-ChildItem "C:\Program Files\ccleaner\" -Include *.exe -Recurse |Select-Object -ExpandProperty VersionInfo | select-object FileDescription, OriginalFilename, FileVersion | ft -AutoSize}}

    I would like to have the servername included in the output. What is the easiest way to accomplish this?

    OUTPUT from above command

    FileDescription OriginalFilename FileVersion
    ————— —————- ———–
    CCleaner ccleaner.exe 4, 10, 00, 4570
    CCleaner ccleaner.exe 4, 10, 00, 4570
    CCleaner Installer 2.0.0.0

  • #14469

    Don Jones
    Keymaster

    Stop using Format-Table on the remote machine. The output would normally include the computername when it gets to your computer. Format it there. The column is PSComputerName, but it isn't added until the output hits your machine. When you have th remote machine formatting, you're preventing it from doing what you want :). Formatting is semi-evil.

    And I don't think you need the ForEach. invoke-command can accept multiple computer names and in fact will parallelize the task for you. You're actually just slowing it down.

  • #14474

    Rob Jaudon
    Participant

    Don,

    Thanks again for all the help. Much appreciated. I see the PSComputername when I remove the -ft. What is the bst option to get output to look like a table but by not using -ft?

    Sorry...Trying to learn something new here.

    FYI – Invoke-command works fine as well...and quicker.

  • #14480

    Don Jones
    Keymaster

    As I said, pipe the output of Invoke-Command to FT. That way you'll have the computer name to include.

  • #14505

    Rob Jaudon
    Participant

    I got it working. Thanks everyone for the help.

    I changed things up and now have everything outputting to a file.


    Thank you for your suggestion. It does work rather nicely.

    Here is the file code of which I piped it out to a file.


    $servers = Get-Content -Path C:\PowershellScripts\RodServers.txt
    $out = ForEach ($server in $servers)
    { invoke-command -computername $server {Get-ChildItem "C:\Program Files\ccleaner\" -Include *.exe -Recurse |
    Select-Object -ExpandProperty VersionInfo |
    select-object FileDescription, OriginalFilename, FileVersion, @{Name='ServerName';Expression={ $env:COMPUTERNAME }} |
    ft -AutoSize
    }}

    $out|Out-File C:\PowershellScripts\Versions.log

  • #33086

    Frank Bazan
    Participant

    I used this it was quick and no remoting needed.

    $results = New-Object System.Collections.ArrayList
    $Computers = Get-Content D:\servers.txt
    
    Foreach ($Computer in $Computers) 
    {
        set-location "\\$computer\c$\users"
        
            $file = Get-Item  Mandatory.v2
    
    
            $results += New-Object psObject -Property @{'Computer'=$computer;'FileName'=$file.FullName;'LastWriteTime'=$file.lastwritetime; 'Size'=($file | Get-ChildItem).count}
       
    
    
     }
        $results
    
    
    • #51307

      William Ellison
      Participant

      Or you could do it like this:

      $java = Get-Content C:\Scripts\computer.txt
      $Version = foreach( $jav in $java) {Get-ChildItem 'C:\*Program Files*\Java' -Include *.exe -Recurse}
      $Version.versionInfo | select @{Name='WSN';Expression={ $env:COMPUTERNAME }}, FileDescription, OriginalFilename, FileVersion |
      Format-Table -AutoSize

      • This reply was modified 1 year, 1 month ago by  William Ellison. Reason: I fucked up the first time

You must be logged in to reply to this topic.