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

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.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the mapped drive. If the