Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Test-SameSubnet {
  2. param (
  3. [parameter(Mandatory=$true)]
  4. [Net.IPAddress]
  5. $ip1,
  6.  
  7. [parameter(Mandatory=$true)]
  8. [Net.IPAddress]
  9. $ip2,
  10.  
  11. [parameter()]
  12. [alias("SubnetMask")]
  13. [Net.IPAddress]
  14. $mask ="255.255.255.0"
  15. )
  16.  
  17. if (($ip1.address -band $mask.address) -eq ($ip2.address -band $mask.address)) {$true}
  18. else {$false}
  19.  
  20. }
  21.  
  22. Function get-school {
  23. Param (
  24.     [parameter(mandatory=$true)]
  25.  
  26.     $IP)
  27.  
  28.         # Check against the IP list (& get working directory - should work with PS2.0 onwards)
  29.         if ($IP -eq '10.69.47.')
  30.         {
  31.             write-host "here"
  32.         }
  33.         else
  34.         {#write-host $IP
  35.         }
  36.         $myfile = 'C:\Users\westadmin\Desktop\temp\CLEO_IP_subnets.xml'
  37.         [xml]$xml = Get-Content $myfile
  38.             #  The following line could do with being updated to use the subnet mask in order to match whether it is within the range.
  39.             #  Use the top function of this file, with a check-subnet 192.168.0.23 192.168.0.0/22 format.
  40.                 $child = $xml.selectnodes("/SiteDetails/Site/Network") | where-object {$_.defaultip -eq $IP}
  41.  
  42.             # If $child is blank, then no hit.  Need to check non/24 networks to scan this.  Search on preceding ranges?
  43.         if (!($child))
  44.             {
  45. $a = 0      
  46. $b = 0        
  47.    $newip = $null
  48.     $splitip=$ip.split(".")
  49.     $3rd = $splitip[2] -1
  50.  while ($loop -ne $false)
  51.                 {
  52.                         # Decrement the last octet by one.
  53.                    
  54.                                
  55.                      $newIP = $splitip[0]+"."+$splitip[1]+"."+$3rd+"."
  56.                     #write-host "New default is" $newip
  57.                     $child = $xml.selectnodes("/SiteDetails/Site/Network") | where-object {$_.defaultip -eq $newip}
  58.                     if ($child)
  59.                         {$loop = $false
  60.                         write-host "Matched" $child.parentnode.id
  61.  
  62.                         } else {$loop =$true
  63.                                                
  64.                         $a++
  65.                         $3rd--
  66.                         if ($a -eq 4){$loop = $false}
  67.                         }
  68.                 }
  69.             }
  70.         #set variables
  71.         if (!($child)){$now= "No match, for $IP" ; $now | out-file C:\users\westadmin\Desktop\unmatched.txt  -NoClobber -Append}
  72.         $a = $child.parentnode.id
  73.         $b = $child.name
  74.         $c = $child.parentnode.name
  75.         $d = $child.supported
  76.       #  $b = $child.parentnode.name
  77.        
  78.         $combined = $a+"-"+$b
  79.         $combined
  80.         $c
  81.         $d
  82.        
  83. }
  84. add-pssnapin sqlserverprovidersnapin100 -ErrorAction silentlycontinue
  85. add-pssnapin sqlservercmdletsnapin100 -ErrorAction silentlycontinue
  86. import-module SQLPS
  87. $dataSource = ".\SophosEC"
  88. $database = "Sophos551"
  89. $sqlCommand = "SELECT IPAddressText, name FROM dbo.ComputersAndDeletedComputers"
  90. write-host "Running SQL..."
  91. $data = Invoke-Sqlcmd -ServerInstance $datasource -Database $database -Query $sqlcommand
  92.  
  93. $myarray = @()
  94. $x = 0
  95. $i = 0
  96.  
  97. foreach ($thing in $data)
  98.     {
  99.     if ($thing.IPAddressText -as [ipaddress])
  100.         {
  101.             $ipsplit = $thing.IPAddressText -split "\d{1,3}$"
  102.             $stringIP = [string]$IpSplit
  103.             $return = get-school $stringIP.trim()
  104.             $type=$null
  105.  
  106.             # Identify P2P or SIMS likely server
  107.             if ($thing.name -like '*server')
  108.                 {$type="P2P"}
  109.             if ($thing.name -like '*2k12mis*' -or $thing.name -like '*2k8mis*'){$type="MIS"}
  110.             $myobj = New-Object -TypeName PSObject
  111.              Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'SchoolID' -Value $return[0]
  112.              Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'SchoolName' -Value $return[1]
  113.              Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Supported' -Value $return[3]
  114.              IF(!([string]::IsNullOrWhiteSpace($type))){Add-Member -InputObject $myobj -MemberType 'NoteProperty' -Name 'Type' -Value $type}
  115.  
  116.             $myarray +=$myobj
  117.         }
  118.     }
  119.  Write-Host "populate Table"
  120.  
  121. # Get number of networks from XML - and number of Sites in total
  122.  
  123. $file = gc 'C:\Users\westadmin\Desktop\CLEO_IP_subnets.xml'
  124. foreach ($a in $file) {if ($a -like '*network*'){$networks = $networks+1}}
  125. foreach ($a in $file) {if ($a -like '*Site *'){$sites = $sites+1}}
  126.  
  127.  
  128. $results = $myarray | Group-Object -Property 'SchoolID' | Select-Object name,count, type |Sort-Object name
  129. $results1 = $myarray | Group-Object -Property 'SchoolID' | Select-Object name,count, type |Sort-Object count -Descending
  130.  
  131. $row=@("")
  132. $row+="<tr><td>SchoolSubnet</td><td>Count</td></tr>"
  133. # Dates are automagically parsed into datetime format
  134. foreach ($item in $results)
  135.     {
  136.         $row+= "<tr><td>"+$item.name+"</td><td>"+$item.count+"</td></tr>"
  137.     }
  138.  
  139. $row1=@("")
  140. $row1+="<tr><td>SchoolSubnet</td><td>Count</td></tr>"
  141. # Dates are automagically parsed into datetime format
  142. foreach ($item in $results1)
  143.     {
  144.         $row1+= "<tr><td>"+$item.name+"</td><td>"+$item.count+"</td></tr>"
  145.     }
  146.  
  147.  
  148.  $body = " Here's the latest stats on the "+$myarray.count+" machines currently categorized by Sophos by subnet.    `n `n Sorted by schoolnumber <table border=1>$row</table> `n And sorted by number of endpoints....<table border=1>$row1</table> <p> This was generated automagically from the EDU-PRD-Sophos server"
  149.  
  150. $recipients = @("ChrisA <Chris.Adams@btlancashire.co.uk>", "ChrisC <Chris.carr@btlancashire.co.uk>", "AshG <Ash.Green@btlancashire.co.uk>")
  151.  
  152. write-host "Sending message"
  153. Send-MailMessage -From "NewSophos <EDU-PRD-Sophos@btlancashire.co.uk>" -To $recipients -Subject "Subnets in sophos" -Body $body -Priority High -SmtpServer "smtp.education.btlsl.co.uk" -BodyAsHtml
  154.  
  155. $myrow=@("")
  156. $myrow+="<tr><td>SchoolSubnet</td><td>P2P/MIS hit</td></tr>"
  157. # Dates are automagically parsed into datetime format
  158. foreach ($bit in $Myarray)
  159.     {
  160.         IF(!([string]::IsNullOrWhiteSpace($bit.type))) {$myrow+= "<tr><td>"+$bit.SchoolID+"</td><td>"+$bit.type+"</td></tr>"}
  161.     }
  162.  $body = " Here's the latest stats on the "+$myrow.count+" P2P/MIS machines currently categorized by Sophos.  There are $networks Networks at $sites Sites.  `n `n <table border=1>$myrow</table> <p> This was generated automagically from the EDU-PRD-Sophos server"
  163.  
  164. $recipients = @("ChrisA <Chris.Adams@btlancashire.co.uk>", "ChrisC <Chris.carr@btlancashire.co.uk>", "AshG <Ash.Green@btlancashire.co.uk>")
  165.  
  166. Send-MailMessage -From "NewSophos <EDU-PRD-Sophos@btlancashire.co.uk>" -To $recipients -Subject "P2P & MIS in Sophos" -Body $body -Priority High -SmtpServer "smtp.education.btlsl.co.uk" -BodyAsHtml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement