Monday, April 15, 2013

Move user between domain's

Move user between domain's in the same forest PowerShell


This week I try to write PowerShell script for moving users between domain's in the same forest.At first I try to use ADMT 3.2 ,in my test environment the ADMT install on a member server.After several testing I try the RTFM approach (read the faking Manuel).From this approach I learn that if you want to script ADMT it must be install on a Domain Controller!

So I start google to find alternative then I found a Book from  O'Reilly
"Active Directory CookBook" Written byLaura E. Hunter,Robbie Allen
in the book was explain in VB for moving users between domain in the same forest.



 

 

 

 

 








Below is the Code in PowerShell that do the work enjoy


100 function GetDomainnamefromobjectDN([string]$objDN){
101      $str = $objDN.Split(",")
102      for ($i=$str.length-1 ;$i -gt -1 ;$i--)
103         {if ($str[$i].substring(0,2) -like "dc")
104            {if($strDC)
105               {$strDC = $str[$i].Split("=")[1] + "." + $strDC}
106            else
107               {$strDC = $str[$i].Split("=")[1]}
108            }
109         }
110         return $strDC
111      }
112
113 $Tou = "CN=Users,DC=London,DC=England,DC=CO,DC=UK"
114 $usrDN = "CN=Doron Zilber,CN=Users,DC=England,DC=CO,DC=UK"
115 $TargetRID = $null
116 $SourceRID = $null
117 $objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
118 $DomainList = @($objForest.Domains | Select-Object Name,RidRoleOwner)
119
120 foreach ($dom in $DomainList)
121      {if ($dom.name -eq (GetDomainnamefromobjectDN($Tou)))
122         {$TargetRID = $dom.RidRoleOwner}
123      if ($dom.name -eq (GetDomainnamefromobjectDN($usrDN)))
124         {$SourceRID = $dom.RidRoleOwner}
125      if ($TargetRID -and $SourceRID)
127         {break}
129      }
130
131 $objUsr = [ADSI]"LDAP://$SourceRID/$usrDN"
132 $MoveToOU = [ADSI]"LDAP://$TargetRID/$Tou"
133
134 # ## Command to Do the actual move
135 $objUsr.PSBase.moveto($MoveToOU)
136





Saturday, March 16, 2013

ComboBox with ToolTip to each item


When we use ComboBox Control in a form we assume certain length for the control.
In case the data is from query (Active Directory, SQL) we can't predict the item length.

And in some case the item is longer than the control. It can be solved by enlarge the ComboBox control length, or add ToolTip to Combobox so after selection you can see the full string , or to ignore long item (kidding..J). Other option (more user friendly) to add ToolTip to each item's In this case the user sees right way if is selection is the right one.

To enable tooltip for each item in the ComboBox control, you must interfering the process of drawing the items. Actually you have to build small routine to draw the item's in the dropdown list & to add to each item a ToolTip.
First we have to catch the event of drawing the item of ComboBox

Then we have to build a small routing to draw the items of combobox .

13: [System.Windows.Forms.DrawItemEventHandler] to catch the Event.
14:
15: Get the location of drawing item.
16: Set the Text for Drawing item.
18: set the color for (foreground Color) it can be name or FromArgb
          (http://msdn.microsoft.com/en-us/library/system.windows.media.colors.aspx).
19: Draw the background for the item.
20: Set the fore color to a brush object.
21: Set the font for Drawing items.
22: Drawing the Item use DrawString method.
24: condition to check if the user is go over the drop down item list
          ($ComboBox1.SelectedIndex -gt -1)
          and that the mouse move to other item .
26: Set the ToolTip for the current select item.

To download the example press here.

RTL ComboBox with ToolTip:


In case your language is RTL Hebrew, Arabic. it's more complicate but it's possible. The complicity comes from the ToolTip object. ComboBox or ListBox or TextBox Have RTL build into the control, his is not the case with ToolTip (at least I didn't find a way).
Because we catch the Event of drawing item we now have to draw the item from right to left. another thing is that ToolTip is always draw to the right and if we want to create the feel that the toolTip is draw from the most left corner of the item to the left ,we have to place it in different X position for every item depending the item length.

We will use the same routine


We add format "String Format" with DirectionRightToLeft.
When we draw the item we have to start in the Right most ($Location.x+$ComboBox1.Size.Width)



Sizef : calculate the length of the tooltip according to the text ($atext) & the Font ($afont). We reduce the SizeF from the left position of the ToolTip. Last & nice we can add timeout for the ToolTip 3 sec' in this example.

  

ToolTip with more the one line RTL aligment:

In my example the ToolTip has just one line ,So the problem of RTL was not seen.
But when there is more than one line it's clear that we have a problem.


You can see on the right what happen when we have tow line. The text is alignment to the left instead to right.
To solve it we have to adjust the ToolTip object and to add routing that will draw the ToolTip text
.


                                                                                                                                               
75: OwnerDraw = $true - allow as to take control over the ToolTip Drawing Proccess.
76: Add_Draw  -
to catch the event of ToolTip drawing.


42: set the color for (Background Color) it can be name or FromArgb.
          (http://msdn.microsoft.com/en-us/library/system.windows.media.colors.aspx).
43: Set the Brush object.
44: drawing the ToolTip frame & background.
45: Draw the border.
46: Draw the ToolTip text according to our definition
.

Important to understand!This routing is activate when $ToolTip.Show is execute.
if you prefer you can set the ToolTip Background color direct in the object
$toolTip1.BackColor = [System.Drawing.Color]::FromArgb(255,224,224,224
).

Hope that this will be useful to someone.
To download the example press here.
I would like to thanks to:renuka krishnan for his article "Usinga Combobox to Select Colors"
PHeiberg from stackoverflow about is answer "Right to left tooltip text c#"Their answer/article makes sense and helps me to understand the MSDN example from MSDN.To draw centered text with GDI+ (DrawString)
http://msdn.microsoft.com/en-us/library/332kzs7c.aspx ComboBox:http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx

Doron Zilber D/..\Z
---------------------------------------------------------------------------------------------

Fix

When you go over the items in the drop down list, the select one is difficult to sea.
the rezone is that in the code I didn't change is color to white to be more visible





If you add the tow line the Select item will be more visible.


 
















The code for the tow line i add it below.
37    $aBrush = new-object Drawing.SolidBrush "white"
38    $_.Graphics.DrawString($atext,$Combobox1.font,$aBrush,$Location.x + `
          $ComboBox1.Size.Width),$Location.y,$format1)

Tuesday, February 26, 2013

How to use Dynamic Distribution list with Lync

Q: is “Dynamic Distribution Groups” can be used in Lync?
A: Wall the formal answer is NO, It’s not a bug it’s by design.

But if we use some creative thinking we can overcome this obstacle.
Organization typically use the GAL to create Address list,
Once we create the rule of the address list, the Address List Are Dynamically populate with user’s.
We can use the GAL as the Source for our “Dynamic Distribution list”,
or we can create new “Address list” just for the Lync & hide it from the rest of the users.
Wall we solve the first obstacle who to crate “Dynamic Distribution list” for Lync => 
use the GAL.

Of course we can’t use the “Address list”  from Lync ,
We have to create “Distribution List” with the same name of The “Address List” .
The "Distribution list" will contain all the Users that is resolve from the “Address List”  rule,
Wall this solve the second obstacle ,
“Address List” are not working with Lync =>
use “Distribution List”.
Ok there are just small details left out.
  1. How to create the “Distribution List” from the GAL?
  2. How to populate the “Distribution List” with recipient’s?
  3. How to maintain all of this, Remove/ADD user’s to “Distribution List”,
    Remove/add “Distribution List” .
Those small details are the reason we are here today.
Attach to this blog is PowerShell script that do all the small details.
  1. Start from a specified passion scan the GAL for All“Address List”,then query Active Directory for DL with the same name of The “Address List” If it failed to find then create new “Distribution List”, if exist then continue.
    Important:
    a) before create the“Distribution List” check that The Address List is not empty.
    b) The “Distribution List”is created in designated OU.
  2. Make sure the Recipient from “Address List” rule is the same as the Member’s
    in the “Distribution List” ,mark the “Distribution List” with date of cheking.
  3. After the Script is run we have “Distribution List” with the same name of the “Address List”,
    those “Distribution List” are “Dynamic Distribution Groups” we can use in Lync.
Of cores The PowerShell Script must be run in retention way once a Day or week .

Explain on the Script:









Set PowerShell Script Environment Variable 


92: Set the output file log name.
93: Set OU where the “Distribution List” will be created.
94: Set the start location at GAL to find the “Address List”.
95: in case there is more than one Domain .
GAL Global Address List
The "Distribution List" Active Directory Users and Computers
The Distribution List Exchange EMC
In this example the “Address List” are in format
* XXXX – Branch number.
* Branch Type – branch ,Center ,standalone.
* Branch Name.
 

 
because the display name of "Address list" in my Example have space and
SamAccountName, Name, Alias can’t have space,
I create $str ,you can change it to what you decide as long there is no space
 
to Download the Script

Friday, August 31, 2012

DSQuery life can be simple

Hi this  is  A test  please do not commant.

Hi most of you perobably familer with AD cmd tools Dsquery sdf


Exch-CleanUP_2011-12-10_x64.zip