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
    }

Set Aging on All DNS Zones

A quick script to set the aging on all zones. In this case it’s only doing reverse lookup zones.

$zones = Get-DnsServerZone -ComputerName dc | ? {($_.IsReverseLookupZone -eq $True) -and ($_.IsDsIntegrated -eq $True)}
    
foreach ($zone in $zones) 
    {
    Set-DnsServerZoneAging -ComputerName dc -Name $zone.zonename -RefreshInterval 4.00:00:00 -NoRefreshInterval 4.00:00:00
    }

Find GPOs that aren’t linked or Don’t Contain Policies

Creates a list of all Group Policy objects that are not linked or don’t have any policies configured.

Import-Module grouppolicy

Write-Host "`nUnlinked GPOs`n"
$allGPOs = Get-GPO -All | sort DisplayName
ForEach ($gpo in $allGPOs) {
    $xml = [xml](Get-GPOReport $gpo.Id xml)
    If (!$xml.GPO.LinksTo) {
        $gpo.DisplayName
        }
    }

Write-Host "`nGPOs with no settings`n"
$allGPOs = Get-GPO -All | sort DisplayName
ForEach ($gpo in $allGPOs) {
    $xml = [xml](Get-GPOReport $gpo.Id xml)
    If ($xml.GPO.LinksTo) {
        If (!$xml.GPO.Computer.ExtensionData -and !$xml.GPO.User.ExtensionData) {
            $gpo.DisplayName
            }
        }
    }