Skip to content

Commit

Permalink
Merge pull request #40 from rstolpe/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rstolpe authored May 14, 2024
2 parents 42ba6fa + d9dc0e6 commit 04a9ec5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
6 changes: 3 additions & 3 deletions DeleteUserProfile/DeleteUserProfile.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
RootModule = '.\DeleteUserProfile.psm1'

# Version number of this module.
ModuleVersion = '2.0.1'
ModuleVersion = '2.0.2'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand All @@ -57,7 +57,7 @@
Description = 'This module will let you list or delete Windows user profile from both local and remote computers.'

# Minimum version of the PowerShell engine required by this module
PowerShellVersion = '7.3'
PowerShellVersion = '7.4'

# Name of the PowerShell host required by this module
# PowerShellHostName = ''
Expand Down Expand Up @@ -134,7 +134,7 @@
ReleaseNotes = 'https://github.com/rstolpe/DeleteUserProfile/releases'

# Prerelease string of this module
#Prerelease = ''
Prerelease = 'beta'

# Flag to indicate whether the module requires explicit user acceptance for install/update/save
RequireLicenseAcceptance = $false
Expand Down
55 changes: 30 additions & 25 deletions DeleteUserProfile/DeleteUserProfile.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,29 @@
$GetUserData = Get-CimInstance -CimSession $CimSession -className Win32_UserProfile | Where-Object { $_.Special -eq $false } | Select-Object LocalPath, LastUseTime, Loaded | Sort-Object -Descending -Property LastUseTime

$UserProfileData = foreach ($_profile in $GetUserData) {

$NotUsedFor = [ordered]@{}
# Calculate how long it was the profile was used
if (-Not([string]::IsNullOrEmpty($_profile.LastUseTime))) {
$NotUsed = NEW-TIMESPAN -Start $_profile.LastUseTime -End (Get-Date) | Select-Object days, hours, Minutes | Foreach-Object {
[PSCustomObject]@{
days = if ($Null -eq $_.Days -or $_.Days -eq "0") { $Null } else { $_.Days }
hours = if ($Null -eq $_.Hours -or $_.Hours -eq "0") { $Null } else { $_.Hours }
minutes = if ($Null -eq $_.Minutes -or $_.Minutes -eq "0") { $Null } else { $_.Minutes }
NEW-TIMESPAN -Start $_profile.LastUseTime -End (Get-Date) | Select-Object days, hours, Minutes | Foreach-Object {
if ($Null -ne $_.Days -or $_.Days -gt "0") {
$NotUsedFor.Add("days", "$($_.Days)")
}
if ($Null -ne $_.Hours -or $_.Hours -gt "0") {
$NotUsedFor.Add("hours", "$($_.Hours)")
}
if ($Null -ne $_.Minutes -or $_.Minutes -gt "0") {
$NotUsedFor.Add("minutes", "$($_.Minutes)")
}
}
}

[PSCustomObject]@{
ProfileUserName = if ($null -ne $_profile.LocalPath) { $_profile.LocalPath.split('\')[-1] }
ProfilePath = if ($null -ne $_profile.LocalPath) { $_profile.LocalPath }
LastUsed = if ($null -ne $_profile.LastUseTime) { ($_profile.LastUseTime -as [DateTime]).ToString("yyyy-MM-dd HH:mm") }
ProfileLoaded = if ($null -ne $_profile.Loaded) { $_profile.Loaded }
NotUsed = if (-Not([string]::IsNullOrEmpty($NotUsed))) { $NotUsed } else { "N/A" }
Computer = $Using:_computer
UserName = if ($null -ne $_profile.LocalPath) { $_profile.LocalPath.split('\')[-1] }
LocalPath = if ($null -ne $_profile.LocalPath) { $_profile.LocalPath }
LastUsed = if ($null -ne $_profile.LastUseTime) { ($_profile.LastUseTime -as [DateTime]).ToString("yyyy-MM-dd HH:mm") }
Loaded = if ($null -ne $_profile.Loaded) { $_profile.Loaded }
NotUsed = if (-Not([string]::IsNullOrEmpty($NotUsedFor))) { $NotUsedFor } else { "N/A" }
}
}

Expand All @@ -92,7 +97,7 @@
}
}
else {
Write-Output "Could not connect to $($Using:_computer) trough WinRM, please check the connection and try again"
Write-Error "Could not connect to $($Using:_computer) trough WinRM, please check the connection and try again"
continue
}
}
Expand All @@ -102,14 +107,14 @@
}
}
else {
Write-Output "Could not establish connection against $($Using:_computer)"
Write-Error "Could not establish connection against $($Using:_computer)"
continue
}
}
}

