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
Gruß
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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ß
Bitte markiere auch die Kommentare, die zur Lösung des Beitrags beigetragen haben
Content-ID: 288905
Url: https://administrator.de/forum/ps-skript-servergespeicherte-profile-loeschen-nach-alter-288905.html
Ausgedruckt am: 14.04.2025 um 16:04 Uhr