I am trying to find a file directory on a list of remote servers. I want to:
- Use Powershell; get targets from comma delimited text file, i.e. server1,file1
Note: file is different for each server.
- Search the C: and E: partitions for a file, even if more than one instance of it
- Get the file directory
- Write the result to a .csv
- 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
#>