$ReturnProfiles = Receive-Job $JobGetProfile -AutoRemoveJob -Wait
$ReturnProfiles
return $ReturnProfiles
}
Function Remove-RSUserProfile {
<#
Expand Down Expand Up @@ -187,17 +192,17 @@ Function Remove-RSUserProfile {
break
}

if ($null -eq $UserName -and $All -eq $false) {
Write-Output "You must enter a username or use the switch -All to delete user profiles!"
<#if ($null -eq $UserName -and $All -eq $false) {
Write-Error "You must enter a username or use the switch -All to delete user profiles!"
break
}
}#>

$JobReturnMessage = [System.Collections.ArrayList]::new()
$CheckComputer = $(try { Test-WSMan -ComputerName $_computer -ErrorAction SilentlyContinue } catch { $null })
$CheckComputer = $(try { Test-WSMan -ComputerName $ComputerName -ErrorAction SilentlyContinue } catch { $null })

if ($null -ne $CheckComputer) {
# Open CIM Session
$CimSession = $(try { New-CimSession -ComputerName $_computer -ErrorAction SilentlyContinue } catch { $null })
$CimSession = $(try { New-CimSession -ComputerName $ComputerName -ErrorAction SilentlyContinue } catch { $null })
# Collecting all user profiles on the computer
if ($null -ne $CimSession) {
$GetAllProfiles = Get-CimInstance -CimSession $CimSession -ClassName Win32_UserProfile | Where-Object { $_.Special -eq $false }
Expand Down Expand Up @@ -231,7 +236,7 @@ Function Remove-RSUserProfile {
# if you don't want to delete all profiles but just one or more
elseif ($All -eq $false) {
$JobDelete = foreach ($_profile in $UserName) {
$CheckProfile = Confirm-RSProfile -UserName $_profile -ProfileData $GetAllProfiles
$CheckProfile = Confirm-RSProfile -UserName $_profile -ProfileData $GetAllProfiles -Exclude $Exclude

if ($CheckProfile.ReturnCode -eq 0) {
$GetProfile = $GetAllProfiles | Where-Object { $_.LocalPath -like "*$($_profile)" }
Expand Down Expand Up @@ -265,27 +270,27 @@ Function Remove-RSUserProfile {
}
}
else {
Write-Output "Could not connect to $($_computer) trough WinRM, please check the connection and try again"
Write-Error "Could not connect to $($_computer) trough WinRM, please check the connection and try again"
continue
}
}
else {
Write-Output "Could not establish connection against $($_computer)"
Write-Error "Could not establish connection against $($_computer)"
continue
}
}
Function Confirm-RSProfile {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, HelpMessage = ".")]
[Parameter(Mandatory = $true, HelpMessage = "Username of the user you want to verify")]
[ValidateNotNullOrEmpty()]
[string]$UserName,
[Parameter(Mandatory = $true, HelpMessage = ".")]
[ValidateNotNullOrEmpty()]
$ProfileData,
[Parameter(Mandatory = $true, HelpMessage = ".")]
[Parameter(Mandatory = $false, HelpMessage = "Enter the username you want to exclude from deletion")]
[ValidateNotNullOrEmpty()]
$Exclude
[String[]]$Exclude
)

$CheckExists = $ProfileData | Where-Object { $_.LocalPath -like "*$($UserName)" }
Expand Down

0 comments on commit 04a9ec5

Please sign in to comment.