【问题标题】:Powershell Script for deleting AD User & Profile用于删除 AD 用户和配置文件的 Powershell 脚本
【发布时间】:2021-08-06 03:49:06
【问题描述】:

我是编码/powershell 的新手。我“写”了这个小脚本

$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
}

现在这个脚本运行良好。但我有一个小“问题”

1.) 对于某些用户,没有 TsProfiles 或 ProfilePath 或用户。所以当我运行脚本时,我得到了这个错误

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

肯定是因为没有这样的路径,因为用户在该文件夹中没有配置文件。现在我正在寻找一种解决方案,可以将这样的内容写入日志文件

** %Username% 的主目录已删除 **

** %Username% 的 TsProfiles 已删除 **

** %Username% 的配置文件路径未找到 **

【问题讨论】:

  • 我想你想要像If(Test-Path "$HomeDirectory\$user"){Remove-Item "$HomeDirectory\$user" -Recurse -Verbose -Force -WhatIf; Add-Content -Value "** HomeDirectory of $User deleted **" -Path $LogFile}Else{Add-Content -Value "** HomeDirectory of $User not found **" -Path $LogFile}这样的东西
  • 非常感谢!那是完美的工作!如果用户也被删除,是否可以登录?我不知道如何在那里检查它,因为我不能使用测试路径:(

标签: powershell active-directory


【解决方案1】:

好的,首先您应该在一个循环中完成所有操作,而不是几个循环,但是您只与 1 个用户一起工作,所以没有什么可循环的。我们将删除所有循环。

然后,如果要输出是否删除了某些内容,则需要检查该内容是否存在。对于用户我们可以使用Get-ADUser,对于文件路径我们可以使用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
}

【讨论】:

  • 是的,那是我的错,我忘了我们删除了循环,所以我们需要使用$Username 而不仅仅是$User。我更新了答案,它现在应该可以按预期工作了。
  • 遗憾的是,用户的部分无法正常工作。如果我使用不存在的用户名,我会从 Powershell Get-ADUser 收到错误:在“DC=int,DC=domain,DC=de”下找不到具有身份的对象:“asdsada”。但是没有找不到用户的日志条目,我只是看到找不到文件夹。
  • 啊,对,Get-ADUser 不喜欢寻找特定用户却找不到。作为替代方案,您可以改用Get-ADUser -filter "samAccountName -eq '$Username'",这不会对您产生错误,并且应该按预期工作
  • 我把那个代码放进去,它的工作只需将 $username 更改为 $user :P 现在最后一个问题是,当用户在 Powershell 窗口中不存在时,我是否也可以显示错误?现在你输入了一个错误的名字,如果有错误必须检查日志文件。
  • 知道了 :) 刚刚添加了 Write-Output "** AD-User $User was not found **" 在每一行都有正确的文本:)
猜你喜欢
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2020-06-16
  • 2017-09-25
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多