How to Trim String?

Thanks for the advice. If it’s that simple I’ll be kicking myself as I’m sure I’ve tried that before.

I’ve taken 2 Powershell courses prior and have done BASH scripting as well. I’ve recently been put in this situation where I’m the only one that can even come close to Powershell. Unfortunately, you can only listen so much in a semester and Powershell’s functionality spans much further than a couple of courses.

Within the -eq -contains functions it seems ridiculous, to me, that a split function exists within a pool of switches that would otherwise return True or False, thus the confusion.

Powershell is something I enjoy and am genuinely curious about learning and growing with. At the same time, it’s probably one of the most obtuse languages at times to me.

I appreciate your feedback and will take note of my lack of fundamentals.

Hmmm … Did you try my code suggestion? Did it work?

Commiserations. I can relate to this, and the late nights of learning new technologies for the next conference call. Hopefully, this forum will serve you well; and in time, you can give back your experiences.

Learning how to select array members should come after PowerShell basics as terminology can throw off the conversation. “Sort out the non-security categories” would conjure other types of data actions for folks. Plow through blogs and videos to find good resources that “teach instead of show”. As I am an INTJ personality, I checkout books from the library and buy the good ones.

The knowledge and skills you might have from other programming languages should speed you along with PowerShell. Once you get a foundation in the syntax, you can build on that to learn when to use certain loops, cmdlets, custom functions, and so forth. For example, += becomes costly when working with large data sets (!! Active Directory !!), and an alternative might be required. Before too long, you might be helping someone decipher something like the following:

$x = New-Object 'object[,]' 2,3;
$x[0,0] = ('up','charm','top')
$x[0,1] = ('Alice','Bob')
$x[0,2] = ('electron','muon','tau')
$x[1,0] = ('down','strange','bottom')
$x[1,2] = ('e-neutrino','m-neutrino','t-neutrino')

for($i=0;$i -lt $x[0,0].length;$i++)
{
  Write-Host $x[0,$i] $x[1,$i]
}

–If you like maths, that’s a 2x3 matrix. The clever eye will notice something collectively funky about that matrix, too.

Now, I threw that bit of over-the-top-not-your-solution code in for a double purpose (triple—when you consider my ego). It shows what PowerShell is capable of in terms of data modeling. It might look odd, but it’s only a grid overlaid with some PowerShell populations. We return the grid elements using a FOR loop (there’s that .length method again).

And this brings me to:

I’m only trying to obtain sec.business and filter out the others in order to inject it into a variable that I will use later for an ADGroup and ADGroupMember cmdlet.

Give me some time to figure out how to present this while you chase down some syntax and terms.

Observe, research, smash with a hammer, analyze; observe, research, smash…

See also

1 Like

Thank you,

Looks like the Where-Object checked out and produces only as stated. Now just to array them and thats it.

Please don’t get me wrong. May I ask what your native language is?

What does that mean? There already is an array in my code suggestion. It’s named “$IdentityReferenceValueList”.

Thank you for your insights and feedback. I’ve been able to grasp the ideas of elements and arrays quickly, my main concern was finding out ways to parse information on outputs in a format that would work for my variables.

I’ve been looking for puzzles and so on to solve on my free brain time away from this project in order to better familiar myself with the language that I’m sure will come in time.

I really appreciate both yours and @Olaf guides as they’re helpful materials for me to understand the context of a lot of commands. I’ll have a look at that Powershell book.

I speak English as a first language currently, French second, some German and some Hungarian

Was raised on French.

The $IdentityReferenceValueList is what I’m looking, yes.

Great. Thanks. :+1:t4:

$array=@()
(Get-ChildItem -Path PSDrive:\).FullName |
foreach{

$array += $_
}



foreach ($i in $array){
$i
(get-acl $i).Access | 
Where-Object AccessControlType -eq Allow |
ft identityreference,filesystemrights,accesscontroltype 

if ((get-acl $i).Access |Where-Object IdentityReference -Value "*Sec.*" -like ){

$IdentityReferenceValueList =
Get-Item -Path $i |
Get-Acl |
Select-Object -ExpandProperty Access |
Select-Object -ExpandProperty IdentityReference |
  ForEach-Object { ($_ -split '\\')[1] | 
   Where-Object {$_ -like 'sec.*'} 
    #Out-String
}         
}
Write-Host ":::Security Groups Associated:::"
write-host $IdentityReferenceValueList
Get-ADGroup -Identity $IdentityReferenceValueList | Select-Object DistinguishedName,Name,SAMaccountName,GroupCategory
Get-ADGroupMember -Identity $IdentityReferenceValueList | ft name, SamAccountName, distinguishedName

}

So, after your help, I’ve been successfully able to call the filtered output of “sec.groups” into my Get-ADGroup and AD-GroupMember output.

Looking from the output, it seems that it is able to, in some instances, successfully complete and others not so much with an error showing as :

Get-ADGroup : Cannot convert ‘System.Object’ to the type
‘Microsoft.ActiveDirectory.PSGroup.ADGroup’ required by parameter ‘Identity’. Specified method is not supported.

I’m thinking here that there’s an issue with the actual object being read as an input variable from the Get-ADGroup and Get-ADGroupMember commands. I’ve used Out-String as an ending pipe within the filtering if statement to no success.

I’ve read that Get-ADGroup and Get-ADGroupMember commands are expecting a Singleton entry from the Identity option available but I believe that it shouldn’t be an issue since there’s only one variable cycling through at a time.

Prior to entering both AD commands, they have been tested on a secondary powershell for full functionality and have worked which is why I believe it to be a variable / entry issue.

Updated : added quotation marks around the variable, cleared up the error. My bad.

“$IdentityReferenceValueList” would be the replacement.

1 Like
$array=@()
(Get-ChildItem -Path PSDrive:\).FullName |
foreach{

$array += $_
}



foreach ($i in $array){
$i
(get-acl $i).Access | 
Where-Object AccessControlType -eq Allow |
ft identityreference,filesystemrights,accesscontroltype 

if ((get-acl $i).Access |Where-Object IdentityReference -Value "*Sec.*" -like ){

$IdentityReferenceValueList=

Get-Item -Path $i |
Get-Acl |
Select-Object -ExpandProperty Access |
Select-Object -ExpandProperty IdentityReference |
    ForEach-Object { ($_ -split '\\')[1] | 
      Where-Object {$_ -like 'sec.*'}
    
}         
}
Write-Host ":::Security Groups Associated:::"
Write-Host "$IdentityReferenceValueList"
Get-ADGroup -Identity "$IdentityReferenceValueList" | Select-Object DistinguishedName,Name,SAMaccountName,GroupCategory
Get-ADGroupMember -Identity "$IdentityReferenceValueList" | ft name, SamAccountName, distinguishedName

}

This will likely be the last post for this topic and I thank everyone for this help in educating me.

For the following script, my variable $IdentityReferenceValueList is currently containing ALL of the instances of sec.group instead of one at a time. When taking this data and inputting it towards ADGroup and ADGroupMembers, it is resolving the first sec.group entry but for any afterwards, it will come up with an error showing that the -Identity parameter in either ADLookup cmdlet is limited to one input at a time.

This is steering me towards another foreach loop that I must create with $IdentityReferenceValueList. I’ve attempted to start and += the instance of it after the if statement , after the filtering of ForEach/WhereObj without any success.

I’ve also tried adding $IdentityReferenceValueList += $_ after the Where-Object {$_ -like ‘sec.*’} command without any success.

Where should I input this loop?

$array=@()
(Get-ChildItem -Path PSDrive:\).FullName |
foreach{

$array += $_
}



foreach ($i in $array){
$i
(get-acl $i).Access | 
Where-Object AccessControlType -eq Allow |
ft identityreference,filesystemrights,accesscontroltype 

if ((get-acl $i).Access |Where-Object IdentityReference -Value "*Sec.*" -like ){

$IdentityReferenceValueList=

Get-Item -Path $i |
Get-Acl |
Select-Object -ExpandProperty Access |
Select-Object -ExpandProperty IdentityReference |
    ForEach-Object { ($_ -split '\\')[1] | 
      Where-Object {$_ -like 'sec.*'}|
        Where-Object {$_ -notlike 'sec.No*'}
    
}
Write-Host ":::Security Groups Associated:::"
Write-Host "$IdentityReferenceValueList"
foreach($y in $IdentityReferenceValueList){
Get-ADGroup -Identity "$y" | Select-Object DistinguishedName,Name,SAMaccountName,GroupCategory
Get-ADGroupMember -Identity "$y" | ft name, SamAccountName, distinguishedName          
}
}

}

After reading through the fundamentals of arrays and foreachloops, I came to the conclusion that since the $IdentityReferenceValueList was containing multiple elements, that I could call instances of those elements individually by using a placeholder variable ($y) and pass that on to the Get-ADGroup and Get-ADGroupMembers commands successfully!

Thank you so much for your help, I appreciate all of your patience and will continue to read on much needed materials. I’m also looking for more exercises to do, if anyone has an idea of where I can find some basic exercises I would very much welcome it.

Sorry if I bothered anyone else. First steps into a Powershell role.

1 Like

That is awesome, good job! You may look up PSKoans and PSHunter.

1 Like

Do not sorry for learning something new. :wink:

You may continue to improve with this:

4 Likes