Showing posts with label multi-valued attributes. Show all posts
Showing posts with label multi-valued attributes. Show all posts

Tuesday, 28 May 2013

Eliminate Ellipsis In Powershell


When trying to display the result of some queries on screen, we may find that some of the output is truncated by ellipsis i.e. “ . . .” , often the case with multi-valued attributes. An example of the ellipsis can be found when retrieving the thread details of any process. The image below shows the output when querying the thread details for system process:

Get-Process System | select Name,Threads




The reason that the thread column gets truncated at 4 values is because of $FormatEnumerationLimit. Help about the variable can be found under About_Preference_Variables. The value is set at 4 by default in the powershell console and the value is 16 in the exchange shell.











The entire thread details can be listed by modifying the above command to:

Get-Process System | select Name,Threads | ft –Wrap –Autosize



An alternate method to display the threads would be to use the ‘ExpandProperty’ switch in Select-Object cmdlet:

get-process system | Select-Object Name -ExpandProperty Threads | ft Name, ID –AutoSize




The easiest method would be to change the default value of the $FormatEnumerationLimit.

The default value has been altered to 80 which displays the whole list of system threads on screen:



Sunday, 12 May 2013

Working With Multi-Valued Attributes - Part 2

Part 1 of the series dealt with 'set' cmdlets with respect to multi-valued attributes.

Now in part 2, we are going to look at the challenges faced when retrieving multi-valued attributes using 'Get' cmdlets and workarounds for them. If the number of the values stored in the attribute is high, then displaying the entire list on screen is not possible as powershell tends to append the list with “. . . “ implying that there are more values in the property than displayed on screen.

Get-DistributionGroup "Test DL" | fl Name, AcceptMessagesOnlyFrom 



Exporting the values to a CSV/ text file is also a concern, since the actual values in the attribute are not exported to the file as seen below.

Get-DistributionGroup "Test DL" | Select-Object Name, AcceptMessagesOnlyFrom | Export-Csv "C:\DL.csv" -Notype







The  pipeline can be manipulated to export all the attributes as shown below:

 Get-DistributionGroup "Test DL"  | Select-Object Name,@{Name="AcceptMessagesOnlyFrom";Expression={[string]::join(";",$_.AcceptMessagesOnlyFrom)}} | Export-Csv c:\DL.csv –NoTypeInformation


The output is shown below : 

The text in image above has been altered to confine within the image size to display all the values contained in the CSV file.

Thursday, 9 May 2013

Working With Multi-Valued Attributes in Powershell - Part 1

Multi-valued attributes are attributes that can contain more than one value. Email-addresses of users, Accept messages only from attribute of Distribution groups are some examples of multi-valued attributes.  Modifying or retrieving single-valued attributes through shell are quite simple whereas caution needs to be exercised when dealing with multi-valued ones.  Powershell, by default, tends to replace a value instead of appending when Set commands are used. So multi-valued properties can be accidentally over-written..

Consider a distribution group ‘Test DL’ which is set to accept messages only from ‘User1’, ‘User2’ and ‘User3’. We can query the attribute using the command below :

Get-DistributionGroup "Test DL" | fl  Name,AcceptMessagesOnlyFrom




Now, when ‘User 4’ needs to be added to the same property we execute the command below:

Set-DistributionGroup "Test DL" -AcceptMessagesOnlyFrom 'User4'

However, when we execute the first command again, we find that the existing users have been replaced by ‘User4’




To avoid replacing the existing users, the below command can be used instead:

Set-DistributionGroup "Test DL" -AcceptMessagesOnlyFrom @{Add = "User4"}



To remove ‘User4’ from the accept messages only from parameter, the command can be altered to:

Set-DistributionGroup "Test DL" -AcceptMessagesOnlyFrom @{Remove = “User4”}




A single command can be used to remove ‘User1’ and add ‘User5’ to the attribute:

Set-DistributionGroup "Test DL" -AcceptMessagesOnlyFrom @{Remove = "User1" ; Add="User5"}



The general syntax would be:

Set-cmdlet ‘Identity ’-MultivaluedAttributeSwitch  @{Add = “value1”,”value2”,”value3” ; Remove = “value4”,”value5”}