- <#
- .SYNOPSIS
- This script uses ADSI to add a new OU to a domain.
- .DESCRIPTION
- This script creates a pointer to the domain, then
- uses the Create method to create a new OU under
- the root of the domain.
- .NOTES
- File Name : New-Ou.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\foo]: .\New-OU.PS1'
- Created OU: PowerShell
- distinguishedName : {OU=PowerShell,DC=cookham,DC=net}
- Path : LDAP://ou=PowerShell,dc=Cookham,dc=Net
- #>
- ##
- # Start of Script
- ##
- #Set variables
- $OuName = “PowerShell”
- $Domain = [ADSI]“LDAP://dc=Cookham,dc=Net”
- # Create the OU
- $Ou = $Domain.Create(”OrganizationalUnit”, “ou=” + $OuName)
- $Ou.SetInfo()
- # Display results
- "Created OU: {0}" -f $OUName
- $OU
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Showing posts with label ADSI. Show all posts
Showing posts with label ADSI. Show all posts
Thursday, 6 August 2009
New-Ou.PS1
Labels:
ADSI,
powershell,
PowerShell scripts
Wednesday, 17 December 2008
Get-FSMOHolders.ps1
- # Get-FSMOHolders.ps1
- # Gets the names of the systems holding the 5 FSMO holders
- # Runs on a domain-joined machine
- # Thomas Lee - tfl@psp.co.uk
- # Get host,forest and domain details
- $Hostname = hostname
- $forest = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
- $domain = [DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
- #Display FSMOs, forest wide first
- "For Host {0}" -f $hostname
- "In Forest {0}, forest masters are: " -f $for.name
- "Schema master : {0}" -f $forest.SchemaRoleOwner.Name
- "Domain Master : {0}" -f $forest.NamingRoleOwner.Name
- ""
- # Current domain
- "In domain {0}, domain masters are:" -f $dom.Name
- "PDC Master : {0}" -f $domain.PdcRoleOwner
- "Infrastructure Master : {0}" -f $domain.InfrastructureRoleOwner
- "RID Master : {0}" -f $domain.RidRoleOwner
This code produces the following output:
PS C:\foo> .\Get-FSMOHolders.ps1
For Host Cookham8
In Forest cookham.net, forest masters are:
Schema master : Cookham1.cookham.net
Domain Master : Cookham1.cookham.netIn domain cookham.net, domain masters are:
PDC Master : Cookham1.cookham.net
Infrastructure Master : Cookham1.cookham.net
RID Master : Cookham1.cookham.net
Saturday, 29 November 2008
Add-DomainUserToLocalAdministatorsGroup.ps1
#Requires -Version 2.0
# Add-DomainUserToLocalAdministatorsGroup.ps1
# Add a domain user to a Local Administrator's group
# Thomas Lee - tfl@psp.co.uk
# Based on http://powershell.com/cs/media/p/380.aspx, with some error checking
# Setup up information on user to add,etc
$Domain = "Cookham" # where the user account comes from
$Computer = "Cookham8" # Computer to add the user to
$User = "tfl" # User
# Try to add user to Administrator's group
try {
$group.Add("WinNT://" + $Domain + "/" + $User)
"User {0}\{1} added to Administrator's local group in system: {2}" -f $domain,$user, $computer
}
catch {
"Error adding user to Administrator's group"
}
""
# Now print results
$computer = [ADSI]("WinNT://" + $Computer + ",computer")
$group = $computer.psbase.children.find("Administrators")
"Group: {0} contains {1} members:" -f $group.Name.Value, $members.Length
$members = $group.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name",'GetProperty',$null,$_,$null)}
$members
PS C:\foo> .\Add-DomainUserToLocalAdministatorsGroup.ps1
User Cookham\tfl added to Administrator's local group in system: Cookham8
Group: Administrators contains 3 members:
Administrator
Domain Admins
tfl
Labels:
ADSI,
powershell,
PowerShell scripts,
PowerShell V2
Subscribe to:
Posts (Atom)