Jan 26 2022 06:34 AM
I've created a simple script to search for a folder (study) in a list of locations. This has been working ok during testing, but I ocassionally encounter a issue where the output doesn't output the path. For example, it only outputs "Study 12345 found. Location - ", rather than "Study 12345 found. Location - @{FullName=\\file-server\e$\folder1\folder2\folder3\folder4\folder5\12345". This also occurs when searching locations locally on the Server.
I can see the folder does exist and it doesn't just happen for one location. I'm puzzled by this and unsure where to start troubleshooting.
Any suggestions?
# Prompt user for study number
$study = Read-Host -Prompt 'Enter the study number to search for...'
# Create variable with value containing a list of study locations.
$study_paths = Get-Content -Path 'E:\pshell-testing\locations2.txt'
# Define recursion depth
$rec_depth = 3
# Begin searching
foreach ($study_path in $study_paths)
{
$study_dirs = Get-ChildItem -Directory -Force -ErrorAction SilentlyContinue -Recurse -Depth $rec_depth -Path $study_path | Where-Object {$_.Name -match $study} | Select-Object -Property FullName
if (!$study_dirs)
{
Write-Host -ForegroundColor Yellow "No studies found in $study_path"
}
elseif ($study_dirs)
{
Write-Host -ForegroundColor Green "Study $study found. Location - $study_dirs"
}
}
Jan 26 2022 10:41 AM
Solution
You have to enclose the variable and use .fullname behind it, you can see the options if you enter $study_dirs after you run the script
# Prompt user for study number
$study = Read-Host -Prompt 'Enter the study number to search for...'
# Create variable with value containing a list of study locations.
$study_paths = Get-Content -Path 'E:\pshell-testing\locations2.txt'
# Define recursion depth
$rec_depth = 3
# Begin searching
foreach ($study_path in $study_paths)
{
$study_dirs = Get-ChildItem -Directory -Force -ErrorAction SilentlyContinue -Recurse -Depth $rec_depth -Path $study_path | Where-Object {$_.Name -match $study} | Select-Object -Property FullName
if (!$study_dirs)
{
Write-Host -ForegroundColor Yellow "No studies found in $study_path"
}
elseif ($study_dirs)
{
Write-Host -ForegroundColor Green "Study $study found. Location - $($study_dirs.Fullname)"
}
}
Jan 27 2022 02:23 AM
Jan 27 2022 02:31 AM