functions/utility/New-ComputerUserReport.ps1
function New-ComputerUserReport { [CmdletBinding()] param ( [Parameter(Mandatory = $true)] [PSFComputer] $ComputerName, [AllowNull()] $LocalAdmins, [AllowNull()] $ServiceAccounts, [AllowNull()] $TaskAccounts, [switch] $DomainOnly, [string] $Server, [PSCredential] $Credential ) $adParameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include Server, Credential #region Resolve Identities #-> Resolutions are cached inside of the called command foreach ($serviceAccount in $ServiceAccounts) { if (-not $serviceAccount.IsDomainAccount) { continue } try { $null = Resolve-Identity @adParameters -InputObject $serviceAccount } catch { # Do Nothing } } foreach ($taskAccount in $TaskAccounts) { if (-not $taskAccount.IsDomainAccount) { continue } try { $null = Resolve-Identity @adParameters -InputObject $taskAccount } catch { # Do Nothing } } #endregion Resolve Identities $domainAccounts = @( ($ServiceAccounts | Where-Object IsDomainAccount).UserSid ($TaskAccounts | Where-Object IsDomainAccount).UserSid ) | Remove-PSFNull -Enumerate foreach ($account in $domainAccounts) { $identity = $null try { $identity = Resolve-Identity @adparameters -Name $account } catch { Write-PSFMessage -Level Warning -Message "[{0}] Failed to resolve identity: {1}" -StringValues $ComputerName, $account } $isLocalAdmin = $null if ($identity) { $isLocalAdmin = $false if ($identity.ObjectSID -in $LocalAdmins.SidString) { $isLocalAdmin = $true } foreach ($group in $identity.GroupMembership) { if ($group -in $LocalAdmins.SidString) { $isLocalAdmin = $true } } } [PSCustomObject]@{ ComputerName = $ComputerName.ComputerName User = $account Identity = $identity IsLocalAdmin = $isLocalAdmin IsLocal = $false Services = $ServiceAccounts | Where-Object { "$account" -eq $_.UserSid } TaskAccounts = $TaskAccounts | Where-Object { "$account" -eq $_.UserSid } } } if ($DomainOnly) { return } $localAccounts = @( ($ServiceAccounts | Where-Object IsDomainAccount -EQ $false).UserSid ($TaskAccounts | Where-Object IsDomainAccount -EQ $false).UserSid ) | Remove-PSFNull -Enumerate foreach ($account in $localAccounts) { $identity = $null $isLocalAdmin = $account -in $LocalAdmins.SidString [PSCustomObject]@{ ComputerName = $ComputerName.ComputerName User = $account Identity = $identity IsLocalAdmin = $isLocalAdmin IsLocal = $true Services = $ServiceAccounts | Where-Object { "$account" -eq $_.UserSid } TaskAccounts = $TaskAccounts | Where-Object { "$account" -eq $_.UserSid } } } } |