pixel0815
Goto Top

PS Skript - Servergespeicherte Profile löschen nach alter

Hallo zusammen,

ich bin auf der Suche nach einem kleinen Skript das auf deinem Profilserver alte nicht benutzte Benutzerprofile löscht.
Habe das hier gefunden und im Grundprinzip macht es wohl das was ich gerne möchte. Besitz übernehmen, Rechte korrigieren und löschen wenn es Anzahl x Tage alt ist.

Kann man das ohne großen Aufwand "umschreiben" das es lediglich auf einen Pfad bezieht, worin sich die Profile befinden?
Die Rechte auf das Profil soll nur der Administrator bekommen. Unbenutzte Profile ( älter wie 30 Tage oder mehr ) sollen gelöscht werden. Alle anderen Profile die nicht in das Suchmuster passen sollen natürlich ignoriert werden.

Leider bietet Delprof2 ja noch nicht so eine Funktion face-sad


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 = "$LogPath\Takeown.log"  
$Icaclslog = "$LogPath\icacls.log"  
$NoWIN7FolderLog = "$LogPath\NoWin7Folder.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\$SubFolder\winx*"  
$winosold = "$ProfilePath\$subfolder\%win*"  
$win7old = "$ProfilePath\$subfolder\WIN7.V2.*"  
$win7old2 = "$ProfilePath\$SubFolder\WIN7.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\$SubFolder\WIN7.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\$SubFolder\WIN7.V2" | Out-File $Takeownlog -append -encoding Default  
        takeown /f $ProfilePath\$SubFolder\WIN7.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\$SubFolder\WIN7.V2" | Out-File $Icaclslog -append -encoding Default  
        ICACLS $ProfilePath\$SubFolder\WIN7.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"  

Gruß

Content-Key: 288905

Url: https://administrator.de/contentid/288905

Printed on: April 26, 2024 at 05:04 o'clock