Hello world !!
I. Get Powershell running
Everybody has heard about the new command shell called PowerShell from Microsoft. The Powershell allow system administrator to manage their system from a powerfull command line. To be able to run Powershell scripts, you will need to have at least Windows XP SP2 and .Net Framework 2.0 installed. Based on your operating system, you might need to download the Powershell package or simply install it as an additional component/feature of Windows 2008 Server.
By default, Powershell does not allow script execution in order to protect your system from unauthorized scripts. For the purpose of these examples, we will allow powershell to run any scripts on the machine. To enable this, you need to type the following command in the powershell command prompt
set-ExecutionPolicy Unrestricted
To check that unrestricted policy is set, you can type the following command
get-ExecutionPolicy
II. Create an OU in Active Directory
A. Powershell Script
Powershell needs the [adsi] type shortcuts to work with directory entires. To create an OU at the root of your Active Directory, you will need a code similar to this :
#——————————– Begin of Script ———————————-#
# Create an OU in Active Directory
$strAD=[adsi] “LDAP://DC=MyDomain,DC=Powershell,DC=Lab”
$strOU= strAD.Create (“OrganizationalUnit”, “OU=PowerShell_OU”)
$strOU.Put(“Description”, “OU Created using PowerShell Script”)
$strOU.SetInfo()
write-host “Operation Completed successfully”
#——————————– End of Script ———————————-#
This code should be self-explanatory. In the begin of the script, we specify where to create the OU. Then, using the Create method, we specify that we want to create an OU. Optionally, you can set some attributes (i.e. the description attribute). Finally, we need to write this information into the Active Directory. We use the SetInfo() method.
To run the script, simply copy and paste the code above (do not forget to adapt it to your infrastructure) and save the file with a *.ps1 extension. I have save it as SingleOUCreation.ps1.
To run the script, open the powershell console and type in the location where the script is located (for example c:\PS_Scripts\SingleOUCreation.ps1)
You should have now a new OU within your Active Directory Infrastructure.
If you are familiar with Vbscript, i have put a example of how to create an ou with vbscript. you will see that the principle is really similar. Vbscripting guys should be able to jump quite quickly into the Powershell scripting new technology
‘—————————————– Begin of Script —————————————————-‘
‘ Create OU in AD using Vbscript
‘Connect to AD
Set oAD=GetObject(“LDAP://DC=MyDomain,DC=LAb”)
set strOU = oAD.Create (“OrganizationalUnit”, “OU=Powershell_OU”)
strOU.SetInfo
‘————————————- End of Script ———————————————————-
To create an OU under another existing OU, simply change the LDAP path. For example, i you want to create an OU under the OU PowserShell_OU, we have created, you simply need to modify the following in the above script
$strAD=[adsi] “LDAP://OU=PowerShell_OU, DC=MyDomain,DC=Powershell,DC=Lab
That’s it. Pretty simple huh…
This conclude the first post over Powershell and Active Directory. Other post will follow illustrating the basics of AD management with Powershell. After basics, we will start posting some more complex scripts that might be usefull in the real world.
Till next Time
See ya
Welcome First time hopped here on your site, founde on Bing.