<# *********************************************************************************************************** This module contains the heading and body line CSV writing functions for Voyage files. I'm putting this into a module to avoid having to maintain code in multiple places. Version: 1.4 Author: Ira Babb Date: 11-13-2018 *********************************************************************************************************** #> ########################################################################################################### Function ParseLine ($Line, $StartPosition, $PrimaryBreakCharacter, $AlternateBreakCharacter) # ########################################################################################################### #This function is passed a string, a position in the string to start and two characters on which if #encountered the processing of the string will stop. This is used to extract information from a messy #and/or inconsistent string. #Essentially you pass it a string and tell it where you want it to stop and on what character (up to two) #you want it to stop. It will return all the characters between where you told it to start and the stop #character, not including the stop character. It also returns the position in the string where it #encountered the stop character. ########################################################################################################### #> { $ResultArray=@("string",2) $CurrentLine = 0 $LineLength = $Line.Length - 1 for ($ParseLoop = $StartPosition; $ParseLoop -le $LineLength; $ParseLoop++) { If ($Line[$ParseLoop] -eq $PrimaryBreakCharacter -or $Line[$ParseLoop] -eq $AlternateBreakCharacter) { $CurrentLine = $ParseLoop $ParseLoop = $LineLength } Else { $PrimaryValue += $Line[$ParseLoop] } } #Push the character array into a string #Why not use -join? PS1 foreach ($Character in $PrimaryValue) {$EndString+=$Character} $ResultArray[0] = $PrimaryValue $ResultArray[1] = [int]$CurrentLine Return $ResultArray } ########################################################################################################### Function Write-VoyageCSVHead ($CSVFilePassed) { <# *********************************************************************************************************** This function writes the header line for the Voyage CSV file *********************************************************************************************************** #> $Outline = "id,account.bdc,account.geographic_location,account.lead_tech,account.manager,account.mrr,account.name,account.number,account.primary_contact,account.status,account.team_name,account.type,automation_opt_out,device.customer_managed,device.hostname,device.make_model,device.online_status,device.os,device.os_name,device.primary_ip,device.status,device.type,do_not_power_on,device.has_doubletake,device.has_lync,device.has_mbu,device.has_mssql,device.has_mssql_support,device.has_mysql,device.has_mysql_support,device.has_oracle,device.has_oracle_support,device.has_rackconnect,device.has_rcg,device.has_sharepoint,device.has_vmr,device.has_vormetric,device.icon_url,move_group,device.processtype,currentDC,migrationDC" Set-Content $CSVFilePassed $Outline } ########################################################################################################### Function Get-VoyageToken () { <# *********************************************************************************************************** This function gets an up to date Voyage token *********************************************************************************************************** #> $AuthURL = "https://identity-internal.api.rackspacecloud.com/v2.0/tokens" $LiguanURI = "https://voyage.res.rackspace.com/liguan" $credential = Get-Credential -Message "Enter your user name and SSO password (not RSA)" $Body = ConvertTo-Json @{"auth"= @{"RAX-AUTH:domain" = @{"name" = "Rackspace"};"passwordCredentials"=@{"username" = $credential.UserName;"password" = $($credential.GetNetworkCredential().Password)}}} -Depth 3 $Global:RackerToken = (Invoke-RestMethod -Uri $AuthURL -Method POST -ContentType "application/json; charset=utf-8" -Body $Body).access.token $Token = @{"token" = $global:RackerToken.id} | ConvertTo-Json $Global:VoyageToken = (Invoke-RestMethod -Method POST -Uri $LiguanURI -Body $Token -ContentType "application/json").token } ########################################################################################################### Function Get-MigrationList () { <# *********************************************************************************************************** This function takes the current pulled device an outputs the account details *********************************************************************************************************** #> $MigrationURI = "https://voyage.res.rackspace.com/api/migrations" $MigrationList = Invoke-RestMethod -Uri $MigrationURI -ContentType "application/json" -Method "GET" -Headers @{"X-Auth-Token"=$Global:VoyageToken} -TimeoutSec 120 return $MigrationList.migrations } ########################################################################################################### Function Get-Milestones ($MigrationID) { <# *********************************************************************************************************** This function takes the current pulled device an outputs the account details *********************************************************************************************************** #> $MilestoneURI = "https://voyage.res.rackspace.com/api/migrations/$MigrationID/milestones" $MilestoneList = Invoke-RestMethod -Uri $MilestoneURI -ContentType "application/json" -Method "GET" -Headers @{"X-Auth-Token"=$Global:VoyageToken} -TimeoutSec 120 return $MilestoneList.milestones } ########################################################################################################### Function Get-MigrationDevices ($MigrationID) { <# *********************************************************************************************************** This function takes the current pulled device an outputs the account details *********************************************************************************************************** #> $MigDeviceURI = "https://voyage.res.rackspace.com/api/migrations/$MigrationID/devices" $MigDeviceList = Invoke-RestMethod -Uri $MigDeviceURI -ContentType "application/json" -Method "GET" -Headers @{"X-Auth-Token"=$Global:VoyageToken} -TimeoutSec 120 return $MigDeviceList.devices } ########################################################################################################### Function Write-VoyageCSVLine ($MoveGroup,$CSVFilePassed,$PulledDevicePassed) { <# *********************************************************************************************************** This function takes the current pulled device an outputs the account details *********************************************************************************************************** #> #Eliminating phoney FQDN $Hostname,$Position= ParseLine $PulledDevicePassed.device.hostname 0 "." " " #Converting the true/false values in NSI to Voyage-appropriate values switch ($PulledDevicePassed.automation_opt_out) { "FALSE" {$AutomationStatus = "Opted In"} "TRUE" {$AutomationStatus = "Opted Out"} default {$AutomationStatus = "Opted In"} } #Performing some data sanitization. There are some fields in NSI that need to be massaged slightly due to inconsistent data within Core if ($PulledDevicePassed.process.os_type -eq "External Storage") {$ProcessType = "Storage External"} elseif ($PulledDevicePassed.process.os_type -eq "Attached Storage") {$ProcessType = "Storage Attached"} elseif ($PulledDevicePassed.process.os_type -eq "Microsoft VM") {$ProcessType = "Windows VM"} elseif ($PulledDevicePassed.process.os_type -eq "Microsoft") {$ProcessType = "Windows"} else {$ProcessType = $PulledDevicePassed.process.os_type} #Performing a check for source and destination DCs. Sometimes these come through blank. ###################################################################################### #Construct the appropriate URI based on MG if ($PulledDevicePassed.environment.current.data_center -eq $Null -or $PulledDevicePassed.environment.current.data_center -eq "" ) { $SourceDC = (ParseLine $MoveGroup 0 "-" ".")[0] } else { $SourceDC = $PulledDevicePassed.environment.current.data_center } if ($PulledDevicePassed.environment.migration.data_center -eq $Null -or $PulledDevicePassed.environment.migration.data_center -eq "") { $DestDC = "IAD3" } else { $DestDC = $PulledDevicePassed.environment.migration.data_center } #Building and writing the output for the CSV file $Outline = $PulledDevicePassed._id+"="+$PulledDevicePassed.account.bdc+"="+$PulledDevicePassed.account.geographic_location+"="+$PulledDevicePassed.account.lead_tech+"="+$PulledDevicePassed.account.manager+"="+$PulledDevicePassed.account.mrr+"="+$PulledDevicePassed.account.name+"="+$PulledDevicePassed.account.number+"="+$PulledDevicePassed.account.primary_contact+"="+$PulledDevicePassed.account.status+"="+$PulledDevicePassed.account.team_name+"="+$PulledDevicePassed.account.type+"="+$AutomationStatus+"="+$PulledDevicePassed.device.customer_managed+"="+$PulledDevicePassed.device.hostname+"="+$PulledDevicePassed.device.make_model+"="+$PulledDevicePassed.device.online_status+"="+$PulledDevicePassed.device.os+"="+$PulledDevicePassed.device.os_name+"="+$PulledDevicePassed.device.primary_ip+"="+$PulledDevicePassed.device.status+"="+$PulledDevicePassed.device.type+"="+$PulledDevicePassed.do_not_power_on+"="+$PulledDevicePassed.has_doubletake+"="+$PulledDevicePassed.has_lync+"="+$PulledDevicePassed.has_mbu+"="+$PulledDevicePassed.has_mssql+"="+$PulledDevicePassed.has_mssql_support+"="+$PulledDevicePassed.has_mysql+"="+$PulledDevicePassed.has_mysql_support+"="+$PulledDevicePassed.has_oracle+"="+$PulledDevicePassed.has_oracle_support+"="+$PulledDevicePassed.has_rackconnect+"="+$PulledDevicePassed.has_rcg+"="+$PulledDevicePassed.has_sharepoint+"="+$PulledDevicePassed.has_vmr+"="+$PulledDevicePassed.has_vormetric+"="+$PulledDevicePassed.device.icon_url+"="+$PulledDevicePassed.move_group+"="+$ProcessType+"="+$SourceDC+"="+$DestDC #Removing any commas in the output $Outline = $Outline | foreach {$_ -replace ",", ""} #Replacing = with commas to make this CSV work $Outline = $Outline | foreach {$_ -replace "=", ","} Add-Content $CSVFilePassed $Outline } ###########################################################################################################