【问题标题】:Remove user with filter based on SID使用基于 SID 的过滤器删除用户
【发布时间】:2018-10-04 21:58:54
【问题描述】:

我有一些机器有很多配置文件,我想删除所有配置文件,除了 3 个是管理员。

如果我想删除所有内容,这将完成这项工作

Get-WMIObject -Class Win32_USerProfile -ComputerName $computer | Remove-WmiObject

我曾尝试使用where 子句和-filter 来做到这一点,但没有成功。

这里有一些例子

Get-WMIObject -Class Win32_UserProfile -ComputerName $computer | where {($.SID -neq $UserSID)} | Remove-WMIObject

...

Get-WMIObject -Class Win32_USerProfile -ComputerName $computer -Filter "SID = TEST" | Remove-WmiObject

编辑:我几乎是这样的:

 Get-WMIObject -Class Win32_USerProfile -ComputerName $computer | Where-Object -FilterScript {$_.SID -ne "S-1-5-18" -and $_.SID -ne "S-1-5-19" -and $_.SID -ne "S-1-5-20"} |Remove-WmiObject -WhatIf

这样我可以过滤输出,但我遇到了异常

+ CategoryInfo          : NotSpecified: (:) [Remove-WmiObject], ArgumentException

【问题讨论】:

    标签: powershell wmi user-profile get-wmiobject


    【解决方案1】:
    $UserSID = 'S-1-5-18'
    Get-WMIObject -Class Win32_UserProfile -ComputerName $computer | where {$_.SID -ne $UserSID} | Remove-WmiObject -WhatIf
    Get-WMIObject -Class Win32_USerProfile -ComputerName $computer -Filter "SID != '$UserSID'" | Remove-WmiObject -WhatIf
    

    【讨论】:

    • 使用-whatif 我可以看到本应被删除的配置文件,这是正确的!但是当我删除它时,我得到了一个 ArgumentException
    • 在目标计算机本地而不是远程运行是否成功?
    • 不,它返回相同的异常
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多