You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Remove-WmiObject删除远程用户配置文件报InputObjectNotBound错误如何解决

报错触发原因

  • 核心原因:你在查询到Win32_UserProfile WMI对象后,直接使用Select-Object自定义了返回属性,这一操作会将原生WMI对象转换为普通PSCustomObject,丢失了WMI对象的类型标识和底层方法。而Remove-WmiObject仅支持接收原生WMI对象作为管道输入,传入自定义对象无法匹配参数绑定规则,因此触发参数绑定异常。
  • 附加问题1:原代码缺少规则要求的超过21天未使用的过滤逻辑,当前会删除任意非排除列表的旧账号,不符合业务要求。
  • 附加问题2:代码中混杂了中文双引号(如”DefaultAppPool”、”profile existing”),PowerShell无法正确识别这类引号,会额外触发语法错误。
  • 附加问题3:Get-WmiObject自PowerShell 3.0起已被弃用,兼容性和安全性较差,也容易出现跨版本调用异常。

修正方案

修改核心逻辑为先过滤符合条件的原生WMI/CIM对象,再按需提取属性展示信息,同时补全21天的时间过滤逻辑,替换为更稳定的CIM cmdlet,修正引号问题。修正后的完整代码如下:

########## 删除超过21天未使用、非测试账号的学生配置文件 #######################
# 禁止删除的账号列表
$ExcludedUsers = "Public","administrator","csimg","DefaultAppPool", "ksnproxy", "test1","test2","test3","test4","test5","test6","test7","test8","test9","test10","test11","test12","test13","test14","test15","test16","test17","test18","test19","test20","test21","test22","test23","test24","test25","test26","test27","test28","test29","test30","test31","test32","test33","test34","test35","test36","test37","test38","test39","test40","test41","test42","test43","test44","test45","test46","test47","test48","test49","test50","test51","test52","test53","test54","test55","test56","test57","test58","test59","test60","test61","test62","test63","test64","test65","test66","test67","test68","test69","test70","test71","test72","test73","test74","test75","test76","test77","test78","test79","test80","test81","test82","test83","test84","test85"
# 21天的时间阈值
$expireDate = (Get-Date).AddDays(-21)

foreach ($computer in (Get-Content e:\cleanupscript\scripts\ComputerList.csv)) {
    # 先检测主机是否在线
    if(-not (Test-Connection -ComputerName $computer -Count 1 -Quiet)){
        Write-Warning "$computer 无法连通,跳过处理"
        continue
    }

    try {
        # 获取符合条件的用户配置文件(保留原生CIM对象供删除使用)
        $StudentsProfiles = Get-CimInstance -ComputerName $computer -ClassName Win32_UserProfile -ErrorAction Stop | 
            Where-Object {
                # 排除系统服务账号
                $_.LocalPath -ne "C:\Windows\ServiceProfiles\ksnproxy" -and
                # 排除禁止删除的账号
                $ExcludedUsers -notcontains $_.LocalPath.Replace("C:\Users\","") -and
                # 过滤最后使用时间不为空且超过21天
                $_.LastUseTime -ne $null -and
                $_.ConvertToDateTime($_.LastUseTime) -lt $expireDate
            } | Sort-Object LastUseTime
        
        foreach ($profile in $StudentsProfiles) {
            $lastUse = $profile.ConvertToDateTime($profile.LastUseTime)
            # 删除配置文件
            Remove-CimInstance -InputObject $profile
            Write-Host "$computer $($profile.LocalPath) $lastUse 配置文件已删除" -ForegroundColor Magenta
        }
    }
    catch {
        Write-Warning "处理$computer 时出错:$_"
    }
}

内容的提问来源于stack exchange,提问作者Rodrigo N M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 21:45:02