ps/Modules/Alkami.DevOps.Operations/Private/Get-UsernamesWithProcesses.ps1
2023-05-30 22:51:22 -07:00

57 lines
1.5 KiB
PowerShell

function Get-UsernamesWithProcesses {
<#
.SYNOPSIS
Retrieves the unique set of usernames associated with all running processes.
.SYNOPSIS
Retrieves the unique set of usernames associated with all running processes. This call requires elevated privileges to execute.
.PARAMETER ComputerName
The name of the server where the operation should be performed. If omitted, defaults to the current host.
.OUTPUTS
An array of strings containing the usernames of all users with running processes.
.EXAMPLE
Get-UsernamesWithProcesses
SYSTEM
njones
LOCAL SERVICE
DWM-2
UMFD-0
UMFD-2
NETWORK SERVICE
#>
[CmdLetBinding()]
[OutputType([string[]])]
param(
[Parameter(Mandatory = $false)]
[string] $ComputerName = $null
)
$scriptBlock = {
if ( -not (Test-IsAdmin)) {
throw 'This call requires elevated permissions to run.'
}
$result = (Get-Process -IncludeUserName).UserName
$result = $result | Where-Object { -not (Test-StringIsNullOrWhitespace -Value $_) }
$result = $result | ForEach-Object { $_.Split('\')[-1] }
$result = $result | Select-Object -Unique
return $result
}
# Determine if we are executing remotely.
$splatParams = @{}
if ( -not (Test-StringIsNullOrWhitespace -Value $ComputerName) -and -not (Compare-StringToLocalMachineIdentifiers -stringToCheck $ComputerName )) {
$splatParams['ComputerName'] = "$ComputerName"
}
$result = Invoke-Command -ScriptBlock $scriptBlock @splatParams
return $result
}