0

タスクは些細なようです!私たちの環境には多くのユーザーが行き来しています。時々、「孤立した」ローミング プロファイル フォルダをチェックして、それらを取り除きたいと思います。私のコードは、ユーザー名がフォルダー名と正確に一致する場合に機能しますが、Windows が .V2、.V3 などを追加したフォルダーには一致しません。ワイルドカードを挿入します。where-object 構築を試みましたが、同様に失敗しました。

たとえば、bob.smith というユーザーがいる場合、「孤立した」リストに bob.smith または bob.smith.V2 という名前のプロファイル フォルダーを含めないようにします。

これが私のコードです:

# Put profile folder names in array

$profilefolders = Get-ChildItem -Name "\\server\profiles$"



# Initialize arrays 

 $orphanprofiles = @()



foreach ($userprofile in $profilefolders)
    {
#  Do profile names match up with user accounts?
    If(Get-ADUser -Filter {SamAccountName -eq $userprofile})

        {
        # User account exists: no action
        }

    Else
        {
        # Profile is an orphan. Add to list.
        $orphanprofiles += $userprofile
                         }
                }




# put array values in a string variable with newline char for screen   output
$outputprofiles = $orphanprofiles -join "`n"

Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
4

2 に答える 2

1

最初の行を次のように変更します。

$profilefolders = Get-ChildItem -Name "\\server\profiles$"|where {$_ -notmach '.*\.V\d$'}

より良い解決策を思いつきました。正規表現を使用してフォルダー名から拡張子を分割するため、その.V2拡張子のない名前をチェックします。中間の $profilefolders は必要ありません。

# Initialize arrays 
$orphanprofiles = @()
$pattern = '^(.*?)(\.V\d)$'

Get-ChildItem -Name "\\server\profiles$"| 
    ForEach {
        If ($_ -match $pattern) {
            If (!(Get-ADUser -Filter {SamAccountName -eq $matches[1]}))
                {$orphanprofiles += $_} else {}
        } Else {
            If (!(Get-ADUser -Filter {SamAccountName -eq $_}))
                {$orphanprofiles += $_}
        }
    }


# put array values in a string variable with newline char for screen   output
$outputprofiles = $orphanprofiles -join "`n"
Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
于 2016-12-20T23:23:55.670 に答える
0

@LotPings に感謝します。これが私のコードです(すべてのデバッグステートメントを含む):

# Initialize array
$orphanprofiles = @()

# set regex pattern
$pattern = '^(.*?)(\.V\d)$'

Get-ChildItem -Name "\\server\profiles$"| 
    ForEach {
        Write-Host "Testfolder: " $_
        If ($_ -match $pattern) 
        {
          Write-Host "Loop 1 Foldername " $_
          Write-Host "Loop 1 Match " $matches[1]
          $Vnuser = $matches[1]

            If(Get-ADUser -Filter {SamAccountName -eq $Vnuser})
                {
             # match: do nothing
                }
               Else 
                {
                $orphanprofiles += $_
                Write-Host "Loop one inside If statement: " $_
                }


     }

      Else
        {
           Write-Host "Folders without .Vn: " $_
           If(Get-ADUser -Filter {SamAccountName -eq $_})
                {
                # match: do nothing
                }
            Else
                {
                $orphanprofiles += $_
                Write-Host "Loop 2 foldername: " $_
                }

   } 

   }



# put array values in a string variable with newline char for screen output
$outputprofiles = $orphanprofiles -join "`n"
Write-Output "`n"
Write-Output "Orphan profiles: `n$outputprofiles"
Write-Output "  "
于 2016-12-23T16:06:58.820 に答える