我正在编写一个powershell脚本来删除用户配置文件,我知道我使用的方法不是最好的。我想知道怎样做才是更好的方法?我对powershell还很陌生,但我愿意学习。
我已经有的代码:
$ErrorActionPreference= 'silentlycontinue'
$Users = Get-WmiObject -Class Win32_UserProfile
$IgnoreList = "helpdesk", "administrator", "Default"
:OuterLoop
foreach ($User in $Users) {
foreach ($name in $IgnoreList) {
if ($User.localpath -like "*\$name") {
continue OuterLoop
}
}
$User.Delete()
}
发布于 2020-02-12 05:50:51
当企业方法是GPO时,WHy脚本这一点。
没有理由从头做起,leverage what others have provided..。
Use PowerShell to remove local profiles
How to delete user profiles older than a specified number of days in Windows
无论您决定使用哪种方法,都可以使用...as以及MS powershellgallery.com中的模块。
Find-Module -Name '*user*profile*' | Format-Table -AutoSize
<#
# Results
Version Name Repository Description
------- ---- ---------- -----------
1.0 UserProfile PSGallery This module manages user profiles on local and remote computers
0.1.1 Microsoft.Graph.Users.ProfilePhoto PSGallery Microsoft Graph PowerShell Cmdlets
1.0.6 Get-UserProfile PSGallery The Get-UserProfile module list or remove User Profiles from local
#>
Find-Module -Name '*userprofile*' | Format-List -Force
更新
但是,你特别说过..。
:我知道我使用的方法不是最好的。我想知道怎样才是更好的方法?
..。我们都建议,使用GPO是最好的方式,也是正常的行业认可的企业方式。不要写脚本,除非你别无选择。Windows AD将为您完成此操作。
不要重新发明轮子,除非你知道它是一个更好的轮子。当然,在学习中,有研究、尝试和错误,但从已经做到这一点的来源中学习和使用。对于这个用例,网络上有大量的例子。搜索一下就行了。没有理由从头开始这么做。
它们显示了你已经在做的事情...示例-通过Ms powershellgallery.com针对此使用情形预先构建的脚本。
Use PowerShell delete a user profile (step-by-step guide)
Get-CimInstance -ComputerName SRV1,SRV2,SRV3 -Class Win32_UserProfile |
Where-Object { $_.LocalPath.split('\')[-1] -eq 'UserA' } |
Remove-CimInstance
Remove-UserProfile - Remove Local User Profiles and Clean C:\Users Directory
此脚本包含一个函数( remove -UserProfile),该函数用于删除用户配置文件以及本地计算机上C:\Users目录(如果指定)的其他内容。
Delete Unused user Profiles on local machine (PowerShell)
Script Delete user profiles over multiple servers v2
ANd使用您在上述命令中看到的模块,并不是以后要做的事情。这些是在MS和PowerShell中直接提供的,这是有原因的。您在PowerShell中使用的所有内容都来自您的机器上托管的模块,以及您从MS和其他资源下载并安装的模块。
同样,在Windows或其他所选操作系统中使用内置的企业工具,如果它们不能提供您需要的东西,那么请考虑使用其他选项,如编写企业工具未在其GUI中公开的对象级脚本。
发布于 2021-12-29 15:09:49
我也做类似的事情。有了很多配置文件,我发现我不得不等待cpu平静下来,因为appxsvc服务会无限制地产生线程。我过去常常删除每个用户的防火墙规则,但现在似乎没有必要了。
$excludedprofilelist = 'C:\Users\admin1','C:\users\admin2'
$myprofiles = $profiles | where { !$_.Special -and
$excludedprofilelist -notcontains $_.LocalPath }
$sleepseconds = 1
$numcores = 4
foreach ($profile in $myprofiles) {
$msg = "deleting profile " + $profile.LocalPath
$profile | remove-wmiobject
$msg = $msg + " $?"
echo $msg # the result
# recycle bin
if (test-path c:\`$recycle.bin\$($profile.sid)) {
rm -r c:\`$recycle.bin\$($profile.sid) -force
}
# wait for appx cleanup, what if profile delete error?
#while ( (get-appxpackage -user $profile.sid).count ) {
# sleep 1
#}
do {
# make sure it's running
$id = Get-WmiObject -Class Win32_Service -Filter "Name = 'appxsvc'" |
Select-Object -ExpandProperty ProcessId
# $proc = get-process -id $id
# access is denied
# 4proc.priorityclass = 'belownormal'
$cpu1 = (get-process -Id $id).cpu
sleep $sleepseconds
$cpu2 = (get-process -Id $id).cpu
$cpu = [int](($cpu2 - $cpu1)/($numcores*$sleepseconds) * 100)
} while ($cpu)
}
https://stackoverflow.com/questions/60170092
复制相似问题