I have the need to search all servers in our domain for specific folders, I already have a script here
function Find-ComputersWithFolder {
# Get all computers in the Active Directory domain
$computers = Get-ADComputer -Filter {name -like 'servernamehere' }
# Initialize an empty array to store the result
$result = @()
# Iterate through each computer
foreach ($computer in $computers) {
$computerName = $computer.Name
# Check if the folder C:\ASNEXT exists on the computer
$folderExists = Test-Path "\\$computerName\C$\Windows\WinSxS\foldernamehere"
# If the folder exists, add the computer name to the result list
if ($folderExists) {
$result += $computerName
}
}
# Return the result array
return $result
}
# Usage example for the Find-ComputersWithFolder function
# Find all computers with the folder
$computersWithFolder = Find-ComputersWithFolder
# Output the result
Write-Output "Computers with folder"
$computersWithFolder
what this does is searches all servers in the domain and looks for the folder that is specified in the line
$folderExists = Test-Path "\\$computerName\C$\Windows\WinSxS\foldernamehere"
However i have a long list of folders i would like to supply to this script so that it went through them instead of me having to manually specify the folder on the specific line
$folderExists = Test-Path "\\$computerName\C$\Windows\WinSxS\foldernamehere"
I tried the following amendment to the script, i put all the folder names in a text file and created a variable $Paths to which i used the get-content.
$paths = get-content C:\scripts\paths.txt
This works in that it fills the variable with the list of folders but when i change the line that actually searches to this
$folderExists = Test-Path "\\$computerName\C$\Windows\WinSxS\$paths"
I dont get any output, I suspect its is something to do with having to somehow get it to do a for-each through the list but i dont know how to get to that point.
Any help would be much appreciated
This is correct, you're missing an inner loop to test each path in
$pathsfor each computer in$computers, however, what I would recommend you to do is to remove complexity from your function, the query for Active Directory computers should be outside of your function, then you can leverage the pipeline by takingValueFromPipelineByPropertyName:In this example the
.Nameproperty from eachADComputerobject is bound thru the pipeline to the$ComputerNameparameter.