<# .DESCRIPTION This script will generate an Excel spreadsheet with accounts from a move group classified as Linux-only, Windows-only, Network-only or mixed. .EXAMPLE .\DNPOClassification.ps1 -MG MG-17 Classifies all accounts in MG-17 #> Param ( [Parameter(Mandatory=$false,Position=0)][string]$Version = "1.0", #Script version [Parameter(Mandatory=$True,Position=0)][string]$MG, #Move group [switch]$IAD3, #Show all IAD3 devices [switch]$GUI, #Are we invoking this from the GUI [switch]$Dropouts #User wants to see the items we're not capturing in the \Dropouts directory ) ############################################################################################################################# Function Write-DeviceException ($Group,$Server,$Directory) <# *********************************************************************************************************** This function outputs unclassified devices to a dump folder for later investigation. *********************************************************************************************************** #> { $Outline = $Server._id+","+$Server.device.hostname+","+$Server.device.os+","+$Server.device.os_name $OutputFile = $Directory+$Group+"-Accounts.csv" add-content $OutputFile $Outline } ############################################################################################################################# ###################################################################################### #Getting the current directory and importing the CTKCore module $Dir=(Get-Item -Path . -Verbose).FullName #If we're executing from the GUI the module directory will be different if ($GUI) { $CTKModule = $Dir+"\Modules\CTKCore\CTKCore.psm1" $ExcelModule = $Dir+"\Modules\ExcelHandlers\ExcelHandlers.psm1" } else { $CTKModule = [string]((get-item $Dir).parent.fullname)+"\Modules\CTKCore\CTKCore.psm1" $ExcelModule = [string]((get-item $Dir).parent.fullname)+"\Modules\ExcelHandlers\ExcelHandlers.psm1" } Import-Module $CTKModule -force -DisableNameChecking Import-Module $ExcelModule -force -DisableNameChecking ###################################################################################### #Setting TLS to v1.2 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ###################################################################################### #Checking CORE token try { $Testing = Get-CoreDevice -Device 879665 } catch { Get-CoreToken } ###################################################################################### #Getting start time $Stopwatch = [system.diagnostics.stopwatch]::startNew() #NSI URI if ($IAD3) { $URI = "https://dbapi.nsi.rackspace.com/migration/iad3/Find" } else { $URI = "https://dbapi.nsi.rackspace.com/migration/dfw1/Find" } #Where we'll save the eventually created Excel file $UniqueAccounts = @() #Array of unique accounts $Column = 1 #Column tracking variable $Row = 1 #Row tracking variable $Dir=(Get-Item -Path . -Verbose).FullName $Destination_File = $Dir+"\"+$MG+" DNPO Classifications.xlsx" $DropoutsDir = $Dir+"\Dropouts\" ###################################################################################### #Querying the move group $JSON = @" { "query": { "move_group":"$MG" } } "@ ###################################################################################### #Cleaning our old dropouts if ($Dropouts) { if (!(test-path $DropoutsDir)) { new-item $DropoutsDir -type directory | Out-Null } $OldDropouts = $DropoutsDir+"\*Accounts.csv" remove-item $OldDropouts } ###################################################################################### #Querying NSI $Results = Invoke-RestMethod -Uri $URI -Body "$JSON" -ContentType "application/json" -Method "POST" -Headers @{"X-Auth"=$Global:Token} -TimeoutSec 120 ###################################################################################### #Iterating through the devices in the results Write-Progress -Activity "Analyzing devices in move group $MG." -Status "%0 Complete" -PercentComplete 0 $CurrentDevice = 0 foreach ($Device in $Results.result) { $CurrentDevice++ $DevicePercentageComplete = ($CurrentDevice/$Results.result.count)*100 $DevicePercentageComplete = "{0:N0}" -f $DevicePercentageComplete Write-Progress -Activity "Analyzing devices in move group $MG." -Status "%$DevicePercentageComplete Complete" -PercentComplete $DevicePercentageComplete if (!($Device.device.status -like "*Deleted*")) { ###################################################################################### #Setting default account to True by default $UniqueAccount = $True ###################################################################################### #Iterate through the existing accounts to see if we've gathered this particular account. Set $UniqueAccount to False if we have this already on record. foreach ($Account in $UniqueAccounts) { if ($Device.account.number -eq $Account.Number) {$UniqueAccount = $False} } ###################################################################################### #If this is a uniqe account we'll establish it in our account array if ($UniqueAccount) { $ServiceLevel = (Get-CoreAccountSegment $Device.account.number)."segment.name" $AccountPSO = [PSCustomObject]@{ Name = $Device.account.name; Number = $Device.account.number; ServiceLevel = $ServiceLevel; AM = $Device.account.manager LT = $Device.account.lead_tech Team = $Device.account.team_name Linux = 0; Network = 0; Windows = 0 } $UniqueAccounts+=$AccountPSO } ###################################################################################### #Classifying each device and incrementing the appropriate counter foreach ($Account in $UniqueAccounts) { if ($Device.account.number -eq $Account.Number) { Switch -casesensitive ($Device.device.os_name) { #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Microsoft" #Microsoft device { if ($Device.device.make_model -like "*inux*") {$Account.Linux++} elseif ($Device.device.os -notlike "*Cluster*") {$Account.Windows++} } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Linux" #Linux device { if ($Device.device.os -notlike "*Cluster*") {$Account.Linux++} } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Other" #"Other" { if ($Device.device.os -like "*Cisco*" -or $Device.device.os -like "*Load-Balancer*") { $Account.Network++ } elseif ($Dropouts) {Write-DeviceException $MG $Device $DropoutsDir} } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Unknown" { ######################################################################################****************** #If Hyper-V determine what kind of device if ($Device.device.make_model -like "*Hyper-V*" -or $Device.device.make_model -like "*Microsoft Cloud OS") { ######################################################################################****************** #Check if VM if ($Device.device.make_model -like "*for Microsoft*") { ######################################################################################****************** #Check OS if ($Device.device.make_model -like "*inux*") {$Account.Linux++} else {$Account.Windows++} } } ######################################################################################****************** #OpenStack Hypervisor elseif ($Device.device.os -like "*DELL*" -or $Device.device.os -like "*HP*") { #Rando Windows device if ($Device.device.os -like "*Windows*") { $Account.Windows++ } #Rando Linux device elseif ($Device.device.os -like "*Linux*") { $Account.Linux++ } } elseif ($Device.device.os -like "*Web Application*") {$Account.Network++} #Drop everything we're not catching into a text file elseif ($Dropouts) {Write-DeviceException $MG $Device $DropoutsDir} } default {if ($Dropouts) {Write-DeviceException $MG $Device $DropoutsDir}} } } } } } ###################################################################################### #Sorting the sheet $UniqueAccounts = $UniqueAccounts|sort-object -property Name ###################################################################################### #Creating the Excel object $Excel = New-Object -ComObject excel.application $Excel.visible = $False $Excel.displayalerts = $False $OutExcel = $Excel.Workbooks.Add() ###################################################################################### #Creating Excel sheets $OutSheet = $OutExcel.Worksheets.Item(1) $OutSheet.Name = "Account Type" ###################################################################################### #Moving to the OS sheet $OutSheet = $OutExcel.Worksheets.Item("Account Type") $OutSheet.Activate()|out-null ###################################################################################### #Writing headings $BeginRow = $Row $BeginColumn = $Column $OutSheet.cells.Item($Row,$Column) = "Account Number" $Column++ $OutSheet.cells.Item($Row,$Column) = "Account Name" $Column++ $OutSheet.cells.Item($Row,$Column) = "AM" $Column++ $OutSheet.cells.Item($Row,$Column) = "LT" $Column++ $OutSheet.cells.Item($Row,$Column) = "SLA" $Column++ $OutSheet.cells.Item($Row,$Column) = "Environment" $Column++ $OutSheet.cells.Item($Row,$Column) = "Windows" $Column++ $OutSheet.cells.Item($Row,$Column) = "Linux" $Column++ $OutSheet.cells.Item($Row,$Column) = "Network" $Column++ $OutSheet.cells.Item($Row,$Column) = "Assigned" $Row++ ###################################################################################### #Going through each of our accounts in the $UniqueAccounts array and outputting their classification $CurrentAccount = 0 Write-Progress -Activity "Writing devices in move group $MG." -Status "%0 Complete" -PercentComplete 0 -id 1 foreach ($Account in $UniqueAccounts) { $CurrentAccount++ $AccountPercentageComplete = ($CurrentAccount/$UniqueAccounts.count)*100 $AccountPercentageComplete = "{0:N0}" -f $AccountPercentageComplete Write-Progress -Activity "Writing devices in move group $MG." -Status "%$AccountPercentageComplete Complete" -PercentComplete $AccountPercentageComplete -id 1 ###################################################################################### $Column = 1 $OutSheet.cells.Item($Row,$Column) = $Account.Number $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.Name $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.AM $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.LT $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.ServiceLevel $Column++ ###################################################################################### #Classifying the account based on device counts if ($Account.ServiceLevel -like "*Managed Colocation*") { $OutSheet.cells.Item($Row,$Column) = "Managed Colocation" } else { if ($Account.Linux -gt 0) { if ($Account.Windows -gt 0) {$OutSheet.cells.Item($Row,$Column) = "Mixed"} else {$OutSheet.cells.Item($Row,$Column) = "Linux"} } elseif ($Account.Windows -gt 0) {$OutSheet.cells.Item($Row,$Column) = "Windows"} elseif ($Account.Network -gt 0) {$OutSheet.cells.Item($Row,$Column) = "Network Only"} else {$OutSheet.cells.Item($Row,$Column) = "Unknown"} } $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.Windows $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.Linux $Column++ $OutSheet.cells.Item($Row,$Column) = $Account.Network $Column++ $Row++ } $Row-- MakeTable "Accounts" $BeginColumn $BeginRow $Column $Row $OutSheet $Excel $OutExcel.Saveas($Destination_File) $OutExcel.Close($True) $Excel.Quit() [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Excel) | Out-Null Remove-Variable Excel ###################################################################################### #Timing functions $Stopwatch.stop() write-host write-host "Complete." -foregroundcolor "green" write-host write-host "Execution time: "$Stopwatch.Elapsed.Minutes"minutes"$Stopwatch.Elapsed.Seconds"seconds." -foregroundcolor "green" write-host