PowerShell Powered DDNS with AWS

I wanted to write a script to update DNS records in AWS to make my own Dynamic DNS solution. I got the idea from some scripts I’d seen online, but really didn’t want to deal with all the Python.
Instead, I found that you can get AWS PowerShell cmdlets to do the same thing. Yay!

# Install AWS PowerShell tools 
# https://docs.aws.amazon.com/powershell/latest/userguide/pstools-getting-set-up.html
# Install-Module $module -Scope CurrentUser 
# Scope required if installing without administrative rights

Import-Module AWSPowerShell

# AWS Information
$AccessID="**Access ID Here**"
$SecureID="**Secure ID Here**"
$ZoneID="**Zone ID Here**"
$Recordset="**FQDN to be updated Here**"
$TTL=100
$Type="A"

# Determine public IP address
$IP=(Resolve-DnsName -Name myip.opendns.com -Server resolver1.opendns.com).IPAddress

# Get the current IP address value of the record
$RecordData=(Test-R53DNSAnswer -AccessKey $AccessID -SecretKey $SecureID -HostedZoneId $ZoneID -RecordName $Recordset -RecordType $type).RecordData


If ($IP -eq $RecordData) # Check to see if the IP value of the record is correct or needs to be updated
    {
    "Record data correct, no action required"
    }
Else
    {
    # Set parameters to delete the existng record
    $Delete = New-Object Amazon.Route53.Model.Change
    $Delete.Action = "DELETE"
    $Delete.ResourceRecordSet = New-Object Amazon.Route53.Model.ResourceRecordSet
    $Delete.ResourceRecordSet.Name = $Recordset
    $Delete.ResourceRecordSet.Type = $Type
    $Delete.ResourceRecordSet.TTL = $TTL
    $Delete.ResourceRecordSet.ResourceRecords.Add(@{Value=$IP})

    # Set parameters to create a new record with the correct IP address
    $Create = New-Object Amazon.Route53.Model.Change
    $Create.Action = "CREATE"
    $Create.ResourceRecordSet = New-Object Amazon.Route53.Model.ResourceRecordSet
    $Create.ResourceRecordSet.Name = $Recordset
    $Create.ResourceRecordSet.Type = $Type
    $Create.ResourceRecordSet.TTL = $TTL
    $Create.ResourceRecordSet.ResourceRecords.Add(@{Value=$IP})

    # Execute the deletion and creation of the record
    Edit-R53ResourceRecordSet -AccessKey $AccessID -SecretKey $SecureID -HostedZoneId $ZoneID -ChangeBatch_Change $Delete,$Create
    }

VMware Photon on VMware Workstation

VMware Photon is a lightweight OS used for cloud-native workloads. The Photon OS is also designed to host Docker for containerized workloads. Able to run within a vSphere environment and can also be easily run within within VMware Workstation for testing.

1. Download the OVA image from the Photon Github site https://github.com/vmware/photon/wiki/Downloading-Photon-OS
2. In VMware Workstation click File – Open and navigate to the OVA image
3. Name the VM and specify the storage and click Import
4. Accept the EULA
5. Upon completion of the import process edit VM settings
6. Select the options tab and click General, set Guest Operating System to Linux and Version to VMware Photon 64-Bit
7. Power on VM
8. Log in at the VM console and you will be required to update the root password – (Username – root / Password – changeme)
9. Open the docker.service file (/etc/systemd/system/multi-user.target.wants/docker.service)
10. Update “ExecStart=/bin/docker” to “ExecStart=/bin/docker -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -s overlay”
11. Enable and start Docker by running systemctl enable docker && systemctl start docker
12. Start the Portainer container – docker run -d -p 9000:9000 portainer/portainer -H tcp://REMOTE_HOST:REMOTE_PORT
13. In a browser, navigate to the Portainer interface – http://REMOTE_HOST:REMOTE_PORT
14. Enter the default username (admin) and enter and confirm a new password

https://vmware.github.io/photon/
http://www.doublecloud.org/2015/05/how-to-enable-remote-management-for-docker-in-vmware-photon/
https://portainer.readthedocs.io/en/latest/deployment.html

Move Hyper-V VM and Storage Files to New Location

The following script will move all of the VMs on a Hyper-V host to a new storage location. In this case we are moving the VMs from “C:\VM\VirtualMachines\” to “D:\VM\VirtualMachines\.” This script assumes that the VM disks are kept in the VM folder and not in a separate location.

$VMs=Get-VM
$Path="D:\VM\VirtualMachines\$($VM.Name)"

foreach ($VM in $VMs)
    {
    Move-VMStorage -VM $(Get-VM $VM.Name) -DestinationStoragePath $Path
    }

Shrink LVM Volume – Ubuntu

Boot from an Ubuntu LiveCD

Locate the volume group that you wish to shrink
ubuntu@ubuntu:~$ sudo lvmdiskscan

ubuntu@ubuntu:/dev$ sudo lvmdiskscan
/dev/ram0 [ 64.00 MiB]
/dev/loop0 [ 1.41 GiB]
/dev/ubuntu-vg/root [ 460.32 GiB]

Issue the following command to shrink the file system and the volume
ubuntu@ubuntu:~$ sudo lvreduce –resizefs –size -230G /dev/ubuntu-vg/root

Reboot

PowerShell Dump DHCP Reservations

A script to dump all of the reservations from a Windows DHCP server

$PropArray = @()
$scopes = Get-DhcpServerv4Scope -ComputerName dhcpprd01

foreach ($scope in $scopes)
    {    
    $Reservations = Get-DhcpServerv4Reservation -ComputerName dhcpprd01 -ScopeId $scope.ScopeId
    foreach ($Reservation in $Reservations)
        {
        $Prop = New-Object System.Object
        $Prop | Add-Member -type NoteProperty -name ScopeID -value $Reservation.ScopeID
        $Prop | Add-Member -type NoteProperty -name IpAddress -value $Reservation.IPAddress
        $Prop | Add-Member -type NoteProperty -name ClientID -value $Reservation.ClientID
        $Prop | Add-Member -type NoteProperty -name Name -value $Reservation.Name
        $Prop | Add-Member -type NoteProperty -name Type -value $Reservation.Type
        $PropArray += $Prop
       
        }
    }
$PropArray | ft -AutoSize

PowerShell Dump DHCP Leases

Dump all of the DHCP leases from a Windows DHCP server into an object using PowerShell

$PropArray = @()
$scopes = Get-DhcpServerv4Scope -ComputerName dhcpprd01

foreach ($scope in $scopes)
    {    
    $Leases = Get-DhcpServerv4Lease -ComputerName dhcpprd01 -ScopeId $scope.ScopeId
    foreach ($Lease in $Leases)
        {
        $Prop = New-Object System.Object
        $Prop | Add-Member -type NoteProperty -name ScopeID -value $Lease.ScopeID
        $Prop | Add-Member -type NoteProperty -name IpAddress -value $Lease.IPAddress
        $Prop | Add-Member -type NoteProperty -name ClientID -value $Lease.ClientID
        $Prop | Add-Member -type NoteProperty -name HostName -value $Lease.HostName
        $Prop | Add-Member -type NoteProperty -name AddressState -value $Lease.AddressState
        $PropArray += $Prop
        }
    }

$PropArray

Get Log Entries For a Range of Time on Linux

You need to check the logs for a problem and you know when it occurred. This will allow you to grab all of the entries for a period of time to make the search for clues easier.

sudo cat secure | awk '/^Dec  1 09:27/,/^Dec  1 09:33/'
Dec  1 09:03:09 u16532612 sshd[24297]: Failed password for root from 43.229.53.54 port 43335 ssh2
Dec  1 09:03:12 u16532612 sshd[24297]: Failed password for root from 43.229.53.54 port 43335 ssh2
Dec  1 09:03:14 u16532612 sshd[24297]: Failed password for root from 43.229.53.54 port 43335 ssh2

Script Creating DNS Records

This script creates Host A records for a list of hosts.

$records = "mimsvc,10.39.0.78","mimsps,10.39.0.78","mimsync,10.36.1.53","mimsvcsql,10.36.1.52","mimspssql,10.36.1.52","mimsyncsql,10.36.1.52"
$DNSServer = "DC"
$Zone = "domain.tld"

foreach ($record in $records)
    {
    $Name = $record.split(",")[0]
    $Ip = $record.split(",")[1]
    
    Add-DnsServerResourceRecordA -ComputerName $DNSServer -ZoneName $Zone -Name $Name -IPv4Address $Ip -PassThru
    }