Skip to main content

How to handle attributes with hyphens in PowerShell

Some attributes that you can pull from Active Directory may have a hyphen in them.  That makes them a bit difficult to work with in PowerShell.  When PowerShell sees the hyphen, it assumes that you just put a cmdlet in the wrong place.  To handle a hyphenated attribute, you need to rename that property.  For this example, I am going to use the msDS-ResultantPSO.  Take a look at the code below.

$UserList = Get-AdUser -filter * -property msDS-ResultantPSO | Select name, @{Name="ResultantPSO";Expression={$_."msDS-ResultantPSO"}}


The @ symbol tells us we are about to rename a property.  In the first section inside double quotes, we declare the new name of the property.  In the Expression portion, we tell PowerShell what attribute we want to rename.  Notice we use the $_. to tell PowerShell to look at the current object passed to it for this attribute.  From here on out, this property is now referred to as msDsResultantPSO.  This is now an attribute that PowerShell can use.

Comments

Purish Dwivedi said…
I am getting below mentioned error while executing the bold part in the script. Please help me to resolve this. A positional parameter cannot be found that accepts argument '-DisplayName'. + CategoryInfo : InvalidArgument: (:) [Set-MailContact], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Set-MailContact Hers is the script

$LogFile = ‘C:\temp\Test_Purish.log’
$userscsv = Import-Csv c:\temp\user.csv;

foreach ($user in $userscsv) {
echo $user.ACTION;
echo '--------------------------';
$userMap=@{
#"ACTION"="$($user.ACTION)";
"NTDomainUID"="$($user.NTDomainUID)";
"DisplayName"="$($user.DisplayName)";
"KnownasGivenName"="$($user.KnownasGivenName)";
"KnownasSn"="$($user.KnownasSn)";
"Knownasinitial"="$($user.Knownasinitial)";
"DivisionName"="$($user.DivisionName)";
"DivisionCode"="$($user.DivisionCode)";
"C"="$($user.C)";
"PostalAddress"="$($user.PostalAddress)";
"city"="$($user.city)";
"St"="$($user.St)";
"ZipCode"="$($user.ZipCode)";
"TelephoneNumber"="$($user.TelephoneNumber)";
"EfaxNumber"="$($user.EfaxNumber)";
"Mobile"="$($user.Mobile)";
"Pager"="$($user.Pager)";
"DepartmentCode"="$($user.DepartmentCode)";
"DepartmentName"="$($user.DepartmentName)";
"Title"="$($user.Title)";
"physicaldeliveryofficename"="$($user.physicaldeliveryofficename)";
"personalAssistantDN"="$($user.personalAssistantDN)";
"CDStatus"="$($user.CDStatus)";
"utcManagerDN"="$($user.utcManagerDN)";
"othertelephone"="$($user.othertelephone)";
"exportcontrol"="$($user.exportcontrol)";
"utcLegacyExchangeDN"="$($user.utcLegacyExchangeDN)";
"alternatedivisioncode"="$($user.alternatedivisioncode)";
}
foreach($eachKey in $userMap.GetEnumerator())
{
<#echo $eachKey.key
echo $eachKey.value#>
if ($user.ACTION -eq "CREATE")
{
#Create new mail contact
}
elseif($user.ACTION -eq "MODIFY")
{
if($eachKey.value.StartsWith("ADD"))
{
$attrKey = $eachKey.key
$attrVal = $eachKey.value.Substring(4)
#Set mail contact
#Set-MailContact -Identity $Alias -$eachKey.key '$eachKey.value';
}
elseif($eachKey.value.StartsWith("REPLACE"))
{
$uniqueKey=$user.NTDomainUID
$attrKey = $eachKey.key
$attrVal = $eachKey.value.Substring(8)
echo $attrKey;
#Set mail contact
Set-MailContact -Identity $uniqueKey -$attrKey $attrVal;
}
if($eachKey.value.StartsWith("REMOVE"))
{
$attrVal = $eachKey.value.Substring(7)
#Set mail contact
}
Write-Output "$attrVal" | Out-File $LogFile -append
}
}

}
Purish,

Can I see the first three lines of the CSV files being imported?
Purish Dwivedi said…
Thank you for your prompt response.
Here are the first three lines.

$s = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://UTCExch01.utctest.com/PowerShell/; Import-PSSession $s -AllowClobber;

$LogFile = ‘C:\temp\Test_Purish.log’
$userscsv = Import-Csv c:\temp\user.csv;
Purish, I believe the problem may be in the CSV file, not the PowerShell code. May I see the first 3 lines of the data file C:\Temp\User.csv
Purish Dwivedi said…
Here is the csv data. It has only one record and I am updating only displayName.

Action,NTDomainUID,DisplayName, KnownasGivenName, KnownasSn, Knownasinitial, DivisionName, DivisionCode, C, PostalAddress, city, St, ZipCode, TelephoneNumber, EfaxNumber, Mobile, Pager, DepartmentCode, DepartmentName, Title, physicaldeliveryofficename,personalAssistantDN, CDStatus, utcManagerDN, othertelephone, exportcontrol, utcLegacyExchangeDN, alternatedivisioncode
MODIFY,p1235,REPLACE~Sunil Mishra SDG

The actual issue is within this block. I am able to calculate the value of attrKey as displayName. But it is throwing above mentioned error when set mail contact cmdlet runs. Please help, I am a newbie.
$uniqueKey=$user.NTDomainUID
$attrKey = $eachKey.key
$attrVal = $eachKey.value.Substring(8)
echo $attrKey;
#Set mail contact
Set-MailContact -Identity $uniqueKey -$attrKey $attrVal;
Purish,

I do not have an Exchange server online right now. Can you sent me your code and data file.

Jason@MCTExpert.com

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.