-
-
Save ITJamie/6d9ceb0d398ea20a799e69cd959ac6b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Powershell refuses to connect to the Netbox API on our setup without this. | |
| add-type @" | |
| using System.Net; | |
| using System.Security.Cryptography.X509Certificates; | |
| public class TrustAllCertsPolicy : ICertificatePolicy { | |
| public bool CheckValidationResult( | |
| ServicePoint srvPoint, X509Certificate certificate, | |
| WebRequest request, int certificateProblem) { | |
| return true; | |
| } | |
| } | |
| "@ | |
| [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy | |
| [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 | |
| # Some useful settings. | |
| $token = "<your token>" | |
| $uri = "https://netbox.domain.com/api" | |
| # These are some ID's in Netbox | |
| # We added a "Hyper-V" cluster type. This needs to match yours! | |
| # Type 8 for us is "Server". | |
| $NetboxHyperVClusterType = 3 | |
| $NetboxServerRoleID = 8 | |
| # Set API Headers | |
| $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" | |
| $headers.Add("Authorization", "Token $token") | |
| $headers.Add("Content-Type", 'application/json') | |
| $headers.Add("Accept", 'application/json') | |
| # If we're not in a cluster, we use "Standalone - $Hostname" as a cluster name instead. | |
| # We try to see if the cluster exists, if it does, we take it's ID for later. If not, we create it. | |
| $clusterName = Get-Cluster -Domain $($env:USERDNSDOMAIN) | Get-ClusterNode | where {$_.Name -eq $(hostname)} | select cluster | |
| if($clusterName -eq $null) {$clusterName = "Standalone-$(hostname)"} | |
| $clusterGet = (Invoke-WebRequest -Uri "$uri/virtualization/clusters/?name=$clusterName" -Method Get -Headers $headers).content | convertfrom-json | |
| if($clusterGet.count -ne "0") { | |
| # The cluster exists, we're getting it now. | |
| $clusterID = $clusterGet.results.id | |
| } | |
| else { | |
| # The cluster doesn't exist. Let's add it ! | |
| $clusterId = ((Invoke-WebRequest -Uri "$uri/virtualization/clusters/" -Method Post -Body $(@{name=$clusterName;type=$hyperVClusterType}| Convertto-json) -Headers $headers).content | convertfrom-json).id | |
| } | |
| # Getting the list of VM from Hyper-V. This script needs to be run as admin on the local server. | |
| $vms = Get-VM | |
| foreach($vm in $vms) { | |
| # Resetting these to nothing. | |
| $virtualMachineBody = @{} | |
| $virtualMachineID = "" | |
| $ip = "" | |
| $ipconcat="" | |
| $ipID = "" | |
| # NSlookup that bad boy. | |
| try {$ip="$(([System.Net.Dns]::GetHostAddresses($vm.VMName).IpAddressToString).ToString())"} catch {$ip = ""} | |
| if ($vm.state -eq "Running" -and $ip -ne "") { | |
| $ipconcat=$ip + "/" + "24" | |
| # We check if the IP already exists. | |
| if(((Invoke-WebRequest -Uri "$uri/ipam/ip-addresses/?q=$ip" -Headers $headers -Method Get).content | convertfrom-json).count -gt 0) { | |
| $ipID = ((Invoke-WebRequest -Uri "$uri/ipam/ip-addresses/?q=$ip" -Headers $headers -Method Get).content | convertfrom-json).results.id[0] | |
| } | |
| else { | |
| # We create an ip address object. | |
| $ipID = ((Invoke-WebRequest -Uri "$uri/ipam/ip-addresses/" -Method POST -Headers $headers -Body $(@{address=$ipconcat} | Convertto-json)).content | convertfrom-json).results.id | |
| } | |
| write-host "$($vm.name) IPID : $ipID" | |
| # status=1 => Active | |
| $virtualMachineBody = $(@{ | |
| name=$($vm.VMName) | |
| cluster=$clusterId | |
| status=1 | |
| role=$NetboxServerRoleID | |
| vcpus=$($vm.ProcessorCount) | |
| memory=$(if($vm.DynamicMemoryEnabled){$vm.MemoryMaximum/1MB}else{$vm.MemoryStartup/1MB}) | |
| primary_ip4=$ipID | |
| disk=$(((($vm.HardDrives | Get-VHD).Size) | Measure-Object -Sum).Sum/1GB) | |
| } | ConvertTo-Json) | |
| } else { | |
| # This is if the VM isn't running. We'll just add it here. | |
| # status=0 => Offline | |
| $virtualMachineBody = $(@{ | |
| name=$($vm.VMName) | |
| cluster=$clusterId | |
| status=0 | |
| role=$NetboxServerRoleID | |
| vcpus=$($vm.ProcessorCount) | |
| memory=$(if($vm.DynamicMemoryEnabled){$vm.MemoryMaximum/1MB}else{$vm.MemoryStartup/1MB}) | |
| disk=$(((($vm.HardDrives | Get-VHD).Size) | Measure-Object -Sum).Sum/1GB) | |
| }| ConvertTo-Json) | |
| } | |
| if(((Invoke-WebRequest -uri "$uri/virtualization/virtual-machines/?q=$($vm.name)" -Method Get -Headers $headers).content | convertfrom-json).count -gt 0) { | |
| # We get the first VM that matches. | |
| $virtualMachineID = ((Invoke-WebRequest -uri "$uri/virtualization/virtual-machines/?q=$($vm.name)" -Method Get -Headers $headers).content | convertfrom-json).results.id[0] | |
| } | |
| else { | |
| # We create the virtual Machine object. | |
| $virtualMachineID = ((Invoke-WebRequest -uri "$uri/virtualization/virtual-machines/" -Method POST -Body $virtualMachineBody -Headers $headers).content | convertfrom-json).results.id | |
| } | |
| write-host "$($vm.name) VMID : $virtualMachineID" | |
| <# | |
| # We check if the VM is running and if the NSLOOKUP worked. | |
| # If it does, we add the IP to Netbox and link it to the VM. | |
| #> | |
| if($vm.State -eq "Running" -and $ip -ne "") { | |
| # We create an interface object. | |
| $interfacePK = (Invoke-WebRequest -Uri "$uri/virtualization/interfaces/" -method Post -Headers $headers -body $(@{name='ethernet';virtual_machine=$virtualMachineID} | Convertto-json)) | |
| # We create an interface object. | |
| Invoke-WebRequest -Uri "$uri/ipam/ip-addresses/$ipID/" -Method Patch -Headers $headers -Body $(@{address=$ipconcat;interface=$(($interfacePK.content | convertfrom-json).id)}| ConvertTo-Json) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment