Changes

Jump to: navigation, search

WPKG with Active Directory

2,766 bytes added, 13:20, 10 November 2014
no edit summary
</source>
 
 
=Pulling workstation names to hosts.xml from Active Directory OUs automatically with Powershell=
 
Script I created to generate hosts.xml and add new profile-id to profiles.xml
We have AD organized with divisions, and every division have container for computers
 
<source lang="powershell">
Import-Module ActiveDirectory
 
 
#script rebuilds hosts.xml every time in case computer was moved to other location
#script check profiles.xml and adds every new profile id
 
#path where xml will be saved
$hostspath = "\\10.10.10.10\Soft\wpkg\hosts.xml"
$profilespath = "\\10.10.10.10\Soft\wpkg\profiles.xml"
 
[ XML ] $contentxml = Get-Content $profilespath
 
 
 
 
#hosts Xml starting element
$xmlHosts = New-Object System.Xml.XmlTextWriter ( $hostspath , $null )
$xmlHosts . Formatting = 'Indented'
$xmlHosts . Indentation = 1
$xmlHosts . IndentChar = "`t"
$xmlHosts . WriteStartDocument()
$xmlHosts . WriteComment( 'List of AD computers' )
$xmlHosts . WriteStartElement( 'wpkg' )
 
#get all computers from ad
$computers = Get-ADComputer -Filter * -Property * | Select-Object @{Label = "name" ;Expression = {( $_ . Name)}} , @{Label = "profile-id" ;Expression = {( $_ . CanonicalName)}} , @{Label = "os" ;Expression = {( $_ . OperatingSystem)}}
 
#For each entry in computers
ForEach ( $entry in $computers )
{
 
$profileid = $profileid -replace $entry . name , "" #remove computer name from profile id
 
#write new element to hosts file
$xmlHosts . WriteStartElement( 'host' )
$xmlHosts . WriteAttributeString( 'name' , $entry . name)
$xmlHosts . WriteAttributeString( 'profile-id' , $profileid )
$xmlHosts . WriteAttributeString( 'os' , $entry . os)
$xmlHosts . WriteEndElement()
 
 
#check profiles xml file
$check = $contentxml . profiles . profile | Select-Object -Property id #| ForEach-Object {[System.Convert]::ToString($_)}
if ( $check -match $profileid ) # if profile id already exists go to next entry, else create new profile id element
{}
else {
$newelement = $contentxml . CreateElement( 'profile' )
$newelementattrib = $contentxml . CreateAttribute( 'id' )
$newelementattrib . Value = $profileid
$newelement . Attributes . Append( $newelementattrib )
 
$newsubelement = $contentxml . CreateElement( 'package' )
$newsubelementtattrib = $contentxml . CreateAttribute( 'package-id' )
$newsubelementtattrib . Value = 'default'
$newsubelement . Attributes . Append( $newsubelementtattrib )
$newelement . AppendChild( $newsubelement )
 
$contentxml . LastChild . AppendChild( $newelement )
$contentxml . Save( $profilespath )
 
 
}
}
 
 
 
 
 
 
 
#add end element in Xml save, flush and close files
 
 
$xmlHosts . WriteEndElement()
$xmlHosts . WriteEndDocument()
$xmlHosts . Flush()
$xmlHosts . Close()
</source>
[[category:Documentation]]
[[category:Installation]]
3
edits

Navigation menu