简体   繁体   English

我想了解如何从 Get-GPO 捕获 ID 而不需要额外的“Id :”行

[英]I would like to learn how capture the ID from Get-GPO without the extra lines of “Id : ”

I am not finding the way to do this right.我没有找到正确的方法。 I would like to learn how capture the ID from Get-GPO without the extra lines of "Id : ".我想了解如何从 Get-GPO 捕获 ID,而无需额外的“Id :”行。 When I was trying to remove the first part;当我试图删除第一部分时; I learned I was working with a "BaseType of System.Object" and not array.我了解到我正在使用“System.Object 的 BaseType”而不是数组。 So I have not found any easy way to just remove it or select on the ID it's self without the extra text.所以我还没有找到任何简单的方法来删除它或在没有额外文本的情况下选择它自己的 ID。 Thank you for you help.谢谢你的帮助。 I do know that "Default Domain Policy" ID is always the same ID) Here is a sample of what I trying to run and sample of what I would like to get.我确实知道“默认域策略”ID 始终是相同的 ID)这是我尝试运行的示例和我想要获得的示例。

$test = Get-GPO "Default Domain Policy" | select id
$test

---results--- - -结果 - -

Id                                                                                                                                                                                                       
--                                                                                                                                                                                                       
31b2f340-016d-11d2-945f-00c04fb984f9

---results I am looking for---- ---我正在寻找的结果----

31b2f340-016d-11d2-945f-00c04fb984f9 

This is simple enough... In your Select command use the parameter -ExpandProperty (I always just use -Expand for short)这很简单......在你的Select命令中使用参数-ExpandProperty (我总是只使用-Expand简称)

PS C:\windows\system32> $test = Get-GPO "Default Domain Policy" | select -Expand id | Select -Expand GUID
PS C:\windows\system32> $test

31b2f340-016d-11d2-945f-00c04fb984f9 

The simple answer is:简单的答案是:

$test = Get-GPO "test1" | select -ExpandProperty id
$Test = $Test.Guid
$test

Edit: Wow, I left this hanging for 2 years?编辑:哇,我把它挂了 2 年? My bad... To expand on removing blank entries, a GPO object has several properties.我的不好... 为了扩展删除空白条目,GPO 对象有几个属性。 One of those properties is Id , which is of the type [System.Guid] , and has a property of Guid , which was the end goal here, to get the GUID of the GPO.其中一个属性是Id ,其类型为[System.Guid] ,并且具有Guid属性,这是此处的最终目标,用于获取 GPO 的 GUID。 The Guid property is of type [System.String] , and that definitely has a .Trim() method. Guid属性的类型为[System.String] ,并且肯定有一个.Trim()方法。 Now, if you have several items, and not all of them have an Id property, or not all of the Id properties have a Guid property, and you iterate through them you will end up with blanks when you output the array.现在,如果您有多个项目,并且并非所有项目都具有Id属性,或者并非所有Id属性都具有Guid属性,并且您遍历它们,则在输出数组时最终会得到空白。 To avoid that we first iterate through the Id properties, and then for each of those we iterate through Guid properties, and lastly remove extra whitespace in the form of empty strings, null values, and extraneous new lines in a multi-line string (we really shouldn't get any of that last one since these are GUIDs, but I'm just being thorough here).为了避免这种情况,我们首先遍历Id属性,然后遍历每个Guid属性,最后以空字符串、空值和多行字符串中无关的新行的形式删除额外的空格(我们真的不应该得到最后一个,因为这些是 GUID,但我只是在这里彻底)。

This time around I will use the shorthand % instead of Select -Expand .这一次我将使用简写%而不是Select -Expand % is actually an alias for ForEach-Object , and basically what I'm doing here is telling it for each GPO, give me the Id property's value, and then for each of those give me the Guid value. %实际上是ForEach-Object的别名,基本上我在这里做的是告诉它每个 GPO,给我 Id 属性的值,然后为每个 GPO 给我 Guid 值。

$test = Get-GPO "Default Domain Policy" | % id | % GUID | ?{!([string]::IsNullOrWhitespace($_))} | %{$_.Trim()}

When I tested this using Get-GPO -all , and output the result I got a wall of just over 1500 GUIDs.当我使用Get-GPO -all测试并输出结果时,我得到了超过 1500 个 GUID 的墙。 No whitespace, no blank lines, just lots and lots of GUIDs.没有空格,没有空行,只有大量的 GUID。

I came across this problem as well and also discovered the same behaviour.我也遇到了这个问题,也发现了同样的行为。
If you use -expandproperty ID (or (Get-GPO "Default Domain Policy).Id ) you will get GUID as your header and no longer ID . Crazy PS...如果您使用-expandproperty ID (或(Get-GPO "Default Domain Policy).Id ),您将获得GUID作为您的标题,而不再是ID 。疯狂的 PS ...

So to me this looked nested.所以对我来说这看起来是嵌套的。 If you want it "clean" like me because you also don't like trimming as suggested above you can try two options to use in this case:如果你像我一样希望它“干净”,因为你也不喜欢上面建议的修剪,你可以尝试在这种情况下使用两个选项:

  1. ((Get-GPO "Default Domain Policy").Id).Guid - my personal preference ((Get-GPO "Default Domain Policy").Id).Guid - 我的个人偏好
  2. (Get-GPO "Default Domain Policy") | select id -ExpandProperty id | select guid -ExpandProperty guid (Get-GPO "Default Domain Policy") | select id -ExpandProperty id | select guid -ExpandProperty guid or a mixture of both. (Get-GPO "Default Domain Policy") | select id -ExpandProperty id | select guid -ExpandProperty guid或两者的混合。

Either one just gives back your GUID as a string.任何一个都只是将您的 GUID 作为字符串返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM