Score:0

Find Directory of Target File on Remote Server

cn flag

I am trying to find a file directory on a list of remote servers. I want to:

  1. Use Powershell; get targets from comma delimited text file, i.e. server1,file1 Note: file is different for each server.
  2. Search the C: and E: partitions for a file, even if more than one instance of it
  3. Get the file directory
  4. Write the result to a .csv
  5. View the output on the Powershell main window

Here is the script I have been working with. I cannot get it to work properly. I need some help:

$scriptpath = $MyInvocation.MyCommand.Path 
$dir = Split-Path $scriptpath
$FileName = "$dir\Servers.txt"
$Data = gc $FileName

foreach ($Row in $Data)
    {
    $Position = $Row.IndexOf(",")
    $Server = $Row.Substring(0, $Position)
    $File = $Row.Substring($Position + 1)

    $CPart = @{
       Name = "CPart"
       Path = "\\$Server\C$"
       #Path = "FileSystem::\\$Server\C$"
    }

    $EPart = @{
       Name = "EPart"
       Path = "\\$Server\E$"
       #Path = "FileSystem::\\$Server\E$"
    }

    @($CPart, $EPart) | ForEach-Object {
        if ($Server -like "*.mydomain.com") {$session = New-PSSession -Computer $Server -Credential MYDOMAIN\userid -ErrorAction Stop}
        else {$Session = New-PSSession -Computer $Server -ErrorAction Stop}
        Invoke-Command -Session $Session -ScriptBlock {$Result = Get-ChildItem -Recurse -Path $_.Path | Where-Object {$_.Name -match '$File'};return $Result}
        $Result
        #$item = Get-ChildItem -Recurse -Path $_.Path | Where-Object {$_.Name -match '$File' -and }
        #Write-Host $item
    }
}
    
<#foreach ($Server in $Servers) 
    {
    "\\$Server\C$" | Get-ChildItem -recurse -filter $FileList | Select Directory, Name | Write-Host $_.Directory + "\" + $_.Name
    "\\$Server\E$" | Get-ChildItem -recurse -filter $FileList | Select Directory, Name | Write-Host $_.Directory + "\" + $_.Name
}

$columnToGet = 0
$columns = gc $fileName | 
   %{ $_.Split(",")[$columnToGet] }
$columns
#>
br flag
take a look at the `CIM_DataFile` class. you can use CIM or WMI to call it with the target system, drive, and file ... and it will return info on what it finds.
cn flag
Thank you for this suggestion. I read up on both (https://powershell.one/wmi/root/cimv2/cim_datafile) and it seems they are prohibitive on remote, recursive searches for files because WMI first collects the entire contents of a partition before it performs any filtering (finding). I am really looking for someone to help me use get-childitem recursively on a remote server partition hopefully in a way that will not take all day long.
br flag
the `Get-ChildItem` cmdlet will also need to read thru the entire volume to find any files _if you don't know the dir tree to limit the search to_. ///// the robocopy command with the "list only" option would be fairly fast ... and - like the G-CI cmdlet - it can be run with `Invoke-Command` to run things in parallel on each target system. then it would send the final result back to the calling system.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.