[玩转系统] 重新审视本地小组成员
作者:精品下载站 日期:2024-12-14 07:48:31 浏览:14 分类:玩电脑
重新审视本地小组成员
有一天,我发布了一篇文章和函数,该文章和函数使用 ADSI 和 PowerShell 列出本地组的成员。有几个人报告了一个我无法复制的异常错误。在故障排除过程中,我对原始函数做了一些更改,至少可以更好地处理这个神秘的错误。这些更改已更新到 GitHub 并作为该函数的 1.5 版本。
但在测试和修改过程中,我决定我不妨真正改进这个命令,并加入一个使用 PowerShell 远程处理的选项,而不必跳过以前版本中的所有麻烦。
该函数的版本 2 包含一些用于远程处理的参数。这意味着定义一些参数集。我还将大部分 ADSI 代码转换为脚本块,可以使用 & 运算符正常调用它。或者使用 Invoke-Command 远程运行。脚本块的一件棘手的事情是能够打开详细输出。我的解决方案是向脚本块添加一个参数,该参数本质上继承了本地计算机的 VerbosePreference。
我还意识到,如果您想导出信息,在结果中包含组名称可能会有所帮助。
您仍然可以在不进行远程处理的情况下使用该命令,这假定您可以创建与计算机的旧连接。
但我认为您会发现远程选项的性能更好。
"chi-web02","chi-core01" | get-localgroupmember -UseRemoting |
format-table -GroupBy Computername -Property ADSPath,Name,Class
您还可以使用备用凭据和 SSL,但我尚未使用 SSL 或证书进行测试,因为我的网络上没有该设置。
该函数的版本 2 及更高版本可以在 Github 上找到:
Get-LocalGroupMember2.ps1:
#requires -version 4.0
Function Get-LocalGroupMember {
Get-LocalGroupMember -computer chi-core01 | tmo -clip
Computername : CHI-CORE01
Group : Administrators
Name : Administrator
ADSPath : WinNT://GLOBOMANTICS/chi-core01/Administrator
Class : User
Domain : GLOBOMANTICS
IsLocal : True
Computername : CHI-CORE01
Group : Administrators
Name : Domain Admins
ADSPath : WinNT://GLOBOMANTICS/Domain Admins
Class : Group
Domain : GLOBOMANTICS
IsLocal : False
Computername : CHI-CORE01
Group : Administrators
Name : Chicago IT
ADSPath : WinNT://GLOBOMANTICS/Chicago IT
Class : Group
Domain : GLOBOMANTICS
IsLocal : False
Computername : CHI-CORE01
Group : Administrators
Name : OMAA
ADSPath : WinNT://GLOBOMANTICS/OMAA
Class : User
Domain : GLOBOMANTICS
IsLocal : False
Computername : CHI-CORE01
Group : Administrators
Name : LocalAdmin
ADSPath : WinNT://GLOBOMANTICS/chi-core01/LocalAdmin
Class : User
Domain : GLOBOMANTICS
IsLocal : True
.EXAMPLE
PS C:\> "chi-hvr1","chi-hvr2","chi-core01","chi-fp02" | get-localgroupmember | where {$_.IsLocal} | Select Computername,Name,ADSPath
Computername Name ADSPath
------------ ---- -------
CHI-HVR1 Administrator WinNT://GLOBOMANTICS/chi-hvr1/Administrator
CHI-HVR2 Administrator WinNT://GLOBOMANTICS/chi-hvr2/Administrator
CHI-HVR2 Jeff WinNT://GLOBOMANTICS/chi-hvr2/Jeff
CHI-CORE01 Administrator WinNT://GLOBOMANTICS/chi-core01/Administrator
CHI-CORE01 LocalAdmin WinNT://GLOBOMANTICS/chi-core01/LocalAdmin
CHI-FP02 Administrator WinNT://GLOBOMANTICS/chi-fp02/Administrator
.EXAMPLE
PS C:\> "chi-core01","chi-hvr1","chi-hvr2","Chi-web02","chi-test02" | Get-LocalGroupMember -useremoting -credential globomantics\administrator | Where {$_.name -notmatch "Administrator|Domain Admins"} | Select Computername,ADSPath,Class
Computername ADSPath Class
------------ ------- -----
CHI-CORE01 WinNT://GLOBOMANTICS/Chicago IT Group
CHI-CORE01 WinNT://GLOBOMANTICS/OMAA User
CHI-CORE01 WinNT://GLOBOMANTICS/chi-core01/LocalAdmin User
CHI-HVR1 WinNT://GLOBOMANTICS/OMAA User
CHI-HVR2 WinNT://GLOBOMANTICS/chi-hvr2/Jeff User
CHI-HVR2 WinNT://GLOBOMANTICS/OMAA User
CHI-WEB02 WinNT://GLOBOMANTICS/OMAA User
Using PowerShell remoting and an alternate credential, find all members of the Administrators group that is not Administrator or Domain Admins.
.EXAMPLE
PS C:\> get-localgroupmember -Name "Hyper-V administrators" -Computername chi-hvr1,chi-hvr2
Computername : CHI-HVR1
Group : Hyper-V Administrators
Name : jeff
ADSPath : WinNT://GLOBOMANTICS/jeff
Class : User
Domain : GLOBOMANTICS
IsLocal : False
Computername : CHI-HVR2
Group : Hyper-V Administrators
Name : jeff
ADSPath : WinNT://GLOBOMANTICS/jeff
Class : User
Domain : GLOBOMANTICS
IsLocal : False
Check group membership for the Hyper-V Administrators group.
.EXAMPLE
PS C:\> get-localgroupmember -Computername chi-core01 | where class -eq 'group' | select Domain,Name
Domain Name
------ ----
GLOBOMANTICS Domain Admins
GLOBOMANTICS Chicago IT
Get members of the Administrators group on CHI-CORE01 that are groups and select a few properties.
.NOTES
NAME : Get-LocalGroupMember
VERSION : 2.1
LAST UPDATED: 2/18/2016
AUTHOR : Jeff Hicks (@JeffHicks)
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
.INPUTS
[string] for computer names
.OUTPUTS
[object]
#>
[cmdletbinding(DefaultParameterSetName = "ADSI")]
Param(
[Parameter(Position = 0)]
[Parameter(ParameterSetName = "remoting")]
[Parameter(ParameterSetName = "ADSI")]
[ValidateNotNullorEmpty()]
[string]$Name = "Administrators",
[Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
[Parameter(ParameterSetName = "remoting")]
[Parameter(ParameterSetName = "ADSI")]
[ValidateNotNullorEmpty()]
[Alias("CN","host")]
[string[]]$Computername = $env:computername,
[Parameter(ParameterSetName = "remoting")]
[switch]$UseRemoting,
[Parameter(ParameterSetName = "remoting")]
[Alias("RunAs")]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName = "remoting")]
[ValidateRange(1, 65535)]
[int]$Port,
[Parameter(ParameterSetName = "remoting")]
[switch]$UseSSL,
[Parameter(ParameterSetName = "remoting")]
[string]$CertificateThumbprint
)
Begin {
Write-Verbose "[Starting] $($MyInvocation.Mycommand)"
Write-Verbose "[Begin] Using parameter set $($PSCmdlet.ParameterSetName)"
#define a scriptblock
$do = {
Param(
[string]$Name = "Administrators",
[string]$Computer=$env:computername,
[string]$Verbose
)
$VerbosePreference = $Verbose
#define a flag to indicate if there was an error
$script:NotFound = $False
#define a trap to handle errors because we're not using cmdlets that
#could support Try/Catch. Traps must be in same scope.
Trap [System.Runtime.InteropServices.COMException] {
$errMsg = "Failed to enumerate $name on $computer. $($_.exception.message)"
Write-Warning $errMsg
#set a flag
$script:NotFound = $True
Continue
}
#define a Trap for all other errors
Trap {
Write-Warning "Oops. There was some other type of error on $Computer : $($_.exception.message)"
Continue
}
Write-Verbose "[Process] Connecting to $computer"
#the WinNT moniker is case-sensitive
[ADSI]$group = "WinNT://$computer/$Name,group"
Write-Verbose "[Process] Querying members of the $Name group"
$members = $group.invoke("Members")
Write-Verbose "[Process] Counting group members"
if (-Not $script:NotFound) {
$found = ($members | measure).count
Write-Verbose "[Process] Found $found members"
if ($found -gt 0 ) {
$members | foreach {
#define an ordered hashtable which will hold properties
#for a custom object
$Hash = [ordered]@{Computername = $computer.toUpper();Group = $Group.Name.Value}
#Get the name property
$hash.Add("Name",$_[0].GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null))
#get ADS Path of member
$ADSPath = $_[0].GetType().InvokeMember("ADSPath", 'GetProperty', $null, $_, $null)
$hash.Add("ADSPath",$ADSPath)
#get the member class, ie user or group
$hash.Add("Class",$_[0].GetType().InvokeMember("Class", 'GetProperty', $null, $_, $null))
$hash.Add("Domain",$ADSPath.Split("/")[2])
#if computer name is found between two /, then assume
#the ADSPath reflects a local object
if ($ADSPath -match "/$computer/") {
$local = $True
}
else {
$local = $False
}
$hash.Add("IsLocal",$local)
#turn the hashtable into an object
New-Object -TypeName PSObject -Property $hash
} #foreach member
}
else {
Write-Warning "No members found in $Name on $Computer."
}
} #if no errors
} #close Do scriptblock
if ($PSCmdlet.ParameterSetName -eq "Remoting") {
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag