使用 PowerShell 将 GUID 字符串转换为 octetBytes
我有一个 powershell 脚本,可以按大小输出所有 Exchange 2003 邮箱。
$computers = "vexch01","vexch02"
foreach ($computer in $computers) {
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object -desc Size | select-object MailboxDisplayName,StoreName,@{Name="Size/Mb";Expression={[math]::round(($_.Size / 1024),2)}}, MailboxGUID | Export-Csv -notype -Path $computer.csv
}
目前,这将 MailboxGUID 输出为字符串类型 GUID(例如 {21EC2020-3AEA-1069-A2DD-08002B30309D})。我想通过这个在AD中查找用户,但是AD将它们存储为octetBytes格式。
我发现了一些 powershell 函数将进行转换,但仅当花括号被删除时。 Guid.ToString 方法应该提供这个,但我无法得到它在上面工作。
但是,如果我能弄清楚如何做到这一点,Guid. ToByteArray 方法可能会让我更接近。
有人破解过这个吗?
更新:到目前为止的答案帮助我编写了一个函数,将 mailguid 转换为通过 LDAP 搜索的正确格式。但是,我现在无法在脚本中使其正常工作。这是我更新的脚本:
function ConvertGuidToLdapSearchString(
[parameter(mandatory=$true, position=0)]$Guid
)
{
$guid_object = [System.Guid]$Guid
($guid_object.ToByteArray() | foreach { '\' + $_.ToString('x2') }) -join ''
}
# Gets data through WMI from specified Exchange mailbox servers
$servers = "vexch01","vexch02"
foreach ($server in $servers) {
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object -desc Size | select-object MailboxDisplayName,StoreName,@{Name="Size/Mb";Expression={[math]::round(($_.Size / 1024),2)}}, @{Name="LDAP Guid";Expression={ConvertGuidToLdapSearchString(MailboxGUID)}} | Export-Csv -notype -Path $server.csv
}
我不知道为什么使用 select-object
中的函数 @{Name="LDAP Guid";Expression={ConvertGuidToLdapSearchString(MailboxGUID)}}< /代码> 不起作用。
是否有另一种方法可以在 select-object
中使用此函数来给出字符串?
I have a powershell script which outputs all Exchange 2003 mailboxes by size.
$computers = "vexch01","vexch02"
foreach ($computer in $computers) {
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object -desc Size | select-object MailboxDisplayName,StoreName,@{Name="Size/Mb";Expression={[math]::round(($_.Size / 1024),2)}}, MailboxGUID | Export-Csv -notype -Path $computer.csv
}
Currently this outputs the MailboxGUID as a string type GUID (e.g. {21EC2020-3AEA-1069-A2DD-08002B30309D}). I want to look up users in AD by this, but AD stores them in octetBytes format.
I have found some powershell functions which will do the conversion but only when the curly braces are removed. The Guid.ToString method should supply this, but I can't get it to work in the above.
However, if I could figure out how to do that, the Guid.ToByteArray method might get me even closer.
Has anyone cracked this?
Update: the answers so far helped me write a function that converts the mailboxguid into the correct format for searching via LDAP. However, I now cannot get this working in the script. This is my updated script:
function ConvertGuidToLdapSearchString(
[parameter(mandatory=$true, position=0)]$Guid
)
{
$guid_object = [System.Guid]$Guid
($guid_object.ToByteArray() | foreach { '\' + $_.ToString('x2') }) -join ''
}
# Gets data through WMI from specified Exchange mailbox servers
$servers = "vexch01","vexch02"
foreach ($server in $servers) {
Get-Wmiobject -namespace root\MicrosoftExchangeV2 -class Exchange_Mailbox -computer $computer | sort-object -desc Size | select-object MailboxDisplayName,StoreName,@{Name="Size/Mb";Expression={[math]::round(($_.Size / 1024),2)}}, @{Name="LDAP Guid";Expression={ConvertGuidToLdapSearchString(MailboxGUID)}} | Export-Csv -notype -Path $server.csv
}
I'm not sure why using the function in the select-object
with @{Name="LDAP Guid";Expression={ConvertGuidToLdapSearchString(MailboxGUID)}}
doesn't work.
Is there another way of using this function in select-object
that will give the string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
结合 Andy Schneider 的回答,您可能会发现这个函数很有用:(
我认为我有一个更聪明的方法来通过向 System.Guid 添加 ScriptProperty 来做到这一点,但我似乎已经了解到您无法有效地将成员添加到结构。)
我不确定我是否理解您根据您的评论想要完成的任务,但我认为您可能刚刚遗漏了 $_。下面是一个有点人为的示例,它创建一个具有 GUID 属性的对象,然后使用 select 和 Convert-GuidToLdapSearchString 来转换格式。我希望它有帮助。
这根本不是我想象的使用该功能的方式。我预计您会使用它来创建 LDAP 搜索子句,例如:
In conjunction with Andy Schneider's answer, you may find this function useful:
(I thought I had a more clever way to do this by adding a ScriptProperty to System.Guid, but I seem to have learned that you can't effectively add members to structs.)
I'm not sure I understand what you are trying to accomplish based on your comment, but I think you may have just left out a $_. Here is a somewhat contrived example that creates an object with a property that is a GUID, then uses select and Convert-GuidToLdapSearchString to convert the format. I hope it helps.
This is not at all how I had imagined the function being used. I expected you would use it to create an LDAP search clause such as:
您是否将字符串转换为 GUID ?
Are you casting the string to a GUID ?