這是使用以下約束/規則遠程洗掉用戶組態檔的代碼:
########## List of accounts/Student profiles older than 21 days and not test accounts to be deleted #######################
foreach ($computer in (Get-Content e:\cleanupscript\scripts\ComputerList.csv)) {
if(Test-Connection -ComputerName $computer -count 1){
$userfolders = Get-ChildItem "\\$computer\C$\users\" | Select-Object -ExpandProperty Name
}
#The list of accounts, which profiles must not be deleted
$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"
$StudentsProfiles = Get-WmiObject -ComputerName $computer -Class Win32_UserProfile |
Select-Object PSComputerName,
Sid,
LocalPath,
@{Label='LastUseTime';Expression={$_.ConvertToDateTime($_.LastUseTime)} } |
Where-Object -FilterScript {![System.String]::IsNullOrWhiteSpace($_.LastUseTime)} |
Where-Object -FilterScript {!($_.LocalPath -eq "C:\Windows\ServiceProfiles\ksnproxy")} |
Where-Object -FilterScript {!($ExcludedUsers -like ($_.LocalPath.Replace("C:\Users\","")))} |
Sort-Object -Property {($_.LastUseTime)} |
Select-Object -First 3 |
foreach ($StudentsProfile in $StudentsProfiles)
{
#Write-Host $StudentsProfile.LocalPath, $computer, $StudentsProfile.LastUseTime, "profile existing” -ForegroundColor Red
if (!($ExcludedUsers -like ($StudentsProfile.LocalPath.Replace("C:\Users\",""))))
{
$StudentsProfile | Remove-WmiObject
Write-Host $computer, $StudentsProfile.LocalPath, $StudentsProfile.LastUseTime, "profile deleted” -ForegroundColor Magenta
}
}
}
Remove-WmiObject :輸入物件不能系結到命令的任何引數,因為該命令不接受管道輸入,或者輸入及其屬性與接受管道輸入的任何引數都不匹配。在行:1 字符:7
- $st | Remove-WmiObject -Authentication Connect
-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~- CategoryInfo : InvalidArgument: (@{PSComputerNam...-07 6:49:46 AM}:PSObject) [Remove-WmiObject], ParameterBindingException
- FullQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.RemoveWmiObject
uj5u.com熱心網友回復:
順便說一句:CIM cmdlet(例如Get-CimInstance,Get-WmiObject)在 PowerShell v3(2012 年 9 月發布)中取代了 WMI cmdlet(例如,)。因此,應該避免使用WMI cmdlet,尤其是因為 PowerShell(核心)(v6 ),未來所有的努力都將進行,甚至不再擁有它們。但是請注意,WMI 仍然是CIM cmdlet 的基礎。有關更多資訊,請參閱此答案。
當從管道
Remove-WmiObject接收輸入時,它需要型別為系結到其引數的輸入物件。System.Management.ManagementObject-InputObject相比之下,你的
$StudentsProfiles變數不不包含此型別的物件,因為你已經應用與屬性名到原來的呼叫,這將創建一個通用型別的物件,即是無關的輸入物件的型別,并且只包含具有從同名輸入物件的屬性復制的值的指定屬性。Select-ObjectGet-WmiObject[pscustomobject]
因此,在管道收集的物件$StudentsProfiles,以Remove-WmiObject使你看到的錯誤:輸入物件是錯誤的型別。
解決方案:
省略Select-Object PSComputerName, ...呼叫并將管道限制為僅過濾(Where-Object呼叫,Select-Object呼叫 with -Last)和排序命令,這兩個命令都不會更改輸入物件的標識:
使用流水線的簡化版本:
$StudentsProfiles = Get-WmiObject -ComputerName $computer -Class Win32_UserProfile |
Where-Object -FilterScript {
$_.LastUseTime -and
$_.LocalPath -ne "C:\Windows\ServiceProfiles\ksnproxy" -and
(Split-Path -Leaf $_.LocalPath) -notin $ExcludedUsers
} |
Sort-Object LastUseTime |
Select-Object -First 3
注意:$_.ConvertToDateTime($_.LastUseTime)- 即顯式轉換為[datetime]- 實際上并不需要:
要測驗該值是否為非空(非空),
$_.LastUseTime- 它是一個表示時間戳的字串,可以按原樣使用,因為將$nullor''(空字串)解釋為布林值會產生$false,而任何非空字串都會產生$true。類似地,
LastUseTime可以傳遞給Sort-Object由該屬性值進行排序直接,因為時間戳的字串表示-例如'20211110143544.500000-300'-的方式被構造詞法排序相當于按時間順序排序。
順便說一句:在這方面,首選的 CIM cmdlet 比 WMI cmdlet 有優勢:它們將時間戳直接表示為[datetime]- 不需要轉換。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/362428.html
下一篇:如果適用,需要使用Windows批處理命令單行洗掉檔案夾按名稱,而不是按date.time使用powershell
