我试图创建一个Powershell脚本,如果经过一定天数后仍未在工作站Windows 10上登录帐户,则会删除该帐户。
该脚本将在本地而非远程运行。 我认为iam非常接近解决方案,但是由于某种原因,即使我按照@microsoft的说明进行操作,iam也无法删除带有参数inputobject的帐户。
例如,iam试图删除帐户3和2,因为它们具有相似的LastUseTime日期。
帐户-LastUseTime
testuser3 - 20190625
testuser2 - 20190625
testuser1 - 20190629
代码:
<# Change Value to remove account after a certain days #>
$DaysFilterAccounts = (get-date (Get-Date).AddDays(-6) -UFormat "%Y%m%d")
<# $CheckLastUseTime
Get all UserProfiles, Filter Service and Admin-accounts
Select and convert LastUsetime to a date object
Where only accounts lastusetime is Less Or Equal to $DaysFilterAccounts
#>
$CheckLastUseTime = Get-CimInstance -Filter "Special=False AND Loaded=False" -ClassName Win32_UserProfile |
Select-Object @{Name="LastUseTime";Expression={Get-Date $_.Lastusetime -Format "yyyyMMdd" } } |
Where-Object { $_.LastUseTime -Le $DaysFilterAccounts }
<# Create a String for Lastusetime #>
$AccountLastUseTime = $CheckLastUseTime.Lastusetime
<#
ForEach-Object In $GetProfiles If .LastUsetime is Less Or Equal to $DaysFilterAccounts
Then Get the UserProfile Data And
Remove Profiledata
#>
If ( $AccountLastUseTime -Le $DaysFilterAccounts )
{
$RemoveTheseAccounts = Get-CimInstance -Filter "Special=False AND Loaded=False" -ClassName Win32_UserProfile |
Select-Object Sid, Localpath, @{Name="LastUseTime";Expression={Get-Date $_.Lastusetime -Format "yyyyMMdd" }} |
Where-Object { $_.LastUseTime -Le $DaysFilterAccounts }
Remove-CimInstance -InputObject $RemoveTheseAccounts
} Else { Write-Warning -Message "Error" }
错误消息:
Remove-CimInstance : Cannot convert 'System.Object[]' to the type 'Microsoft.Management.Infrastructure.CimInstance'
required by parameter 'InputObject'. Specified method is not supported.
At C:\Users\fardin.TEST\Desktop\Rolf v 3.ps1:52 char:39
+ Remove-CimInstance -InputObject $RemoveTheseAccounts
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Remove-CimInstance], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.Management.Infrastructure.CimCmdlets.RemoveCimInstanceCommand
解决方案:
<# Change Value to remove account after a certain days #>
$DaysFilterAccounts = (get-date (Get-Date).AddDays(-180) -UFormat "%Y%m%d")
<# $CheckLastUseTime
Get all UserProfiles, Filter Service and Admin-accounts
Select and convert LastUsetime to a date object
Where only accounts lastusetime is Less Or Equal to $DaysFilterAccounts
#>
$CheckLastUseTime = Get-CimInstance -Filter "Special=False AND Loaded=False" -ClassName Win32_UserProfile |
Select-Object @{Name="LastUseTime";Expression={Get-Date $_.Lastusetime -Format "yyyyMMdd" } } |
Where-Object { $_.LastUseTime -Le $DaysFilterAccounts }
<# Create a String for Lastusetime #>
$AccountLastUseTime = $CheckLastUseTime.Lastusetime
<#
ForEach-Object In $GetProfiles If .LastUsetime is Less Or Equal to $DaysFilterAccounts
Then Get the UserProfile Data And
Remove Profiledata
#>
If ( $AccountLastUseTime -Le $DaysFilterAccounts )
{
# Remove Profiles
$GetUserProfileData = Get-WmiObject -Class Win32_UserProfile -Filter "Special=False AND Loaded=False" |
Where { $_.LastUseTime -Le $DaysFilterAccounts }
$GetUserProfileData | Remove-WmiObject
} Else { Write-Warning -Message "Error" }
答案 0 :(得分:3)
在此处进行一些解释,教一个人钓鱼等。按照约定:InputObject属性是指管道中的当前属性,通常意味着您应该将该对象管道化。
运行Get-Help Remove-CimInstance -Parameter InputObject
会向您显示错误的原因:
-InputObject <CimInstance>
Specifies a CIM instance object to be removed from the CIM server.
Note: The input object passed to the cmdlet is not changed, only the instance in the CIM server is removed.
Required? true
Position? 1
Default value none
Accept pipeline input? True (ByValue)
Accept wildcard characters? false
从帮助文档中,您可以看到InputObject接受类型为 CimInstance 的单个对象,因此为它提供了一个 CimInstance [] 数组>无效。您还可以看到它接受管道输入。这给您两个选择。您可以在foreach块中循环浏览对象(不推荐),也可以将对象通过管道传递到函数中。这将为数组中的每个项目调用Remove-CimInstance上的process
块;为每个条目传递 CimInstance 类型的对象。
正如Mathias所说,以下应解决此问题:
$RemoveTheseAccounts | Remove-CimInstance
有关过程块(以及开始和结束块)的更多信息,可以在这里找到:https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_methods?view=powershell-6#process