Showing posts with label ADSI. Show all posts
Showing posts with label ADSI. Show all posts

Thursday, 6 August 2009

New-Ou.PS1

  1. <# 
  2. .SYNOPSIS 
  3.     This script uses ADSI to add a new OU to a domain. 
  4. .DESCRIPTION 
  5.     This script creates a pointer to the domain, then 
  6.     uses the Create method to create a new OU under 
  7.     the root of the domain. 
  8. .NOTES 
  9.     File Name  : New-Ou.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\New-OU.PS1' 
  17.     Created OU: PowerShell 
  18.      
  19.     distinguishedName : {OU=PowerShell,DC=cookham,DC=net} 
  20.     Path              : LDAP://ou=PowerShell,dc=Cookham,dc=Net 
  21. #> 
  22. ## 
  23. # Start of Script 
  24. ## 
  25.  
  26. #Set variables 
  27. $OuName = “PowerShell” 
  28. $Domain = [ADSI]“LDAP://dc=Cookham,dc=Net” 
  29.  
  30. # Create the OU 
  31. $Ou = $Domain.Create(”OrganizationalUnit”, “ou=” + $OuName) 
  32. $Ou.SetInfo() 
  33.  
  34. # Display results 
  35. "Created OU: {0}" -f $OUName 
  36. $OU 
Technorati Tags: ,,,

Wednesday, 17 December 2008

Get-FSMOHolders.ps1

  1. # Get-FSMOHolders.ps1 
  2. # Gets the names of the systems holding the 5 FSMO holders 
  3. # Runs on a domain-joined machine 
  4. # Thomas Lee - tfl@psp.co.uk 
  5.  
  6. # Get host,forest and domain details 
  7. $Hostname   = hostname 
  8. $forest = [DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() 
  9. $domain = [DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() 
  10.  
  11. #Display FSMOs, forest wide first 
  12. "For Host {0}" -f $hostname 
  13. "In Forest {0}, forest masters are: " -f $for.name 
  14. "Schema master          : {0}" -f $forest.SchemaRoleOwner.Name 
  15. "Domain Master          : {0}" -f $forest.NamingRoleOwner.Name 
  16. "" 
  17. # Current domain 
  18. "In domain {0}, domain masters are:" -f $dom.Name 
  19. "PDC Master            : {0}" -f $domain.PdcRoleOwner 
  20. "Infrastructure Master : {0}" -f $domain.InfrastructureRoleOwner 
  21. "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.net

In 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
This script produces the following output (assuming no errors!):
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


Technorati Tags: ,,