Проблема с вашим кодом связана с использованием оператора -contains
. Вот страница с документацией. Вот соответствующий пример:
PS> "abc", "def", "ghi" -contains "abc", "def"
False
Вместо этого вы можете дважды использовать -contains
с логическим -and
или командлетом Compare-Object
.
Возможные решения:
$RootFolder = 'C:\example\'
$EditFile = 'file1.txt'
$DependentFile = 'file2.txt'
# Recursively get all folders
$Folders = Get-ChildItem -Recurse -Directory $RootFolder
Использование -and
# Check each folder for the two files
foreach ($Folder in $Folders) {
# Get names of each file in folder
$FolderContents = Get-ChildItem $Folder.fullname | Select-Object -ExpandProperty name
# Check that the folder has file1 AND file2
if ($FolderContents -contains $EditFile -and $FolderContents -contains $DependentFile) {
}
}
Использование Compare-Object
# Check each folder for the two files
foreach ($Folder in $Folders) {
# Get names of each file in folder
$FolderContents = Get-ChildItem $Folder.fullname | Select-Object -ExpandProperty name
# Compare the filenames in folder to an array of with two filenames, validate no extra elements found in second array
if ((Compare-Object $FolderContents @($EditFile,$DependentFile)).SideIndicator -notcontains '=>') {
}
}