删除包含';在过去6个月内未使用Cim和Powershell登录
我想通过删除过去6个月内未登录服务器的C:\用户的用户配置文件来释放服务器上的一些C驱动器空间。我使用PowerShell Cim命令连接到服务器 到目前为止,我只找到了删除包含';在过去6个月内未使用Cim和Powershell登录,powershell,user-profile,Powershell,User Profile,我想通过删除过去6个月内未登录服务器的C:\用户的用户配置文件来释放服务器上的一些C驱动器空间。我使用PowerShell Cim命令连接到服务器 到目前为止,我只找到了Get-cimsinstance-CimSession$CimSession-ClassName Win32\u UserProfile命令,该命令将列出用户配置文件,但没有列出每个用户的上次登录时间。是否有其他命令可用于列出具有LastLogon的用户配置文件?一旦我有了这个列表,我想删除在过去6个月内没有登录到服务器的任何配
Get-cimsinstance-CimSession$CimSession-ClassName Win32\u UserProfile
命令,该命令将列出用户配置文件,但没有列出每个用户的上次登录时间。是否有其他命令可用于列出具有LastLogon的用户配置文件?一旦我有了这个列表,我想删除在过去6个月内没有登录到服务器的任何配置文件
此PowerShell脚本示例显示如何删除超过指定天数的用户配置文件
Example 1:
C:\Script\RemoveLocalUserProfile.ps1 -ListUnusedDay 1
Example 2:
C:\Script\RemoveLocalUserProfile.ps1 -DeleteUnusedDay 1 -ExcludedUsers “marry”
# Begin Script
If ($ProfileInfo -eq $null)
{
Write-Warning -Message "The item not found."
}
Else
{
Foreach ($RemoveProfile in $ProfileInfo)
{
#Prompt message
$Caption = "Remove Profile"
$Message = "Are you sure you want to remove profile '$($RemoveProfile.LocalPath)'?"
$Choices = [System.Management.Automation.Host.ChoiceDescription[]]`
@("&Yes", "&No")
[Int]$DefaultChoice = 1
$ChoiceRTN = $Host.UI.PromptForChoice($Caption, $Message, $Choices, $DefaultChoice)
Switch ($ChoiceRTN)
{
0
{
Try {$RemoveProfile.Delete(); Write-Host "Delete profile '$($RemoveProfile.LocalPath)' successfully."}
Catch {Write-Host "Delete profile failed." -ForegroundColor Red}
}
1 {break}
}
}
$ProfileInfo|Select-Object @{Expression = {$_.__SERVER}; Label = "ComputerName"}, `
@{Expression = {$_.ConvertToDateTime($_.LastUseTime)}; Label = "LastUseTime"},`
@{Name = "Action"; Expression = {If (Test-Path -Path $_.LocalPath)
{"Not Deleted"}
Else
{"Deleted"}
}
}
}
# End Script
类似的方法可以在这里看到:
删除配置文件时要小心,不要打到机器特殊帐户。
Win32\u UserProfile
类有一个可以依赖的LastUseTime
属性
$session = New-CimSession -ComputerName $cn
$gcimParams = @{
'CimSession' = $session
'ClassName' = 'Win32_UserProfile'
'Filter' = 'RefCount<1 and Special="false" and Loaded="false"'
}
$profileList = (Get-CimInstance @gcimParams).Where{$PSItem.LastUseTime -lt (Get-Date).AddMonths(-6)}
foreach ($user in $profileList)
{
$user | Remove-CimInstance -CimSession $session
}
$session=New CimSession-ComputerName$cn
$gcimParams=@{
“CimSession”=$session
“ClassName”=“Win32\u用户配置文件”
“Filter'=”RefCount类包括LastLogon
property这不使用CIM
。这是允许的,但最后一个链接显式使用。我的回答是,如果一个链接不起作用,一个就有选项。最后一个链接显式不起作用。它只使用一次命令,然后放弃它,用于Get WmiObject
,重点是操作说了他们“目前发现的”,而不是满足用例的任何最终决定。同样,正如您所知,并且正如您在本网站的几封回复中所说的,如果您在X或Y方面没有成功,那么使用PowerShell还有其他方法。即使OP选择使用CIM,其他参与此讨论的人也可能无法使用,因此回到之前的方法s仍然有效。即使OP也可能运行到无法进行CIM的场景中。执行WMI删除的多个搜索。