PowerShell: Convert Object to String

We have Exchange information storages that start with UsersA-B, UsersC-D, etc., and then some of them are not included in this naming convention.

$allIS = Get-MailboxDatabase | Where { $_.name -notlike "*Users*" } | Select Identity 

I will look at the current user information store, and then try to make a comparison with the $ allIS array. If appropriate, do some action.

For example, outputting the value $ allIS [0] returns @{Identity=MSCCR\CEO\CEO} .

I would like to throw these converted strings into another array and then do a comparison. This will have a dynamic list of information stores for comparison. But perhaps this is not the best and most effective way. What is the best way to try to make this comparison, since now I am comparing apples to oranges here?

+7
source share
3 answers

It is hard to say whether this can be optimized without seeing the second part ...

But it's pretty easy to get a flat array of identities. Either use -ExpandProperty to select, or use foreach { $_.Identity } instead of select:

 $allIS = Get-MailboxDatabase | ? { $_.name -notlike "*Users*" } | select -expand Identity $allIS = Get-MailboxDatabase | ? { $_.Name -notlike '*Users*' | foreach { $_.Identity} 
+9
source

PowerShelly Method

 $isNames = @() $allIS = Get-MailboxDatabase | Where { $_.name -notlike "*Users*" } | Select Identity | %{ $isNames += $_.name } 

It directs the output to the foreach using % instead.

More procedural way

 $isNames = @() foreach ($is in $allIS) { $isNames += $is.identity } 

This gives you a simple array of storage names only as strings instead of objects.

+1
source

When you try to use the "property dereference operator". for a property that is not a member of the array class, it will dereference each element of the array.

 $allIS = (Get-MailboxDatabase | ? { $_.name -notlike "*Users*" }).Identity 
0
source

Source: https://habr.com/ru/post/918000/


All Articles