Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Create-XML($inObj)
- {
- #begin drive element
- $xmlWriter.WriteStartElement('Drive')
- $xmlWriter.WriteAttributeString('clsid',$($inObj.CLSID))
- $xmlWriter.WriteAttributeString('name', "$($inObj.name)")
- $xmlWriter.WriteAttributeString('status', "$($inObj.driveLetter):")
- $xmlWriter.WriteAttributeString('image', '0')
- $xmlWriter.WriteAttributeString('changed', $(get-date -uformat "%Y-%m-%d %T"))
- $xmlWriter.WriteAttributeString('uid',"{$([guid]::NewGuid().ToString())}")
- $xmlWriter.WriteAttributeString('bypassErrors', '1')
- #begin properties element
- $xmlWriter.WriteStartElement('Properties')
- $xmlWriter.WriteAttributeString('action',"C")
- $xmlWriter.WriteAttributeString('thisDrive',"NOCHANGE")
- $xmlWriter.WriteAttributeString('allDrives',"NOCHANGE")
- $xmlWriter.WriteAttributeString('userName',"")
- $xmlWriter.WriteAttributeString('path',"$($inObj.path)")
- $xmlWriter.WriteAttributeString('label',"$($inObj.label)")
- $xmlWriter.WriteAttributeString('persistent',"1")
- $xmlWriter.WriteAttributeString('useLetter',"1")
- $xmlWriter.WriteAttributeString('letter',"$($inObj.driveLetter)")
- #end properties element
- $xmlwriter.WriteEndElement()
- #begin filters element
- $xmlWriter.WriteStartElement('Filters')
- #begin filterGroup element
- $xmlWriter.WriteStartElement('FilterGroup')
- $xmlWriter.WriteAttributeString('bool', "AND")
- $xmlWriter.WriteAttributeString('not', "0")
- $xmlWriter.WriteAttributeString('name', "CONTOSO\$($inObj.Group)")
- $xmlWriter.WriteAttributeString('sid', "$($inObj.Sid.ToString())")
- $xmlWriter.WriteAttributeString('userContext', "1")
- $xmlWriter.WriteAttributeString('primaryGroup', "0")
- $xmlWriter.WriteAttributeString('localGroup', "0")
- #endFilterGroup Element
- $xmlwriter.WriteEndElement()
- #end Filters element
- $xmlWriter.WriteEndElement()
- #end drive element
- $xmlwriter.WriteEndElement()
- }
- #define arrays and variables
- $fileArray = New-Object -TypeName System.Collections.ArrayList
- $MapArray = New-Object -TypeName System.Collections.ArrayList
- $siteArray = New-Object -TypeName System.Collections.ArrayList
- $xmlStuArray = New-Object -TypeName System.Collections.ArrayList
- $xmlStaffArray = New-Object -TypeName System.Collections.ArrayList
- $GroupsSearchBaseDN = 'OU=Groups,DC=Contoso,DC=Com'
- $StaffGroupsSearchBaseDN = 'OU=Staff Groups,DC=Contoso,DC=Com'
- $driveCLSID = "{935D1B74-9CB8-4e3c-9914-7DD559B7A417}"
- $arrayOfSchoolNumbersGroups = Get-ADGroup -filter * -searchbase $StaffGroupsSearchBaseDN
- $arrayOfSchoolNumbers = $arrayOfSchoolNumbersGroups.Name.SubString(0,4)
- #Grab all of the old terrible batch files from sysvol
- $files = Get-ChildItem -Path "\\$env:userdnsdomain\SysVol\$env:userdnsdomain\Policies" -filter *.bat -recurse
- #filter out school startup scripts
- $filteredfiles = $files | Where-Object {$_.Name -match '[0-9][0-9][0-9][0-9].bat'}
- #scrape out all net use statements, ignore commented out ones
- ForEach ($fileobj in $filteredfiles)
- {
- $file = Get-Content -literalpath $($fileobj.FullName)
- #each file is an array of strings so now we need to work with each line in each file
- ForEach ($line in $file)
- {
- If (($line -match 'net use*') -AND ($line -notmatch 'rem net use*'))
- {
- $obj = New-Object PSObject -Property @{
- Line = $Line
- FileName = $Fileobj.Name
- }
- $fileArray.Add($obj) | Out-Null
- }
- }
- }
- $filteredFileArray = $fileArray | Where-Object -filter {(($_.Line -like "net use i:*") -OR ($_.line -like "net use j:*") -OR ($_.line -like "net use k:*") -OR ($_.line -like "net use q:*") -OR ($_.line -like "net use u*") -OR ($_.line -like "net use v:*") -OR ($_.line -like "net use w:*") -OR ($_.line -like "net use x:*"))}
- #iterate once per school
- ForEach ($num in $arrayOfSchoolNumbers)
- {
- #define new array to be used in each iteration of school loop
- $MapArray = New-Object -TypeName System.Collections.ArrayList
- $added = $:false
- #go through all files
- ForEach ($site in $filteredFileArray)
- {
- #match school number to batch file
- If ($num -eq $($site.FileName.SubString(0,4)))
- {
- $added = $:true
- If ($($Site.Line) -notmatch " /Y")
- {
- $MapArray.Add($($site.Line.ToString())) | Out-Null
- }
- Else
- {
- $MapArray.Add($($site.Line.ToString().SubString(0,$site.Line.ToString().Length-3))) | Out-Null
- }
- }
- }
- If ($added)
- {
- #create new object and populate with school number and array of files from each site
- $siteObjArray = New-Object PSObject -Property @{
- SchoolNum = $num
- Lines = $MapArray
- }
- $SiteArray.Add($SiteObjArray) | Out-Null
- }
- }
- #go through each new object in the site array and clean them up, then put them in a new object format to be converted into XML
- ForEach ($siteObj in $SiteArray)
- {
- $driveLetterArray = New-Object -TypeName System.Collections.ArrayList
- ForEach ($lineObj in $siteObj.lines)
- {
- $lineObj = $lineObj -replace '/','\'
- $splitLine = $lineObj.Split(" ")
- $driveLetter = $splitLine[2].SubString(0,1)
- $UNCPath = $splitLine[3]
- If (($UNCPath -match 'oldServerName[0-9][0-9][0-9][0-9]') -and ($UNCPath -notmatch 'oldPath'))
- {
- $SplitUNCPath = $UNCPath.SubString(1).Split("\")
- $len = $SplitUNCPath.Length
- $label = $SplitUNCPath[$len-1]
- $Group = Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Staff)" -searchbase $GroupsSearchBaseDN
- If ($driveLetterArray -contains $driveLetter)
- {
- $Group = Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Students)" -searchbase $GroupsSearchBaseDN
- }
- If ($label -eq "%Username%")
- {
- $label = $SplitUNCPath[$len-2]
- $Group = Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Students)" -searchbase $GroupsSearchBaseDN
- }
- $partialPath = $($SplitUNCPath[2..$($len-1)])
- $fixedpath = $partialPath -join '\'
- $finalPath = "\\FS$($SiteObj.SchoolNum)\$fixedPath"
- $xmlObj = New-Object PSObject -Property @{
- DriveLetter = $($driveLetter.ToUpper())
- Path = $finalPath
- Label = $label
- CLSID = $driveCLSID
- group = $Group.Name
- sid = $Group.sid
- name = "$($($SiteObj.SchoolNum) + " " + $label)"
- }
- If ($($xmlObj.Group) -like "*Students")
- {
- $xmlStuArray.Add($xmlObj) | Out-Null
- }
- Else
- {
- $xmlStaffArray.Add($xmlObj) | Out-Null
- }
- }
- If (($UNCPath -match 'oldServerName[0-9][0-9][0-9][0-9]') -and ($UNCPath -match 'oldPath'))
- {
- $SplitUNCPath = $UNCPath.SubString(1).Split("\")
- $len = $SplitUNCPath.Length
- $label = $SplitUNCPath[$len-1]
- $Group = Get-ADGroup -filter {name -eq "GroupName"} -searchbase $GroupsSearchBaseDN
- $partialPath = $($SplitUNCPath[2..$($len-1)])
- $fixedpath = $partialPath -join '\'
- $finalPath = "\\FS$($SiteObj.SchoolNum)\$fixedPath"
- $xmlObj = New-Object PSObject -Property @{
- DriveLetter = $($driveLetter.ToUpper())
- Path = $finalPath
- Label = $label
- CLSID = $driveCLSID
- group = $Group.Name
- sid = $Group.sid
- name = "$($($SiteObj.SchoolNum) + " " + $label)"
- }
- $xmlStaffArray.Add($xmlObj) | Out-Null
- }
- $driveLetterArray.Add($driveLetter) | Out-Null
- }
- }
- #generate the XML with the new XML arrays
- $pathArray = "$PSScriptRoot\XMLStu.xml","$PSScriptRoot\XMLStaff.xml"
- ForEach ($path in $pathArray)
- {
- $XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)
- #xml setup
- $xmlWriter.Formatting = 'Indented'
- $xmlWriter.Indentation = 1
- $XmlWriter.IndentChar = "`t"
- $xmlWriter.WriteStartDocument()
- $xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")
- $xmlWriter.WriteStartElement('Drives')
- If ($path -like "*Stu.xml")
- {
- ForEach ($element in $xmlStuArray)
- {
- Create-XML $element
- }
- }
- Else
- {
- ForEach ($element in $xmlStaffArray)
- {
- Create-XML $element
- }
- }
- #cleanup and close xml stream
- $xmlWriter.WriteEndElement()
- $xmlWriter.Flush()
- $xmlWriter.Close()
- }
- #$fileArray |Sort filename,line
- #filter out common drive maps
- #$fileArray | Where-Object -filter {(($_.Line -notlike "net use i:*") -AND ($_.line -notlike "net use j:*") -AND ($_.line -notlike "net use k:") -AND ($_.line -notlike "net use q:*") -AND ($_.line -notlike "net use u*") -AND ($_.line -notlike "net use v:*") -AND ($_.line -notlike "net use w:*") -AND ($_.line -notlike "net use x:*"))}
- #give only common drive maps
- #$fileArray | Where-Object -filter {(($_.Line -like "net use i:*") -OR ($_.line -like "net use j:*") -OR ($_.line -like "net use k:") -OR ($_.line -like "net use q:*") -OR ($_.line -like "net use u*") -OR ($_.line -like "net use v:*") -OR ($_.line -like "net use w:*") -OR ($_.line -like "net use x:*"))}
Advertisement
Add Comment
Please, Sign In to add comment