Skip to content

Instantly share code, notes, and snippets.

@ppagal
Created June 17, 2020 15:30
Show Gist options
  • Save ppagal/59819bce3331248b2fa1d1c290fc27c2 to your computer and use it in GitHub Desktop.
Save ppagal/59819bce3331248b2fa1d1c290fc27c2 to your computer and use it in GitHub Desktop.
Function Get-NetLocalGroup {
[cmdletbinding()]
Param(
[Parameter(Position=0)]
[ValidateNotNullorEmpty()]
[object[]]$Computername=$env:computername,
[ValidateNotNullorEmpty()]
[string]$Group = "Administrators",
[switch]$Asjob
)
Write-Verbose "Getting members of local group $Group"
#define the scriptblock
$sb = {
Param([string]$Name = "Administrators")
$members = net localgroup $Name |
where {$_ -AND $_ -notmatch "command completed successfully"} |
select -skip 4
New-Object PSObject -Property @{
Computername = $env:COMPUTERNAME
Group = $Name
Members=$members
}
} #end scriptblock
#define a parameter hash table for splatting
$paramhash = @{
Scriptblock = $sb
HideComputername=$True
ArgumentList=$Group
}
if ($Computername[0] -is [management.automation.runspaces.pssession]) {
$paramhash.Add("Session",$Computername)
}
else {
$paramhash.Add("Computername",$Computername)
}
if ($asjob) {
Write-Verbose "Running as job"
$paramhash.Add("AsJob",$True)
}
#run the command
Invoke-Command @paramhash | Select * -ExcludeProperty RunspaceID
} #end Get-NetLocalGroup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment