#============================================================================= # # File Name : AD-MigDBcopy.ps1 # Author : Jay Pieren # Date : Jan 2015 # Version : V1 # # Description of script functionality # # # Files: # - DataCollector-to-SPS.ps1 : The main script # # Requirements: # - PowerShell # - SPS client lib # - File Share # # Usage: # 1) start up PowerShell # 2) run the following command './AD-MigDBcopy.ps1 -Org name [-simulation] [-verbose] # # Note: with the switch '-Verbose' you can show additional log information # #============================================================================= [CmdletBinding()] param( [string]$inputFN = "", [switch]$leasedel = $false, [switch]$simulation = $true ) function usage { write-host "" write-host "usage DataCollector-to-SPS.ps1 [-Org] [-Verbose] [-simulation:$true/$false]" write-host " -Verbose show detailed process information" write-host " -ISCdhcp defauklt fals, from KEA dhcp" write-host " -simulation just simulates the file moves, default = true" write-host "" } #======================================================================================================= # Script variables #------------------------------------------------------------------------------------------------------- $error.Clear() $global:Delimiter = "," $globaL:ArrayDelimiter = "|" $scriptName = "opnsensedhcpwriteres" # define various environment variables $script_path = $(Get-Item $($MyInvocation.MyCommand.Path)).DirectoryName Import-Module Microsoft.PowerShell.SecretManagement Import-Module Microsoft.PowerShell.SecretStore $Environments = @{ 'prod'=@{ 'InputPath' = '/Volumes/Media/network'; 'psglobal' = '/Volumes/Media/psh-global'; 'ArchiveDir' = '/Volumes/Media/network'; 'Site' = 'https://opnsense.pieren.arpa:8443/api'; 'apicmd' = @{ 'kea' = @{ 'getSubent' = @('Dhcpv4','GET'); 'searchSubnet' = @('Dhcpv4','GET'); 'getReservation' = @('Dhcpv4','GET'); 'searchReservation' = @('Dhcpv4','GET'); 'addReservation' = @('Dhcpv4','POST'); 'lease4-del' = @('Dhcpv4','POST'); 'search' = @('leases4','POST'); 'status' = @('service','GET'); } 'acemclient' = @{ 'search' = @('certificates','Get'); } 'dhcpv4' = @{ 'searchLease' = @('leases','Get'); 'status' = @('service','Get'); } } 'LogDir' = '/Volumes/Media/network'; 'AdminUser' = 'hb0J8Ik4Chlnopw1VPs7xd9ULxcJ7Kn2fiT+iqBWMaGItaPrzoh0djQBzYu9gA3qCWlwUXfRXS0NZQM6'; 'adminpwd' = 'xEjrxfmR9aXojkUwDrUotjObkIbsCMQT8QYmX2i35dO5dQG6xvKF0+m5gBk1AI8KVYQfmFrLcalU5Zq7'; 'delimiter' = ','; }; 'Q'=@{ } } # Script functions #------------------------------------------------------------------------------------------------------- $Environment='Prod' # check envexists if ([string]::IsNullOrEmpty($Environment)) { #no valid input write-Error "Can not resolve environment, Abort!" exit 0 } # set directories $InputPath = $Environments.Item($Environment).InputPath $PSglobal = $Environments.Item($Environment).psglobal $Site = $Environments.Item($Environment).Site $apicmds = $Environments.Item($Environment).apicmd $LogPath = $Environments.Item($Environment).LogDir $AdminUser = $Environments.Item($Environment).AdminUser $adminpwd = $Environments.Item($Environment).adminpwd $delim = $Environments.Item($Environment).delimiter $EnvironmentUser = $env:USER $Computername= $(Hostname) . $PSglobal/globalfunctions.ps1 $AdminUserpwdhash = Get-Hash -env $Environment -EnvUser $EnvironmentUser -compi $Computername -id $AdminUser -path $PSglobal $AdminUserpwd = ConvertTo-SecureString $AdminUserpwdhash -ErrorAction SilentlyContinue -ErrorVariable E # $MailFrom = "datacollector@" $MailTo = "jay.pieren@" $MailHost = "smtp.swissport.aero" $FileError1 = "Check locked: file:{0} error on {1} to {2};Error: {3}" $FileError2 = "Archive: file:{0} error on {1} to {2};Error: {3}" $FileError3 = "copy to target: file:{0} error on {1} to {2};Error: {3}" [array]$ArrayError = @() $scriptName = "opnsense-dhcp-read-res" $dt = Get-Date -f "yyyy-MM-dd-hh-mm" $LogFN = "$LogPath\$scriptname-$dt.log" $FN = "$InputPath\$inputFN" $inputDic = createDictionary2FromCSV -csvFile $FN -key "MAC-Address" -columns @('IP-Address','Hostname','Description') -Delim $delim -descr 'MAC check' $inputDicMac = $inputDic[0] $errList = $inputDic[1] $inputDic = createDictionary2FromCSV -csvFile $FN -key "IP-Address" -columns @('MAC-Address','Hostname','Description') -Delim $delim -descr 'IP check' $inputDicIP = $inputDic[0] $errList2 = $inputDic[1] If ([string]::IsNullOrEmpty($errList) -or [string]::IsNullOrEmpty($errList2)) { Write-Host Write-Host write-host "Input file $FN contains duplicate MAC and/or IP addresses]" $errList exit } $data = Import-CSV $FN -Delimiter $delim -Encoding UTF8 Logging -Msg ("Items to process found:{0}" -f $data.Count) -Logfile $LogFN -OutputToScreen -ForegroundColor Magenta # Build auth header $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $AdminUser, $adminpwd))) # Set proper headers $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo)) $headers2 = $headers $headers2.Add('Content-Type','application/json') $Error.Clear() [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #get all res # prepare opnsense connect and chech login and get current resvweration to check against $module = 'kea' $cmd = 'searchReservation' $cmdstruc = $apicmds.$module.$cmd $controller = $cmdstruc[0] $method = $cmdstruc[1] $URI = ("{0}/{1}/{2}/{3}" -f $site, $module, $controller, $cmd) try { $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $Uri -SessionVariable "ws" } catch { $msg = ("{0}: Can't connect to opnsense. Abort {1}!" -f $dt, $SnowUriBase) $error[0] Logging -Msg $msg -Logfile $LogFN -OutputToScreen -ForegroundColor Red $body = Get-Content $LogFN EmailAlert -Subject $msg -body $($body -join "`n") exit } [array]$requests = $response $sheet = $response | ConvertTo-csv $hashKeaMac = createHashFromArray -data $response.rows -key "hw_address" -columns @('ip_address','hostname','description') -Delim ',' $hashKeaIP = createHashFromArray -data $response.rows -key "ip_address" -columns @('hw_address','hostname','description') -Delim ',' foreach ($row in $data) { #check IP $key = $row."IP-Address" if ($hashKeaIP.ContainsKey($key)) { wire-host "IP Address in $FN already in opnsense, $key!" continue } #chek MAC $key = $row."MAC-Address" if ($hashKeaMac.ContainsKey($key)) { wire-host "Mac Address in $FN already in opnsense, $key!" continue } #All okay proceeds with add res $module = 'kea' $cmd = 'getReservation' $cmdstruc = $apicmds.$module.$cmd $controller = $cmdstruc[0] $method = $cmdstruc[1] $URI = ("{0}/{1}/{2}/{3}" -f $site, $module, $controller, $cmd) if ($simulate) { Out-File -InputObject ("Simulation: onboarding Resource: ITSMkey:{0}" -f $ItsmKey) -FilePath $FNlog -append } else { # prepare opnsense connect and chech login try { $response = Invoke-RestMethod -Headers $headers -Method $method -Uri $Uri -SessionVariable $ws } catch { $msg = ("{0}: Can't perocess to opnsense. Abort {1}!" -f $dt, $URI) $error[0] Logging -Msg $msg -Logfile $LogFN -OutputToScreen -ForegroundColor Red $body = Get-Content $LogFN EmailAlert -Subject $msg -body $($body -join "`n") exit } [string]$net = $response.reservation.subnet $net = $net -replace '@{','' $net = $net -replace '=}','' [string]$ip_address = $row."IP-Address"; [string]$hw_address = $row."MAC-Address"; [string]$hostname = $row."Hostname"; [string]$description = $row."Description"; $body = @{ reservation=@{ subnet = $net; ip_address = $ip_address; hw_address = $hw_address; hostname = $hostname; description = $description; }; } $body = $body | ConvertTo-Json # $body = @" # { # "reservation": # { # "subnet": "$net", # "ip_address": "$ip_address", # "hw_address": "$hw_address", # "hostname": "$hostname", # "description": "$description" # } # } # "@ $module = 'kea' $cmd = 'addReservation' $cmdstruc = $apicmds.$module.$cmd $controller = $cmdstruc[0] $method = $cmdstruc[1] $URI = ("{0}/{1}/{2}/{3}" -f $site, $module, $controller, $cmd) if ($simulate) { Out-File -InputObject ("Simulation: onboarding resevration: ITSMkey:{0}" -f $ItsmKey) -FilePath $FNlog -append } else { # addReservation try { $req = Invoke-RestMethod -Headers $headers2 -Method $method -Uri $URI -Body $body -SessionVariable $ws -SkipCertificateCheck } catch { $msg = ("{0}: Can't perocess to opnsense. Abort {1}!" -f $dt, $URI) $error[0] Logging -Msg $msg -Logfile $LogFN -OutputToScreen -ForegroundColor Red $body = Get-Content $LogFN EmailAlert -Subject $msg -body $($body -join "`n") exit } if ($req.result -eq "failed") { $msg = ("{0}: could'nt add reserevation {1}!" -f $dt, $row."IP-Address") $error[0] Logging -Msg $msg -Logfile $LogFN -OutputToScreen -ForegroundColor Red } else{ $msg = ("{0}: added reserevation {1}!" -f $dt, $row."IP-Address") Logging -Msg $msg -Logfile $LogFN -OutputToScreen -ForegroundColor green } } } } $FN = "$InputPath\kea-reservations-$dt.csv" $sheet = $response.rows | ConvertTo-csv Out-File -InputObject $sheet -Encoding utf8 -LiteralPath $FN Logging -Msg ("Items to process found:{0}" -f $requests.Count) -Logfile $LogFN -OutputToScreen -ForegroundColor Magenta