Powershell:递归设置对配置文件的继承,同时删除旧的配置文件



场景:我们有漫游配置文件在工作。在我们将客户端计算机从XP升级到Win7的过程中,Win7.v2子文件夹的创建没有设置继承标志(小问题很容易解决)。除此之外,我们还有大量需要清除的旧配置文件(winxp、winxp.old、winxp_old、winexp_、win7.v2_old等)。我已经想出了一个脚本来尝试做到这一点,但我一直在删除旧的个人资料。

环境:漫游配置文件的格式如下:

  • P:\Profiles$\User1\WIN7.V2
  • P: \Profiles$\User1\winxp
  • P: \Profiles$\User1\WIN7.V2_old

我自学成才,所以很抱歉使用了垃圾脚本。我还没有使用$date变量,但希望不会删除过去10天内修改过的任何文件夹。

cls
# Date and time script is started
$StartDate = date
# Date variable for 30 day buffer
$date = (Get-Date).AddDays(-30)
# Sets path and log variables
$ProfilePath = "D:WorkProfiles"
$LogPath = "D:WorkLogs"
$Takeownlog = "$LogPathTakeown.log"
$Icaclslog = "$LogPathicacls.log"
$NoWIN7FolderLog = "$LogPathNoWin7Folder.log"
# Deletes any previous log entries
del $Takeownlog
del $Icaclslog
del $NoWIN7FolderLog
# Gets Subfolder list
$FolderList = Get-ChildItem $ProfilePath
# Main body of script. 
foreach ($SubFolder in $FolderList)
{
$winxp = "$ProfilePath$subfolderwinxp"
$winos = "$ProfilePath$subfolder%winos%"
$winvar = "$ProfilePath$subfolderwin"
# Checks if the WIN7.V2 folder exists. If it doesn't, it logs it and moves to next folder
if(-not(Test-Path -path $ProfilePath$SubFolderWIN7.V2)){
Write-Host "$SubFolderWIN7.V2 does not exist. Moving on..." -ForegroundColor Red
Write-Output "$ProfilePath$SubFolderWIN7.V2 does not exist" | Out-File  $NoWIN7FolderLog -Append -encoding default
} Else
{
# If the WIN7.V2 folder does exist it will recursively set Ownership to Administrators and then set the inheritance on the WIN7.V2 folder
Write-Host "Fixing ownership and inheritance: $ProfilePath$SubFolder" -foregroundcolor Green
Write-Output "Fixing ownership and ineritance: $ProfilePath$SubFolderWIN7.V2" | Out-File $Takeownlog -append -encoding Default
takeown /f $ProfilePath$SubFolderWIN7.V2 /A /R /D Y | Out-File $Takeownlog -append -encoding Default
Write-Output "" | Out-File $Takeownlog -append -encoding Default
#
Write-Output "" | Out-File $Icaclslog -append -encoding Default
Write-Output "Fixing inheritance: $ProfilePath$SubFolderWIN7.V2" | Out-File $Icaclslog -append -encoding Default
ICACLS $ProfilePath$SubFolderWIN7.V2 /inheritance:e /c /t | Out-File $Icaclslog -append -encoding Default
}
# Deletes any old profiles winxp or win7.v2_*
Write-Host "Removing any old profiles..."
if(Test-Path -path $winxp){
#if((-not(Test-Path -Path $winxp)) -and (-not(Test-Path -Path $winos)) -and (-not(Test-Path -Path $winvar)) {
write-host "No old profiles to delete for $SubFolder"
} Else
{
# If any old profiles are found it will delete them
Write-Host "Old profiles found for $subfolder. Deleting now..."
Remove-Item  -Path $winxp 
}
}

Write-Host ""
$EndDate = date
Write-Host "Started: $StartDate"
Write-Host "Ended:   $EndDate"
Write-Host ""

重置继承的脚本的第一部分工作正常,如下所示:

Fixing ownership and inheritance: D:WorkProfilesmcbridt 
Fixing ownership and inheritance: D:WorkProfilessinghj 
Fixing ownership and inheritance: D:WorkProfilestest1 
test2WIN7.V2 does not exist. Moving on...
Started: 04/13/2015 16:25:09 
Ended:   04/13/2015 16:25:09

但删除任何"旧"配置文件的第二部分根本不起作用。我已经尝试了很多次移除项目的迭代,但一生都无法解决。我感谢任何建议和解决方案。

非常感谢

$limit = (Get-Date).AddDays(-10)
$path = "$ProfilePath$subfolderwinxp"
# Delete files older than the $limit.
if(Test-Path -path $winxp){
Write-Host "Old profiles found for $subfolder. Deleting now..."
Get-ChildItem -Path $path -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $limit } | Remove-Item -Force
} Else{
write-host "No old profiles to delete for $SubFolder"}

在我的脚本中,使用-force删除只读文件和隐藏文件,再加上你可以在脚本末尾使用-whatif来了解如果脚本运行会发生什么

如果使用Remove-Item -Path $winxp -recurse,delete命令会更好地工作

此外,更重要的是,看起来您的逻辑在测试xp部分是向后的。

if(Test-Path -path $winxp){
write-host "No old profiles to delete for $SubFolder"
} Else{
Write-Host "Old profiles found for $subfolder. Deleting now..."
Remove-Item  -Path $winxp}

应该是…

if(Test-Path -path $winxp){
Write-Host "Old profiles found for $subfolder. Deleting now..."
Remove-Item  -Path $winxp -Recurse
}Else{
write-host "No old profiles to delete for $SubFolder"}

除此之外,如果脚本像在示例输出中那样在前面和后面命中部分,那么它看起来应该命中该部分。

非常感谢上面的两位用户,他们能够引导我朝着正确的方向前进。经过一点小小的修改,我就可以使它工作了。它不漂亮,但很管用。

cls
# Date and time script is started
$StartDate = date
# Date variable for 30 day buffer
$date = (Get-Date).AddDays(-30)
# Sets path and log variables
$ProfilePath = "<local drive><share>"
$LogPath = "C:temp"
$Takeownlog = "$LogPathTakeown.log"
$Icaclslog = "$LogPathicacls.log"
$NoWIN7FolderLog = "$LogPathNoWin7Folder.log"
# Deletes any previous log entries
del $Takeownlog -ErrorAction SilentlyContinue
del $Icaclslog -ErrorAction SilentlyContinue
del $NoWIN7FolderLog -ErrorAction SilentlyContinue
# Gets Subfolder list
$FolderList = Get-ChildItem $ProfilePath
# Main body of script. 
foreach ($SubFolder in $FolderList)
{
# Sets commonly known 'old' profile folder names
$winxpold = "$ProfilePath$SubFolderwinx*"
$winosold = "$ProfilePath$subfolder%win*"
$win7old = "$ProfilePath$subfolderWIN7.V2.*"
$win7old2 = "$ProfilePath$SubFolderWIN7.V2_*"
# Checks if the WIN7.V2 folder exists. If it doesn't, it logs it and moves to next folder
if(-not(Test-Path -path $ProfilePath$SubFolderWIN7.V2)){
Write-Host "No WIN7.V2 folders exists for: $subfolder" -ForegroundColor Red
Write-Output "No WIN7.V2 folders exists for: $subfolder" | Out-File $NoWIN7FolderLog -Append -encoding default
} Else
{
# If the WIN7.V2 folder does exist it will recursively set Ownership to Administrators and then set the inheritance on the WIN7.V2 folder
Write-Host "Fixing ownership and inheritance for: $SubFolder" -foregroundcolor Green
Write-Host "Path: $ProfilePath$SubFolder" -ForegroundColor Green
Write-Output "Fixing ownership and ineritance for: $ProfilePath$SubFolderWIN7.V2" | Out-File $Takeownlog -append -encoding Default
takeown /f $ProfilePath$SubFolderWIN7.V2 /A /R /D Y | Out-File $Takeownlog -append -encoding Default
Write-Output "" | Out-File $Takeownlog -append -encoding Default
#
Write-Output "" | Out-File $Icaclslog -append -encoding Default
Write-Output "Fixing inheritance: $ProfilePath$SubFolderWIN7.V2" | Out-File $Icaclslog -append -encoding Default
ICACLS $ProfilePath$SubFolderWIN7.V2 /inheritance:e /c /t | Out-File $Icaclslog -append -encoding Default
}
# Deletes any old profiles winxp or win7.v2_*
if((Test-Path -path $winxpold) -or 
(Test-path -Path $winosold) -or
(Test-path -Path $Win7old) -or
(Test-Path -path $win7old2)){
Write-Host "Old profiles found for: $subfolder. Deleting now..." -ForegroundColor Yellow 
Write-Output "Old profiles found for: $subfolder. Deleting now..." | Out-File $Icaclslog -append -encoding Default
Get-ChildItem -Path $ProfilePath$subfolder -Force | 
Where-Object { $_.PSIsContainer -and $_.LastWriteTime -lt $date -and $_.Name -like "winx*" -or $_.Name -like "%win*" -or $_.Name -like "WIN7.V2.*" -or $_.Name -like "WIN7.V2_*" } | 
Remove-Item -Recurse -Force
} Else{
write-host "No old profiles to delete for: $SubFolder" -ForegroundColor Green}
Write-Host ""
}
Write-Host ""
$EndDate = date
Write-Host "Started: $StartDate"
Write-Host "Ended:   $EndDate"
Write-Host ""

相关内容

  • 没有找到相关文章

最新更新