Catalog (name,path,version) files via PowerShell

301 Views Asked by At

I am trying to catalog a set of files over multiple remote servers. Setting up a mapped drive is not an option at this time. Furthermore, I am not a hundred percent sure all drives available for mapping. I am only interested in just logical drives, no network or usb or anything of the like. I think I mostly have it, but being new to PowerShell I am not sure how to implement my current working script to loop through a set of remote servers.

My current working script is as follows and works great if local to the system:

$ErrorActionPreference = "SilentlyContinue"
Set-ExecutionPolicy RemoteSigned
Get-WMIObject Win32_LogicalDisk -filter "DriveType = 3" | 
Select-Object DeviceID | 
ForEach-Object {Get-Childitem ($_.DeviceID + "\") -include file1.txt, file2.txt, file3.txt -recurse} | 
./get-fileversion.ps1

I have found several references on how to connect to remote systems like the following: get-FileVersion Source

where it uses a file to list the server (easy to understand) but I also need to pass credentials to each server and would like to execute the script above on each server.

Can anyone help me figure this out?

Thank you in advance!

get-fileversion.ps1 source:

found at http://jtruher3.wordpress.com/2006/05/14/powershell-and-file-version-information/ on 2014-11-11

param ( [string[]]$paths )
begin {
    # I want to do some stuff with relative paths.   
    # create a variable that I can use later
    $P = [string](get-location)

    # the workhorse of the script
    function GetVersionInfo
    {
        param ( [string]$path )
        # resolve the path, we're going to need a fully qualified path to hand
        # to the method, so go get it.  I may not have that depending on how
        # was called
        $rpath = resolve-path $path 2>$null
        # the thing we hand to the method is the path string, so we'll tuck that away
        $path = $rpath.path
        # check to be sure that we're in the filesystem
        if ( $rpath.provider.name -ne "FileSystem" ) 
        { 
            "$path is not in the filesystem"
            return $null
        }
        # now that I've determined that I'm in the filesystem, go get the fileversion
        $o = [system.diagnostics.fileversioninfo]::GetVersionInfo($path)
        # this little dance adds a new property to the versioninfo object
        # I add the relative path to the versioninfo so I can inspect that in the output object
        # the way that add-member works is to not emit the object, so I need to 
        # use the -passthrough parameter
        $o|add-member noteproperty RelativePath ($path.replace($P,".")) -pass
    }
    # whoops! something bad happened
    function ShowFileError
    {
        param ( [string]$path )
        if ( test-path $path -pathtype container )
        {
            "$path is a container"
        }
        else
        {
            "$path not found"
        }
    }
}

# data could have been piped - check $_ to see if this cmdlet had data
# piped to it
process {
    if ( $_ )
    {
        # make sure that I'm not trying to get a versioninfo of a directory
        if ( test-path $_ -pathtype leaf )
        {
            GetVersionInfo $_
        }
        else
        {
            ShowFileError $_
        }
    }
}

# we could have also gotten arguments on the command line
end {
    if ( $paths )
    {
        # by calling resolve path first, I can deal with wildcards on the command line
        foreach ( $path in resolve-path $paths )
        {
            # make sure it's a file, not a directory
            if ( test-path $path -pathtype leaf )
            {
                GetVersionInfo $path
            }
            else
            {
                ShowFileError $path
            }
        }
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Assuming you have a script called Get-Info.ps1 with this content:

$Report = @{}

# check free disk space
$Report.Disks = Get-PSDrive -PSProvider FileSystem | where Free | select Name, Used, Free
# this is just an example, collect whatever info you want from each machine
# and add it to the $Report object:
# $Report.MoreInfo = 'Foo'

[pscustomobject] $Report

And a list of target computers in computers.txt which all have a common account with permissions, you can run

Invoke-Command "Get-Info.ps1" -ComputerName (gc computers.txt) -Credential (Get-Credential) -AsJob | Wait-Job | Receive-Job