Reputation: 25
im a complete newbie at coding/powershell. I "wrote" this small Script
$Username = Read-Host -Prompt 'Bitte den Usernamen eingeben'
$HomeDirectory = '\\server\user'
$ProfilePath = '\\server\profiles'
$TsProfiles = '\\server\tsprofiles'
foreach ($user in $Username) {
Remove-ADUser -Identity $user
}
foreach ($user in $Username) {
Remove-Item "$HomeDirectory\$user" -Recurse -Force -Verbose -whatif
}
foreach ($user in $Username) {
Remove-Item -Path "$ProfilePath\$user" -Recurse -Force -Verbose -whatif
}
foreach ($user in $Username) {
Remove-Item "$TsProfiles\$user*" -Recurse -Force -Verbose -whatif
}
Now this Script is running fine. But i have one small "problem"
1.) For some User there is no TsProfiles or ProfilePath or User. So when i run the script i get this error
At line:15 char:5
+ Remove-Item -Path "$ProfilePath\$user" -Recurse -Force -Verbose - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (\\server\profiles\myusername:String) [Remove-Item], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand
For sure because there is no such path as the user dont have a Profile in that folder. Now im searching for a solution which can write me something like this to a Logfile
** HomeDirectory of %Username% deleted **
** TsProfiles of %Username% deleted **
** Profilepath for %Username% not found **
Upvotes: 0
Views: 1239
Reputation: 36287
Ok, first off you should have it all in one loop, not several loops, but you are only working with 1 user, so there's nothing to loop through. We'll just remove all the loops.
Then, if you want to output if you deleted something you need to check if that something is there. For users we can use Get-ADUser
, and for file paths we can use Test-Path
.
$Username = Read-Host -Prompt 'Bitte den Usernamen eingeben'
$HomeDirectory = '\\server\user'
$ProfilePath = '\\server\profiles'
$TsProfiles = '\\server\tsprofiles'
If(Get-ADUser $username){
Remove-ADUser -Identity $username
Add-Content -Value "** User Account for $Username deleted **" -Path $LogFile
}Else{
Add-Content -Value "** User Account for $Username not found **" -Path $LogFile
}
If(Test-Path "$HomeDirectory\$username"){
Remove-Item "$HomeDirectory\$username" -Recurse -Verbose -Force -WhatIf
Add-Content -Value "** HomeDirectory of $Username deleted **" -Path $LogFile
}Else{
Add-Content -Value "** HomeDirectory of $Username not found **" -Path $LogFile
}
If(Test-Path "$ProfilePath\$username"){
Remove-Item "$ProfilePath\$username" -Recurse -Verbose -Force -WhatIf
Add-Content -Value "** ProfilePath of $Username deleted **" -Path $LogFile
}Else{
Add-Content -Value "** ProfilePath of $Username not found **" -Path $LogFile
}
If(Test-Path "$TsProfiles\$username"){
Remove-Item "$TsProfiles\$username" -Recurse -Verbose -Force -WhatIf
Add-Content -Value "** TsProfiles of $Username deleted **" -Path $LogFile
}Else{
Add-Content -Value "** TsProfiles of $Username not found **" -Path $LogFile
}
Upvotes: 1