英文:
Powershell script for deleting users off of a computer
问题
我已编写了一个脚本,使我能够删除计算机上多个用户配置文件和相关的注册表键。这是为了工作,我们几个诊所的多个人共享计算机,最终计算机会充满不再在那里工作或刚刚拥有自己计算机的人的用户配置文件。因此,存储空间会耗尽,这就是我介入的地方,删除许多用户配置文件以清理磁盘。我希望能够更轻松地完成这项工作,所以我尝试编写了一个脚本。以下是脚本。顺便说一句,这都是域加入的。我知道可以通过组策略来完成这个任务,但我的工程师们还没有实现,我只是帮助台试图让自己的工作更轻松。
$profiles = Get-CimInstance -Class Win32_UserProfile
$users = 'schaudhary'
foreach ($profile in $profiles){
if ($profile.Special -ne 'True'){
if ($profile.LocalPath.split('\')[-1] -notcontains $users) {
$profile | Where-Object { $_.LocalPath.split('\')[-1] } | Remove-CimInstance -WhatIf
Write-Host "deleting" $profile.LocalPath
}
}
}
问题是,当我尝试从删除过程中排除多个用户时,它不起作用。但当只有一个用户,就像现在的"schaudhary"一样,它会生效(它会排除schaudhary)。如何使其排除多个用户?我必须排除本地管理员帐户、计算机上的活动用户和一些特殊的服务帐户。如果有人能提供如何在此处添加上次使用时间的提示,那将有所帮助。只有在用户满90天或更多时才删除,类似于这样。
英文:
I have written up a script that gives me the ability to delete multiple user profiles and the registry keys associated off of a computer. this is for work, multiple people share computers at a few clinics of ours and eventually the computer gets filled up of user profiles of people that no longer work there or just got their own computer. so the storage ends up running out, thats where i come in, deleting a lot of the user profiles to clean up the disk. i wanted to make this a lot easier so i tried writing a script. here it is. btw this is all domain join. this can be done by group policy i'm aware but my engineers haven't made that happen, i'm just help desk trying to make my life easier.
$profiles = Get-CimInstance -Class Win32_UserProfile
$users = 'schaudhary'
foreach ($profile in $profiles){
if ($profile.Special -ne 'True'){
if ($profile.LocalPath.split('\')[-1] -notcontains $users) {
$profile | Where-Object { $_.LocalPath.split('\')[-1] } | Remove-CimInstance -WhatIf
Write-Host "deleting" $profile.LocalPath
}
}
}
problem is when i try to exclude multiple users from the deletion process, it doesn't work. but when i have just one user, like right now "schaudhary" it'll work (it'll exclude schaudhary). how can i make it exclude multiple users?? I have to exclude the local admin acct, the active users on the machine and some special service accounts. and if anyone can give tips on adding last use time included in here that'll help. so only delete if user is 90 days old or more, something like that.
答案1
得分: 1
一个与用户相关的事件意味着他正在计算机上工作。但是你必须确保有90天的日志。
$sid =(Get-adUser $username).sid.value
$StartDate =(Get-Date) -(New-TimeSpan -Day 90)
Get-WinEvent -FilterHashtable @{LogName='Security';data=$sid;StartTime=$StartDate} -MaxEvents 1
英文:
an event associated with a user would mean that he was working on a computer.
But you have to make sure there are logs for 90 days
$sid =(Get-adUser $username).sid.value
$StartDate = (Get-Date) - (New-TimeSpan -Day 90)
Get-WinEvent -FilterHashtable @{LogName='Security';data=$sid;StartTime=$StartDate} -MaxEvents 1
答案2
得分: 0
您在使用 -notcontains
时颠倒了变量的顺序。
它的使用方式是 $collectionOfThings -contains $singleThing
或
$collectionOfThings -notcontains $singleThing
。
自PowerShell 3版本以来,您还可以使用 -in
和 -notin
来执行相反操作,如下所示:
$singleThing -in $collectionOfThings
或其否定形式
$singleThing -notin $collectionOfThings
。
请参阅包含运算符
尝试:
$profiles = Get-CimInstance -Class Win32_UserProfile
$users = @('schaudhary') # 要保留的配置文件数组
foreach ($profile in $profiles){
if (!$profile.Special) {
$userName = $profile.LocalPath.split('\')[-1]
# 或:
# if ($userName -notin $users) {
if ($users -notcontains $userName) {
if (!$profile.Loaded) {
$profile | Remove-CimInstance -Confirm:$false -WhatIf
Write-Host "删除 $($profile.LocalPath)"
}
else {
Write-Warning "配置文件 $($profile.LocalPath) 当前已加载.."
}
}
}
}
英文:
You are reversing the variables when using -notcontains
..
Its use is $collectionOfThings -contains $singleThing
or
$collectionOfThings -notcontains $singleThing
.
Since PowerShell version 3, you can also do the reverse using -in
and -notin
as in
$singleThing -in $collectionOfThings
or the negation of that
$singleThing -notin $collectionOfThings
.
<sup>See Containment operators</sup>
Try
$profiles = Get-CimInstance -Class Win32_UserProfile
$users = @('schaudhary') # array of profiles to keep
foreach ($profile in $profiles){
if (!$profile.Special) {
$userName = $profile.LocalPath.split('\')[-1]
# or:
# if ($userName -notin $users) {
if ($users -notcontains $userName) {
if (!$profile.Loaded) {
$profile | Remove-CimInstance -Confirm:$false -WhatIf
Write-Host "Deleting $($profile.LocalPath)"
}
else {
Write-Warning "Profile $($profile.LocalPath) is currently loaded.."
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论