[玩转系统] 使用 WMI、WSMAN 和 PowerShell 解析 SID
作者:精品下载站 日期:2024-12-14 07:40:03 浏览:14 分类:玩电脑
使用 WMI、WSMAN 和 PowerShell 解析 SID
在 Windows 世界中,帐户 SID 可能是一件非常神秘的事情。 S-1-5-21-2250542124-3280448597-2353175939-1019 是谁?幸运的是,许多应用程序(例如事件日志查看器)将 SID 解析为帐户名。缺点是,当您从 PowerShell 访问相同类型的信息时,您最终会得到“原始”SID。虽然有各种各样的命令行工具,甚至可能有人会在我发布本文后分享一些很酷的 .NET 技巧,但您很可能想要找到 PowerShell 解决方案。
您最初的假设可能是使用 WMI。搜索 Root\CIMv2 您甚至会找到 Win32_SID 类。呜呼!这就是我需要做的:
get-wmiobject win32_sid -filter "sid='S-1-5-21-2250542124-3280448597-2353175939-1019'"
嗯,不。如图所示,您无法查询这个特定的类。
相反,您需要直接访问 Win32_SID 类的实例。在PowerShell中,简单的方法是使用[WMI]类型加速器,并指定实例的路径。
[WMI]"root\cimv2:win32_sid.sid='S-1-5-21-2250542124-3280448597-2353175939-1019'"
如果要查询远程计算机上的 SID,路径将为 \\SERVERNAME\root\cimv2:CLASSNAME.Keyproperty='Something'。但请注意,无法使用 [WMI] 指定备用凭据。不过,您可以查询 Win32_Account 类来获取 SID。
PS Scripts:\> get-wmiobject win32_account -filter "sid='S-1-5-21-2250542124-3280448597-2353175939-1019'"
AccountType : 512
Caption : Serenity\localadmin
Domain : Serenity
SID : S-1-5-21-2250542124-3280448597-2353175939-1019
FullName :
Name : localadmin
这使您可以享受使用 cmdlet、查询远程计算机和使用备用凭据的好处。
在 PowerShell 3.0 中,如果您想使用新的 CIM cmdlet 并通过 WSMan 查询 WMI,则可以很容易地将以前的命令转换为 CIM 命令。
PS Scripts:\> get-ciminstance win32_account -filter "sid='S-1-5-21-2250542124-3280448597-2353175939-1019'"
Name Caption AccountType SID Domain
---- ------- ----------- --- ------
localadmin Serenity\localadmin 512 S-1-5-21-22505421... Serenity
这些查询非常好,但我相信如果您能直接进入实例,那就更好了。不幸的是,我找不到任何与 CIM 相关的加速器可以给我提供与使用 [WMI] 加速器相同的结果。请记住,我的目标是利用新的 WSMan 协议。解决方案是使用 Get-WSManInstance cmdlet。
Get-WSManInstance -ResourceURI "wmicimv2/Win32_SID" -SelectorSet @{SID="S-1-5-21-2250542124-3280448597-2353175939-1019"}
我认为您可以看出 ResourceUri 是类的路径,而 SelectorSet 是一个带有键属性(在本例中为 SID)和相应值的哈希表。结果看起来有点不同,但关键部分(例如帐户名称)都在那里。
Get-WSManInstance 还支持备用凭据。因此,考虑到所有这些,我组合了一个使用这种方法的名为 Resolve-SID 的函数。但作为后备方案,您也可以告诉它使用 WMI。
Function Resolve-SID {
<#
.Synopsis
Resolve account name from SID.
.Description
This command uses the WSMAN protocol to query WMI and resolve an account based
on its SID. Using WMI it was possible to run a command like this:
[wmi]$user="\SERVER01\root\cimv2:Win32_SID.Sid='S-1-5-18'"
But this relies on WMI and DCOM. This command uses a CIM-cmdlet approach that
queries WMI over the WSMAN protocol. If the SID can't be resolved to a user name
an exception will be thrown.
If you want to revert back to the WMI and DCOM approach, use the -UseWMI parameter.
However, you will not be able to use alternate credentials.
.Parameter SID
It is assumed the SID will start with S- and you must enter a complete SID.
Wildcards are not allowed.
.Parameter Computername
The name of the computer to query. The default is the localhost. The parameter
has an alias of CN.
.Parameter UseWMI
Revert to the legacy [WMI] command. This parameter has an alias of WMI.
.Parameter Credential
This parameter as an alias of RunAs. Specify either a username or a PSCredential
object.
.Notes
Last Updated: October 15, 2013
Version : 1.0
Learn more:
PowerShell in Depth: An Administrator's Guide (http://www.manning.com/jones2/)
PowerShell Deep Dives (http://manning.com/hicks/)
Learn PowerShell 3 in a Month of Lunches (http://manning.com/jones3/)
Learn PowerShell Toolmaking in a Month of Lunches (http://manning.com/jones4/)
PowerShell and WMI (http://www.manning.com/siddaway2/)
****************************************************************
* 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. *
****************************************************************
.Example
PS C:\> resolve-sid S-1-5-18
Name : NT AUTHORITY\SYSTEM
AccountName : SYSTEM
ReferencedDomainName : NT AUTHORITY
SID : S-1-5-18
Computername : WIN8-LAP
.Example
PS C:\> resolve-sid S-1-5-21-1199145963-1667969739-787794555-1011 -Computername chi-win8-01 -Credential globomantics\administrator
Name : CHI-WIN8-01\localadmin
AccountName : localadmin
ReferencedDomainName : CHI-WIN8-01
SID : S-1-5-21-1199145963-1667969739-787794555-1011
Computername : CHI-WIN8-01
.Example
PS C:\> resolve-sid S-1-5-18 -verbose -computername jdhit-dc01 -UseWMI
VERBOSE: Starting Resolve-SID
VERBOSE: Resolving SID S-1-5-18 on jdhit-dc01
VERBOSE: Reverting back to WMI
VERBOSE: \jdhit-dc01\root\cimv2:Win32_SID.SID='S-1-5-18'
VERBOSE: Associated account found
Name : NT AUTHORITY\SYSTEM
Accountname : SYSTEM
ReferencedDomainName : NT AUTHORITY
SID : S-1-5-18
Computername : JDHIT-DC01
VERBOSE: Ending Resolve-SID
.Link
Get-WSManInstance
Get-CIMInstance
.Link
https://jdhitsolutions.com/blog/2013/10/resolving-sids-with-wmi-wsman-and-powershell
.Inputs
Strings
.Outputs
A custom object
#>
[cmdletbinding(DefaultParameterSetName="CIM")]
Param(
[Parameter(Position=0,Mandatory=$True,HelpMessage="Enter a SID",
ValueFromPipeline,ValueFromPipelineByPropertyName)]
[ValidatePattern("^S-")]
[string]$SID,
[Parameter(ValueFromPipelineByPropertyName)]
[Alias("CN","PSComputername")]
[ValidateNotNullorEmpty()]
[string]$Computername=$env:computername,
[Alias("RunAs")]
[Parameter(ParameterSetName="CIM")]
[System.Management.Automation.Credential()]$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName="WMI")]
[Alias("wmi")]
[switch]$UseWMI
)
Begin {
Write-Verbose -Message "Starting $($MyInvocation.Mycommand)"
} #begin
Process {
Write-Verbose "Resolving SID $SID on $Computername"
#build a hashtable of paramters to splat to Get-WSManInstance
$paramHash=@{
ErrorAction="Stop"
ErrorVariable="MyError"
ResourceURI="wmicimv2/win32_SID"
SelectorSet=@{SID="$SID"}
Computername=$Computername
}
If ($Credential.username) {
Write-Verbose "Adding alternate credential for $($Credential.username)"
$paramHash.Add("Credential",$Credential)
}
Try {
#if UseWMI, use Get-WMIObject
if ($UseWMI) {
Write-Verbose "Reverting back to WMI"
Write-Verbose "\$computername\root\cimv2:Win32_SID.SID='$SID'"
[WMI]$Result = "\$computername\root\cimv2:Win32_SID.SID='$SID'"
}
else {
$result = Get-WSManInstance @paramhash
}
}
Catch {
Write-Warning "Get-WSManInstance failed to retrieve SID from $($Computername.ToUpper())"
Write-Warning $myError.ErrorRecord
#bail out
Return
}
<#
if there is no account name then the SID was not resolved, but there was
no error. The query will still succeed and write an object to the pipeline
but it won't have any useful information. Only write the result to the pipeline
if there is an associated account, otherwise an exception will be thrown.
#>
if ($result.AccountName) {
Write-Verbose "Associated account found"
$result |
Select @{Name="Name";Expression={"$($_.ReferencedDomainName)$($_.AccountName)"}},
Accountname,ReferencedDomainName,SID,
@{Name="Computername";Expression={$Computername.ToUpper()}}
}
else {
Write-Verbose "Failed to resolve SID. This is the result"
Write-Verbose $($Result | Out-String)
Throw "Failed to resolve SID $SID on $($Computername.ToUpper())"
}
} #process
End {
Write-Verbose -Message "Ending $($MyInvocation.Mycommand)"
} #end
} #close function Resolve-SID
我认为在基于注释的帮助、内部注释和详细消息之间,您应该能够理解该函数是如何工作的。现在您拥有了多种解决 SID 的技术。在本地查询、使用 [WMI] 或查询 Win32_Account 类的 SID 在性能方面应该足够了。但在远程,使用 [WMI] 或 Get-WSManInstance 比查询和过滤要快得多。使用 Get-WMIboject 或 Get-CIMInstance 需要 600-750 毫秒,而 [WMI] 方法大约需要 200 毫秒,而使用 Get-WSManInstance 需要 150 毫秒。
我希望您决心不再让 SIDS 成为您的障碍。
猜你还喜欢
- 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