Created
March 8, 2019 10:14
-
-
Save spy86/ea016b4cf8069fdd10a4d047df790d52 to your computer and use it in GitHub Desktop.
Function Get-NetLocalGroup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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