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
$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
}
}
}
Can I see the first three lines of the CSV files being imported?
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;
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;
I do not have an Exchange server online right now. Can you sent me your code and data file.
Jason@MCTExpert.com