Param ( [Parameter(Mandatory=$false,Position=0)][string]$Version = "1.6", #Script version [Parameter(Mandatory=$true,Position=0)][string]$MG, #Move group [switch]$GUI, #Are we invoking this from the GUI [switch] $IAD2 #User wants to see the items we're not capturing in the \Dropouts directory ) ###################################################################################### #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 ###################################################################################### #Getting start time $Stopwatch = [system.diagnostics.stopwatch]::startNew() ###################################################################################### #Checking CORE token and getting the token if it doesn't exist try {$Testing = Get-CoreDevice -Device 879665} catch {Get-CoreToken} #******************************** #Excel border definitions ******* #******************************** $BorderLeft = 7 # $BorderTop = 8 # $BorderBottom = 9 # $BorderRight = 10 # $BorderInsideVertical = 11 # $BorderInsideHorizontal = 12 # #******************************** #Variable definition #Creating move groups if ($IAD2) {$URI = "https://dbapi.nsi.rackspace.com/migration/iad2/Find"} else {$URI = "https://dbapi.nsi.rackspace.com/migration/dfw1/Find"} ###################################################################################### #Creating file paths and setting up variables $Dir=(Get-Item -Path . -Verbose).FullName $TimeStamp = Get-Date -Format d | foreach {$_ -replace "/", "-"} $Destination_File = $Dir+"\$MG Account List - "+$TimeStamp+".xlsx" $Destination_CSV = $Dir+"\$MG Upload List - "+$TimeStamp+".csv" $AMColumn = 1 $AMRow = 1 $CLRow = 1 $CLColumn = 1 $DLRow = 1 $DLColumn = 1 $Sheetcount = 1 $UniqueAccounts=@() $UniqueAMs=@() #Creating the Excel object ###################################################################################### $Excel = New-Object -ComObject excel.application $Excel.visible = $False $Excel.displayalerts = $False $OutExcel = $Excel.Workbooks.Add() $OutSheet = $OutExcel.Worksheets.Item(1) $OutSheet.Name = "Customer List" ###################################################################################### #Writing row headings on the Overview sheet $OutSheet.cells.Item($CLRow,$CLColumn) = "Account Name" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Account Number" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "AM" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "LT" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Status" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Service Level" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Team" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "MRR" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Geographic Location" $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = "Move Group" #Writing CSV row headings ###################################################################################### $Outline = "account_number,computer,account_name" Set-Content $Destination_CSV $Outline #Writing CSV row headings ###################################################################################### <###################################################################################### #Moving to the MG sheet $OutSheet = $OutExcel.Worksheets.Item("$MG") $OutSheet.Activate()|out-null#> ###################################################################################### #JSON generation $JSON = @" { "query": { "move_group":"$MG" } } "@ ###################################################################################### #Querying NSI $Results = Invoke-RestMethod -Uri $URI -Body "$JSON" -ContentType "application/json" -Method "POST" -Headers @{"X-Auth"=$Global:Token} -TimeoutSec 120 ###################################################################################### #Tracking and writing progress#Iterating through the devices $CurrentDevice = 0 #Progress variable Write-Progress -Activity "Analyzing devices in move group $MG." -Status "%0 Complete" -PercentComplete 0 Write-Progress -Activity "Writing devices in move group $MG." -Status "%0 Complete" -PercentComplete 0 -id 1 Write-Progress -Activity "Writing AMs in move group $MG." -Status "%0 Complete" -PercentComplete 0 -id 2 Foreach ($Device in $Results.Result) { ###################################################################################### #Tracking and writing progress $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 ###################################################################################### #Assuming this is a new account and a new AM $NewAccount = $True $NewAM = $True #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #Determining the type of device if ($Device.deleted -ne "true" -and $Device.device.type -notlike "*Cloud Consolidation*" -and $Device.device.type -notlike "*Custom Monitoring*" -and $Device.device.type -notlike "*Leased Line*" -and $Device.device.type -notlike "*Managed Backup*" -and $Device.device.type -notlike "*Switch*" -and $Device.device.type -notlike "*Virtual Server Cluster*" -and $Device.device.type -notlike "*Unknown*" -and $Device.device.type -notlike "*VM*" -and $Device.device.status -notlike "*Decommission*") { #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ #Checking to see if we've seen this account previously and if we have set the account to be processed Foreach ($Account in $UniqueAccounts) { if ($Device.account.number -eq $Account.Number) { $NewAccount = $False } } ###################################################################################### #Checking to see if we've seen this AM previously and if we have, add it to our AM list Foreach ($AM in $UniqueAMs) { if ($Device.account.manager -eq $AM) { $NewAM = $False } } ###################################################################################### #If this is a new account add it to our unique accounts list if ($NewAccount) { ###################################################################################### #Creating a new device PSO and setting the attribute flags to false if ((Get-CoreAccountSegment $Device.account.number)."segment.name" -like "*Managed Colocation*") { $ServiceLevel = "Managed Colocation" } else { $ServiceLevel = $Device.account.type } $AccountPSO = [PSCustomObject]@{ Name = $Device.account.name Number = $Device.account.number AM = $Device.account.manager LT = $Device.account.lead_tech MRR = $Device.account.mrr Geo = $Device.account.geographic_location Status = $Device.account.status Team = $Device.account.team_name ServiceLevel = $ServiceLevel } $UniqueAccounts+=$AccountPSO } ###################################################################################### #If this is a new account add it to our unique accounts list if ($NewAM) { $UniqueAMs+=$Device.account.manager } ###################################################################################### #Adding the device to the upload list CSV $Outline = [string]$Device.account.number+","+[string]$Device._id+","+$Device.account.name Add-Content $Destination_CSV $Outline } } #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ ###################################################################################### #Moving to the Overview sheet #$OutSheet = $OutExcel.Worksheets.Item($MG) #$OutSheet.Activate()|out-null #All devices in a MG done. Process the accounts $CurrentAccount = 0 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 $CLRow++ $CLColumn = 1 $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.Name $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.Number $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.AM $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.LT $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.Status $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.ServiceLevel $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.Team $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.MRR $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $Account.Geo $CLColumn++ $OutSheet.cells.Item($CLRow,$CLColumn) = $MG } MakeTable "$MG" "A" 1 "J" $CLRow $OutSheet $Excel $OutExcel.Saveas($Destination_File) GoHome $OutSheet ###################################################################################### #Creating the AM list tab $OutExcel.Worksheets.Add()|out-null $OutSheet = $OutExcel.Worksheets.Item(1) $OutSheet.Name = "AM List" $OutSheet.cells.Item($AMRow,$AMColumn) = "Account Manager" $CurrentAM = 0 foreach ($AM in $UniqueAMs) { $CurrentAM++ $AMPercentageComplete = ($CurrentAccount/$UniqueAccounts.count)*100 $AMPercentageComplete = "{0:N0}" -f $AMPercentageComplete Write-Progress -Activity "Writing AMs in move group $MG." -Status "%$AMPercentageComplete Complete" -PercentComplete $AMPercentageComplete -id 2 $AMRow++ $OutSheet.cells.Item($AMRow,$AMColumn) = $AM } MakeTable "$MG" "A" 1 "A" $AMRow $OutSheet $Excel GoHome $OutSheet $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