Problem changing msExchHideFromAddressLists flag in Powershell/C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I need to set or clear the msExchHideFromAddressLists flag for each Exchange user based on data read from our LDAP server. Rather than reading several thousands user records, Im taking a single user (sAMAccountName) and attempting to change the flag
via a Powershell stream created in C# as shown in the code below. Before I get o this point, Ive already created the runspace and added it to the powershell object. And its already been used prior to the snippet below. The snippet generates no
errors and does not change the value of the msExchHideFromAddressLists flag. Ive tried it with and without adding the DOMAIN value and Ive tried it with a completely invalid sAMAccountName and still no errors are reported. That suggests to me
that Im getting errors but not seeing them. How can I dig deeper to find the error? Any ideas?<br/>


Hashtable ferpaStatus = new Hashtable();<br/>
ferpaStatus.Add("rmeinel", false);<br/>
//getStudentFERPAStatus(ldapConn);<br/>
<br/>
IDictionaryEnumerator ferpaEnum = ferpaStatus.GetEnumerator();<br/>
while (ferpaEnum.MoveNext())<br/>
{<br/>
String uid = (String)ferpaEnum.Key;<br/>
Boolean value = (Boolean)ferpaStatus[uid];<br/>
<br/>
Command getMailbox = new Command("Get-Mailbox");<br/>
getMailbox.Parameters.Add(new CommandParameter("Verbose"));<br/>
getMailbox.Parameters.Add(new CommandParameter("Identity", "CATNET\" + uid));<br/>
<br/>
powershell.Commands.AddCommand(getMailbox);<br/>
<br/>
Command setMailbox = new Command("Set-Mailbox");<br/>
setMailbox.Parameters.Add(new CommandParameter("Verbose"));<br/>
setMailbox.Parameters.Add(new CommandParameter("HiddenFromAddressListsEnabled", value));<br/>
<br/>
// Console.WriteLine(setMailbox.Parameters);<br/>
powershell.Commands.AddCommand(setMailbox);<br/>
<br/>
try<br/>
{<br/>
Collection<PSObject> results = powershell.Invoke();<br/>
<br/>
if (powershell.Streams.Error.Count > 0)<br/>
<br/>
foreach (VerboseRecord obj in powershell.Streams.Verbose)<br/>
{<br/>
results.Add(new PSObject((object)obj));<br/>
}<br/>
<br/>
StringBuilder stringBuilder = new StringBuilder();<br/>
foreach (PSObject obj in results)<br/>
{<br/>
stringBuilder.AppendLine(obj.ToString());<br/>
Console.WriteLine(obj.ToString());<br/>
}<br/>
<br/>
if (stringBuilder.Length > 0)<br/>
{<br/>
eventLog.WriteEntry("Get/Set-Mailbox: " + stringBuilder.ToString());<br/>
powershell.Streams.Error.Clear();<br/>
}<br/>
<br/>
continue;<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
eventLog.WriteEntry("Get/Set-Mailbox: " + ex.Message);<br/>
powershell.Streams.Error.Clear();<br/>
continue;<br/>
<br/>
}<br/>
}<br/>
<br/>
<br/>
Thanks,<br/>
Rob


<a name="x_reply " title="Reply

View the full article
 
Back
Top