但是我也必须为我的老板将这个首页地址分类为2个文件夹(UserToMove.txt / UserToRemove.txt)-在相同的条件下。
如果您听不懂什么,我可以再解释一次。对我来说很重要
$homeDriveRoot = "F:\UserHome"
$leaversRoot = "\new storage on NAS"
$folders = Get-ChildItem $homeDriveRoot | Select -ExpandProperty Name
foreach($folder in $folders) {
$folder
#Compare by name
$u = Get-ADUser -identity $folder -Filter {Enabled -eq $true}|Select ExpandProperty Name
#If>0
if (($u).count -gt 0) {
#If empty - remove
if(($u) -eq $null){ Copy-Object -Path "$homeDriveRoot$_" -Destination C:\Users\branym.adm\desktop\remove.csv -Force}
#If<0 write to file
else{Copy-Object -Path "$homeDriveRoot$_" -Destination C:\Users\branym.adm\Desktop\active.csv -Force};
}
#If dont search
else { echo "lost $u folder"}
}
答案 0 :(得分:0)
我认为这可能对您有帮助
$homeDriveRoot = "F:\UserHome"
$leaversRoot = "\new storage on NAS"
# create two variables for the output text files. (they will end up on your desktop)
$removeFile = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'UserToReMove.txt'
$moveFile = Join-Path -Path ([Environment]::GetFolderPath("Desktop")) -ChildPath 'UserToMove.txt'
# check if the destination folder in $leaversRoot exists. If not create it first
if (!(Test-Path -Path $leaversRoot -PathType Container)) {
New-Item -Path $leaversRoot -ItemType Directory | Out-Null
}
# get a list of all folders in the $homeDriveRoot.
# The items in the list are FolderInfo objects, not simply strings.
# If your PowerShell version is less than 3.0, write it like this:
# $folders = Get-ChildItem -Path $homeDriveRoot | Where-Object { $_.PSIsContainer }
$folders = Get-ChildItem -Path $homeDriveRoot -Directory
foreach($folder in $folders) {
# see if we can find an AD user with this SamAccountName
$user = Get-ADUser -Identity $folder.BaseName
if (!$user -or $user.Enabled -eq $false) {
# there is no active AD user found for this folder name
# test if the folder is empty or not
# by using Select-Object -First 1 the enumeration of files and/or folders stops at the first item
if ((Get-ChildItem -Path $folder.FullName -Force | Select-Object -First 1 | Measure-Object).Count -eq 0) {
# the folder is empty, so it can be deleted
# Add a line to the $removeFile
Add-Content -Path $removeFile -Value $folder.BaseName
Remove-Item -Path $folder.FullName -Force -Confirm:$false -WhatIf
}
else {
# the folder has items in it, so move it to NAS
# Add a line to the $moveFile
Add-Content -Path $moveFile -Value $folder.BaseName
Move-Item -Path $folder.FullName -Destination $leaversRoot -Force -WhatIf
}
}
}
如果结果是您期望的结果,请从-WhatIf
和Remove-Item
cmdlet中释放Move-Item
开关。
这些开关是用于测试的,实际上没有移动或移除任何东西。