<# .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 .\AccountClassification.ps1 -MG MG-17 Classifies all accounts in MG-17 #> Param ( [Parameter(Mandatory=$false,Position=0)][string]$Version = "1.7", #Script version [Parameter(Mandatory=$True,Position=0)][string]$MG, #Move group [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 ($Server,$OutFile) <# *********************************************************************************************************** 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 add-content $OutFile $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() #Setting the collection name and move group name, configure the URI and set the JSON for polling $MG = $MG.ToUpper() $CollectionName = $($MG.Substring(0,7) -replace '-','_').ToLower() $URI = "https://dbapi.nsi.rackspace.com/migration/"+$CollectionName+"/Find" $JSON = @" { "query": { "move_group": "$MG", "deleted": {"`$ne": true} } } "@ #Where we'll save the eventually created Excel file $Destination_File = ([Environment]::GetFolderPath("Desktop"))+"\$MG Account Classification.xlsx" #Our Account classification file $UniqueAccounts = @() #Array of unique accounts $Column = 1 #Column tracking variable $Row = 1 #Row tracking variable $DropoutsDir = ([Environment]::GetFolderPath("Desktop"))+"\Dropouts\" ###################################################################################### #Cleaning our old dropouts if ($Dropouts) { if (!(test-path $DropoutsDir)) { new-item $DropoutsDir -type directory | Out-Null } $OldDropouts = $DropoutsDir+"\*Account Classification Dropouts.csv" remove-item $OldDropouts $DropoutFile = $DropoutsDir+"\"+$MG+"-Account Classification Dropouts.csv" $Outline = "DeviceNumber,Hostname,device.os,os_name" set-content $DropoutFile $Outline } ###################################################################################### #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 ###################################################################################### #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 ESX = 0; 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 -wildcard ($Device.process.os_type) { #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Windows*" #Microsoft devices { if ($Device.account.number -eq "828883") { write-host $Device._id write-host $Device.process.os_type write-host $Device.device.status write-host } $Account.Windows++ } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "Linux*" #Linux devices { $Account.Linux++ } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "*Firewall*" #Network devices { $Account.Network++ } "*Load-Balancer*" #Network devices { $Account.Network++ } "*WAF*" #Network devices { $Account.Network++ } "Leased Line" { $Account.Network++ } } } } } ################################################################################### #Sorting the sheet $UniqueAccounts = [array]($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) = "Team" $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) = "Hypervisors" $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 = ($CurrentAccount/($UniqueAccounts|Measure-Object).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++ $OutSheet.cells.Item($Row,$Column) = $Account.Team $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++ $OutSheet.cells.Item($Row,$Column) = $Account.Hypervisors $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