SOLVED

Is the use of "$psitem" correct?

Brass Contributor

Hello friends,

 

First of all, I apologize for the words, but I don't speak English and I rely on an online translator.

 

Well, I have a doubt and I believe it is of orientation. Still learning to use Powershell in the "force" ;)

 

I need - I'm trying - to extract data from fields in different commands/modules...

 

Through Get-SPOSite -Identity https://mytenet.sharepoint.com/sites/mysite I look for the Owner. In some cases it brings me the email of the user who created/is the Owner and that satisfies me.
In other cases it tells me the ID of the Group that created/originates the site.
From the ID I do the following:
Get-UnifiedGroup -Identity [Group ID] | Select ManagedBy


As I need this in a report, I started doing it like this:

Get-SPOSite -Identity https://mytenet.sharepoint.com/sites/mysite | ForEach-Object -Process {
[PSCustomObject][Ordered]@{
Url = $PSItem.Url
'Owner' = Get-UnifiedGroup -Identity [Group ID] | Select-Object -Property @{Name = 'Owner'; Expression={$PSItem.ManagedBy}}
'Usage' = $PSItem.StorageUsageCurrent
'Space' = $PSItem.StorageQuota
'Last Access' = $PSItem.LastContentModifiedDate
}
}


I wanted to understand why he always returns the Owner field to me
Url : https://mytenet.sharepoint.com/sites/mysite
Owner : @{Owner=name.user}
Usage : 1
Space: 5120
Last Access: 10/19/2022 18:15:26

 

And sometimes something like "system.collections.arraylist" or similar.

 

Thank you very much in advance for your help and guidance!!!

 

Big Hugs!!

3 Replies

@tcboeira 

 

So, the reason it looks that way is because a group can have multiple managers (or owners), which you can see when you check the type of the ManagedBy field.

 

LainRobertson_2-1666221138247.png

 

Despite the complicated type name, in reality, it's just an array of strings, meaning we can use PowerShell operators like "-join" to join all of the string members together as the example below shows.

 

LainRobertson_3-1666222207003.png

 

So, putting this together with your example code:

 

# Original line.
'Owner' = Get-UnifiedGroup -Identity [Group ID] | Select-Object -Property @{Name = 'Owner'; Expression={$PSItem.ManagedBy}}

# Changes very slightly at the end to this line.
'Owner' = Get-UnifiedGroup -Identity [Group ID] | Select-Object -Property @{Name = 'Owner'; Expression={$PSItem.ManagedBy -join ", "}}

 

Also, for someone only starting to learn PowerShell, you're doing very, very well! Keep up the great work!

 

Cheers,

Lain

Thank you very, very much for the clarification!!!

I confess that I get a little confused in the construction of the "command". And sometimes to see it working without understanding what is actually happening is even a little frustrating. although very cool

Well, I would like to share a doubt that is persistent...

When I use this way:


Get-SPOSite -Identity [My Site] | ForEach-Object -Process {
[PSCustomObject][Ordered]@{
Url = $PSItem.Url
'Owner' = Get-UnifiedGroup -Identity [ID] | Select-Object -Property @{Name = 'Owner'; Expression={ $PSItem.ManagedBy -join ", " }}
'Usage' = $PSItem.StorageUsageCurrent
'Space' = $PSItem.StorageQuota
'Last Access' = $PSItem.LastContentModifiedDate
}
}

My return in the Owner field has always been like this:
Owner : @{Owner=name1, name2, name3}

For example: If only "name1, name2, name3" came, when I import the CSV I can handle it and everything else...


IF it were such a reality,


Get-UnifiedGroup -Identity [id] | ForEach-Object -Process {
[PSCustomObject][Ordered]@{
Url = $PSItem.SharePointSiteUrl
'ID' = $PSItem.ExternalDirectoryObjectId
'Owner' = $PSItem.ManagedBy -join ", "
'uAccess' = $PSItem.WhenChanged
}
}
Success is guaranteed, but then I have difficulties mixing the results.


How do I get rid of the "@{Owner=" and "}" in the output of the first example? or rather... How do I understand the best way to do it?

Again, thank you so much for the help!!!
best response confirmed by tcboeira (Brass Contributor)
Solution

Olá Amigos...

 

Eu descobri como "se livrar" de conteúdo indesejado na tela.

No final da linha, uso uma referência ao objeto que desejo que seja exibido "| Select-Object -ExpandProperty 'NomeObjeto' " ...

 

O Script na pergunta tem esta aparência:

 

$CSV = Import-Csv .\Lista-SITEGRP.csv
$CSV. Conte
$CSV. | do proprietário ForEach-Object {
Get-UnifiedGroup -Identity $_ | ForEach-Object -Process {[PSCustomObject][Ordered]@{

Url = $PSItem.SharePointSiteUrl'Owner
' = $PSItem.ManagedBy -join ", "
'StorageQuota' = Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'StorageQuota'; Expression={ $PSItem.StorageQuota}} | Select-object -expandProperty 'StorageQuota''StorageUsageCurrent'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'StorageUsageCurrent'; Expression={ $PSItem.StorageUsageCurrent}} | Select-object -expandProperty 'StorageUsageCurrent''Template'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'Template'; Expression={ $PSItem.Template}} | Select-Object -ExpandProperty 'Template''LocaleId'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'LocaleId'; Expression={ $PSItem.LocaleId}} | Select-Object -ExpandProperty 'LocaleId'

}
}
}

 

 

Muito obrigado a todos pela vossa ajuda e atenção!!!
Grande abraço

1 best response

Accepted Solutions
best response confirmed by tcboeira (Brass Contributor)
Solution

Olá Amigos...

 

Eu descobri como "se livrar" de conteúdo indesejado na tela.

No final da linha, uso uma referência ao objeto que desejo que seja exibido "| Select-Object -ExpandProperty 'NomeObjeto' " ...

 

O Script na pergunta tem esta aparência:

 

$CSV = Import-Csv .\Lista-SITEGRP.csv
$CSV. Conte
$CSV. | do proprietário ForEach-Object {
Get-UnifiedGroup -Identity $_ | ForEach-Object -Process {[PSCustomObject][Ordered]@{

Url = $PSItem.SharePointSiteUrl'Owner
' = $PSItem.ManagedBy -join ", "
'StorageQuota' = Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'StorageQuota'; Expression={ $PSItem.StorageQuota}} | Select-object -expandProperty 'StorageQuota''StorageUsageCurrent'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'StorageUsageCurrent'; Expression={ $PSItem.StorageUsageCurrent}} | Select-object -expandProperty 'StorageUsageCurrent''Template'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'Template'; Expression={ $PSItem.Template}} | Select-Object -ExpandProperty 'Template''LocaleId'
= Get-SPOSite -Identity $_. | SharePointSiteUrl Select-Object -Property @{Name = 'LocaleId'; Expression={ $PSItem.LocaleId}} | Select-Object -ExpandProperty 'LocaleId'

}
}
}

 

 

Muito obrigado a todos pela vossa ajuda e atenção!!!
Grande abraço

View solution in original post