smartguy5000

Generate-GPPreferenceMappingsFromStartupScripts

Jun 25th, 2018
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Create-XML($inObj)
  2.     {
  3.         #begin drive element
  4.         $xmlWriter.WriteStartElement('Drive')
  5.         $xmlWriter.WriteAttributeString('clsid',$($inObj.CLSID))
  6.         $xmlWriter.WriteAttributeString('name', "$($inObj.name)")
  7.         $xmlWriter.WriteAttributeString('status', "$($inObj.driveLetter):")
  8.         $xmlWriter.WriteAttributeString('image', '0')
  9.         $xmlWriter.WriteAttributeString('changed', $(get-date -uformat "%Y-%m-%d %T"))
  10.         $xmlWriter.WriteAttributeString('uid',"{$([guid]::NewGuid().ToString())}")
  11.         $xmlWriter.WriteAttributeString('bypassErrors', '1')
  12.    
  13.         #begin properties element
  14.         $xmlWriter.WriteStartElement('Properties')
  15.         $xmlWriter.WriteAttributeString('action',"C")
  16.         $xmlWriter.WriteAttributeString('thisDrive',"NOCHANGE")
  17.         $xmlWriter.WriteAttributeString('allDrives',"NOCHANGE")
  18.         $xmlWriter.WriteAttributeString('userName',"")
  19.         $xmlWriter.WriteAttributeString('path',"$($inObj.path)")
  20.         $xmlWriter.WriteAttributeString('label',"$($inObj.label)")
  21.         $xmlWriter.WriteAttributeString('persistent',"1")
  22.         $xmlWriter.WriteAttributeString('useLetter',"1")
  23.         $xmlWriter.WriteAttributeString('letter',"$($inObj.driveLetter)")
  24.  
  25.         #end properties element
  26.         $xmlwriter.WriteEndElement()
  27.    
  28.         #begin filters element
  29.         $xmlWriter.WriteStartElement('Filters')
  30.  
  31.         #begin filterGroup element
  32.         $xmlWriter.WriteStartElement('FilterGroup')
  33.         $xmlWriter.WriteAttributeString('bool', "AND")
  34.         $xmlWriter.WriteAttributeString('not', "0")
  35.         $xmlWriter.WriteAttributeString('name', "CONTOSO\$($inObj.Group)")
  36.         $xmlWriter.WriteAttributeString('sid', "$($inObj.Sid.ToString())")
  37.         $xmlWriter.WriteAttributeString('userContext', "1")
  38.         $xmlWriter.WriteAttributeString('primaryGroup', "0")
  39.         $xmlWriter.WriteAttributeString('localGroup', "0")
  40.         #endFilterGroup Element  
  41.         $xmlwriter.WriteEndElement()
  42.  
  43.         #end Filters element
  44.         $xmlWriter.WriteEndElement()
  45.  
  46.         #end drive element
  47.         $xmlwriter.WriteEndElement()
  48.     }
  49.  
  50. #define arrays and variables
  51. $fileArray = New-Object -TypeName System.Collections.ArrayList
  52. $MapArray = New-Object -TypeName System.Collections.ArrayList
  53. $siteArray = New-Object -TypeName System.Collections.ArrayList
  54. $xmlStuArray = New-Object -TypeName System.Collections.ArrayList
  55. $xmlStaffArray = New-Object -TypeName System.Collections.ArrayList
  56. $GroupsSearchBaseDN = 'OU=Groups,DC=Contoso,DC=Com'
  57. $StaffGroupsSearchBaseDN = 'OU=Staff Groups,DC=Contoso,DC=Com'
  58. $driveCLSID = "{935D1B74-9CB8-4e3c-9914-7DD559B7A417}"
  59. $arrayOfSchoolNumbersGroups = Get-ADGroup -filter * -searchbase $StaffGroupsSearchBaseDN
  60. $arrayOfSchoolNumbers = $arrayOfSchoolNumbersGroups.Name.SubString(0,4)
  61.  
  62.  
  63. #Grab all of the old terrible batch files from sysvol
  64. $files = Get-ChildItem -Path "\\$env:userdnsdomain\SysVol\$env:userdnsdomain\Policies" -filter *.bat -recurse
  65.  
  66. #filter out school startup scripts
  67. $filteredfiles = $files | Where-Object {$_.Name -match '[0-9][0-9][0-9][0-9].bat'}
  68.  
  69.  
  70.  
  71. #scrape out all net use statements, ignore commented out ones
  72. ForEach ($fileobj in $filteredfiles)
  73.     {        
  74.          $file = Get-Content -literalpath $($fileobj.FullName)
  75.          #each file is an array of strings so now we need to work with each line in each file
  76.             ForEach ($line in $file)
  77.             {
  78.                 If (($line -match 'net use*') -AND ($line -notmatch 'rem net use*'))
  79.                 {
  80.                     $obj = New-Object PSObject -Property @{
  81.                     Line = $Line
  82.                     FileName = $Fileobj.Name
  83.                     }
  84.                     $fileArray.Add($obj) | Out-Null
  85.                 }
  86.             }
  87.     }
  88.  
  89. $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:*"))}
  90. #iterate once per school
  91. ForEach ($num in $arrayOfSchoolNumbers)
  92.     {
  93.         #define new array to be used in each iteration of school loop        
  94.         $MapArray = New-Object -TypeName System.Collections.ArrayList
  95.         $added = $:false
  96.  
  97.         #go through all files
  98.             ForEach ($site in $filteredFileArray)
  99.                 {
  100.                     #match school number to batch file            
  101.                     If ($num -eq $($site.FileName.SubString(0,4)))
  102.                         {
  103.                             $added = $:true
  104.                             If ($($Site.Line) -notmatch " /Y")
  105.                                 {
  106.                                     $MapArray.Add($($site.Line.ToString())) | Out-Null
  107.                                 }
  108.                             Else
  109.                                 {                        
  110.                                     $MapArray.Add($($site.Line.ToString().SubString(0,$site.Line.ToString().Length-3))) | Out-Null
  111.                                 }
  112.                    
  113.                        
  114.                         }
  115.                 }
  116.             If ($added)
  117.                 {
  118.                     #create new object and populate with school number and array of files from each site
  119.                     $siteObjArray = New-Object PSObject -Property @{
  120.                     SchoolNum = $num
  121.                     Lines = $MapArray
  122.                     }
  123.                     $SiteArray.Add($SiteObjArray) | Out-Null
  124.                 }
  125.     }
  126.  
  127. #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
  128. ForEach ($siteObj in $SiteArray)
  129.     {
  130.         $driveLetterArray = New-Object -TypeName System.Collections.ArrayList  
  131.         ForEach ($lineObj in $siteObj.lines)
  132.             {
  133.                 $lineObj = $lineObj -replace '/','\'
  134.                 $splitLine = $lineObj.Split(" ")
  135.                 $driveLetter = $splitLine[2].SubString(0,1)        
  136.                 $UNCPath = $splitLine[3]
  137.  
  138.                 If (($UNCPath -match 'oldServerName[0-9][0-9][0-9][0-9]') -and ($UNCPath -notmatch 'oldPath'))
  139.                     {            
  140.                         $SplitUNCPath = $UNCPath.SubString(1).Split("\")
  141.                         $len = $SplitUNCPath.Length
  142.                         $label = $SplitUNCPath[$len-1]                                                      
  143.                         $Group =  Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Staff)" -searchbase $GroupsSearchBaseDN
  144.                         If ($driveLetterArray -contains $driveLetter)
  145.                             {
  146.                                 $Group = Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Students)" -searchbase $GroupsSearchBaseDN
  147.                             }
  148.                         If ($label -eq "%Username%")
  149.                             {
  150.                                 $label = $SplitUNCPath[$len-2]
  151.                                 $Group = Get-ADGroup -ldapfilter "(name=$($SiteObj.SchoolNum) Students)" -searchbase $GroupsSearchBaseDN
  152.                             }                
  153.                         $partialPath = $($SplitUNCPath[2..$($len-1)])              
  154.                         $fixedpath = $partialPath -join  '\'                
  155.                         $finalPath = "\\FS$($SiteObj.SchoolNum)\$fixedPath"
  156.                             $xmlObj = New-Object PSObject -Property @{
  157.                             DriveLetter = $($driveLetter.ToUpper())
  158.                             Path = $finalPath
  159.                             Label = $label
  160.                             CLSID = $driveCLSID
  161.                             group = $Group.Name
  162.                             sid = $Group.sid
  163.                             name = "$($($SiteObj.SchoolNum) + " " + $label)"
  164.                             }
  165.                         If ($($xmlObj.Group) -like "*Students")
  166.                             {
  167.                                 $xmlStuArray.Add($xmlObj) | Out-Null
  168.                             }
  169.                         Else
  170.                             {
  171.                                 $xmlStaffArray.Add($xmlObj) | Out-Null
  172.                             }
  173.                
  174.                     }
  175.                 If (($UNCPath -match 'oldServerName[0-9][0-9][0-9][0-9]') -and ($UNCPath -match 'oldPath'))
  176.                     {            
  177.                         $SplitUNCPath = $UNCPath.SubString(1).Split("\")
  178.                         $len = $SplitUNCPath.Length
  179.                         $label = $SplitUNCPath[$len-1]
  180.                         $Group = Get-ADGroup -filter {name -eq "GroupName"} -searchbase $GroupsSearchBaseDN                  
  181.                         $partialPath = $($SplitUNCPath[2..$($len-1)])              
  182.                         $fixedpath = $partialPath -join  '\'                
  183.                         $finalPath = "\\FS$($SiteObj.SchoolNum)\$fixedPath"
  184.                             $xmlObj = New-Object PSObject -Property @{
  185.                             DriveLetter = $($driveLetter.ToUpper())
  186.                             Path = $finalPath
  187.                             Label = $label
  188.                             CLSID = $driveCLSID
  189.                             group = $Group.Name
  190.                             sid = $Group.sid
  191.                             name = "$($($SiteObj.SchoolNum) + " " + $label)"
  192.                             }
  193.                         $xmlStaffArray.Add($xmlObj) | Out-Null
  194.                     }
  195.                     $driveLetterArray.Add($driveLetter) | Out-Null
  196.        
  197.        
  198.             }
  199.     }
  200.  
  201. #generate the XML with the new XML arrays
  202. $pathArray = "$PSScriptRoot\XMLStu.xml","$PSScriptRoot\XMLStaff.xml"
  203. ForEach ($path in $pathArray)
  204.     {
  205.         $XmlWriter = New-Object System.XMl.XmlTextWriter($Path,$Null)
  206.         #xml setup
  207.         $xmlWriter.Formatting = 'Indented'
  208.         $xmlWriter.Indentation = 1
  209.         $XmlWriter.IndentChar = "`t"
  210.         $xmlWriter.WriteStartDocument()
  211.         $xmlWriter.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='style.xsl'")
  212.         $xmlWriter.WriteStartElement('Drives')
  213.         If ($path -like "*Stu.xml")
  214.             {
  215.                 ForEach ($element in $xmlStuArray)
  216.                     {
  217.                         Create-XML $element  
  218.                     }
  219.             }
  220.         Else
  221.             {
  222.                 ForEach ($element in $xmlStaffArray)
  223.                     {
  224.                         Create-XML $element  
  225.                     }
  226.             }
  227.            
  228.         #cleanup and close xml stream
  229.         $xmlWriter.WriteEndElement()
  230.         $xmlWriter.Flush()
  231.         $xmlWriter.Close()
  232.  
  233.     }
  234.  
  235.  
  236.  
  237. #$fileArray |Sort filename,line
  238.  
  239. #filter out common drive maps
  240. #$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:*"))}
  241.  
  242. #give only common drive maps
  243. #$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