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

Sticky Key problem between Windows Server 2012 and LogMeIn

This week I instructed my first class using Windows Server 2012 accessed via LogMeIn and discovered a Sticky Key problem every time you press the Shift key. Here is my solution to resolve this.  First off, in the Preferences of LogMeIn for the connection to the Windows Server, click General . Change the Keyboard and mouse priority to Host side user and click Apply at the bottom. On the Windows 2012 server, open the Control Panel – Ease of Access – Change how your keyboard works . Uncheck Turn on Sticky Keys . Click Set up Sticky Keys . Uncheck Turn on Sticky Keys when SHIFT is pressed five times . Click OK twice. If you are using Windows Server 2012 as a Hyper-V host, you will need to redo the Easy of Use settings on each guest operating system in order to avoid the Sticky Key Problem. Updated Information: March 20, 2013 If you continue to have problems, Uncheck Turn on Filter Keys .

With the AD Recycle Bin Turned on, What Happens when you Create a User Account with a Password that does not meet the Password Policy?

This was an interesting observation from one of my Windows Server 2012 classes.  While working with the AD Recycle bin in a lab, one of my students discovered some interesting accounts that were created. When he created user accounts that did not meet password complexity requirements, an account is temporarily made and then deleted.  When a new password is provided that meets the password requirements, then a new account is made. We discovered this in two places.  First off in the Active Directory Administrative Center.  This is what caused the initial confusion.  Take a look.  This is in the Deleted Objects OU. You can see multiple deleted accounts for Test2 and one for Test3.  Test3 is a valid, functioning user account.  Using the PowerShell command Get-ADObject –IncludeDeletedObjects –Filter * –Properties ObjectSID we can see that indeed, two accounts were created, with one of them deleted. Notice the RID portion of the SID is different. ...

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...