简体   繁体   English

从Powershell中删除字符串中的多余信息

[英]Remove excess information from string in Powershell

Good morning. 早上好。 What I am trying to accomplish is easier said than done. 我想要完成的事情说起来容易做起来难。 With the code below, I return the following: 使用下面的代码,我返回以下内容:

Group is owned by: @{samaccountname=aduser1} 集团拥有:@ {samaccountname = aduser1}

Group is owned by: @{samaccountname=aduser2} 集团拥有:@ {samaccountname = aduser2}

Here's the code: 这是代码:

$GroupList = get-content "Z:\text.txt"

ForEach($Entry in $GroupList){

$SubGroups = @()

$AllMembers = @()

$strGroupOwner = Get-ADGroup -identity $Entry -Properties ManagedBy | select managedby 

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select samaccountname

 "Group is owned by: " + $strOwnerName

I simply need to remove '@{samaccountname=' and the '}' at the end from my string $strOwnerName before passing it through to the next step to make my resume something closer to: 我只需要从字符串$ strOwnerName中删除'@ {samaccountname ='和'}',然后再将其传递到下一步,使我的简历更接近:

Group is owned by: aduser1 集团归:aduser1所有

Group is owned by: aduser2 集团归:aduser2所有

All I was able to find on Google was removing 'White Space'. 我在谷歌上找到的只是删除了“白色空间”。 Any help or reading material would be most appreciated. 任何帮助或阅读材料将是最受欢迎的。

Thanks! 谢谢!

In your code $strOwnerName is a PSCustomObject . 在你的代码中, $strOwnerName是一个PSCustomObject To convert it to a string change 将其转换为字符串更改

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select samaccountname

to

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select -ExpandProperty samaccountname

or 要么

$strOwnerName = (get-aduser -identity $strGroupOwner.managedby).samaccountname

Use the -expandproperty parameter for select-object . select-object使用-expandproperty参数。

$strOwnerName = get-aduser -identity $strGroupOwner.managedby -properties samaccountname |select-object -expandproperty samaccountname

Also note that I've used select-object instead of the alias select ; 另请注意,我使用了select-object而不是别名select ; aliases should be avoided in scripts because they make an assumption about the execution environment which may not always be true. 应该避免在脚本中使用别名,因为它们对执行环境做出了假设,这可能并不总是正确的。

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

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