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

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Set up your Environment to utilize Invoke-GPUpdate

I’m sitting in O’Hare enjoying my 3 hour layover as I get ready to teach a Windows 10 class in Fort Wayne.  I’m actually prepping for my class in Fort Wayne in a few weeks, 10982 Supporting and Troubleshooting Windows 10.  This class is very much like the Windows 7 version that I used to teach so I decided to up the detail quite a bit.  In the GPO troubleshootin chapter, I’m including a demonstration on how to use PowerShell’s Invoke-GPUpdate.  I needed to make sure the environment is set up for me to be able to utilize this cmdlet.  If you read the Notes section on Invoke-GPUpdate’s help file, you will see there are three firwall rules that need to be in place:          Remote Scheduled Tasks Management (RPC)          Remote Scheduled Tasks Management (RPC-ERMAP)          Windows Management Instrumentation (WMI-IN) I decided to create a little PowerShell script to crea...

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...