Skip to content

Instantly share code, notes, and snippets.

@johnafogarty4
Last active September 30, 2022 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnafogarty4/b6aa0c935c76e57fed65dbfee8c6be85 to your computer and use it in GitHub Desktop.
Save johnafogarty4/b6aa0c935c76e57fed65dbfee8c6be85 to your computer and use it in GitHub Desktop.
PowerShell Snippets

check Comics

```powershell
"a".."z" | foreach-object {
$files = Get-ChildItem -Filter "$_*"
foreach ($file in $files.name){Invoke-Item $file}
sleep 15
}
```

hyper-v stuff

```powershell
#Path of the VM HDD file stored
$VMLOC = "c:\hyper-v"

#Name of virtual switch which will be used in the VMs
$VMNet = "Default Switch"

$VMName = 'lab-mdt-image','lab-dc','lab-dfs-primary','lab-dfs-read-01','lab-dfs-read-02'
Foreach($vm in $VMName) { 
    New-VM -Name $VM -Generation 2 -SwitchName $VMNet
    New-VHD -Path "$VMLOC\$VM\$vm.vhdx" -Dynamic -SizeBytes 256GB
    ADD-VMHardDiskDrive -VMName $vm -Path "$VMLOC\$VM\$vm.vhdx"
    New-VHD -Path "$VMLOC\$VM\$vm-1.vhdx" -Dynamic -SizeBytes 512GB
    ADD-VMHardDiskDrive -VMName $vm -Path "$VMLOC\$VM\$vm-1.vhdx"
    Add-VMDvdDrive -VMName $vm -path "$VMLOC\iso\2019.iso"
    Set-VM $VM -MemoryStartupBytes 8GB -AutomaticCheckpointsEnabled $false
    Set-VMProcessor $VM -Count 2
    Set-VMFirmware -VMName $vm -FirstBootDevice ((Get-VMFirmware -VMName $vm).BootOrder | Where-Object Device -like *DvD*).Device
    Checkpoint-VM -Name $vm -SnapshotName BeforeInstall
}

$VMLOC = "c:\hyper-v" $VMNet = "Default Switch" $VM = "odbc" $ISO = "C:\hyper-v\iso\Win11_22H2_English_x64.iso"

#Create the VM's

New-VM -Name $VM -Generation 2 -SwitchName $VMNet New-VHD -Path "$VMLOC$VM$vm.vhdx" -Dynamic -SizeBytes 256GB ADD-VMHardDiskDrive -VMName $vm -Path "$VMLOC$VM$vm.vhdx" Set-VM $VM -MemoryStartupBytes 8GB -AutomaticCheckpointsEnabled $false Add-VMDvdDrive -VMName $vm -path $ISO Set-VMFirmware -VMName $vm -FirstBootDevice ((Get-VMFirmware -VMName $vm).BootOrder | Where-Object Device -like DVD).Device enable-vmtpm -VMName $vm Set-VMMemory $vm -DynamicMemoryEnabled $false Set-VMProcessor $vm -Count 2

```

Grab UAT ASGs

```powershell
$asgs = Get-ASAutoScalingGroup -Region us-east-1 -ProfileName lii-mlp-prod.AWS-Org-Full-Access-SSO | where {$_.AutoScalingGroupName -like "pay-uat*"}
```

Refresh UAT ASGs

```powershell
foreach ($asg in $asgs){Start-ASInstanceRefresh -AutoScalingGroupName $asg.AutoScalingGroupName -Region us-east-1 -ProfileName lii-mlp-prod.AWS-Org-Full-Access-SSO}
```

Check Refresh status UAT

```powershell
Do {
$refresh=foreach ($asg in $asgs){get-ASInstanceRefresh -AutoScalingGroupName $asg.AutoScalingGroupName -Region us-east-1 -ProfileName lii-mlp-prod.AWS-Org-Full-Access-SSO | where {$_.Status -eq "InProgress"} | select AutoScalingGroupName}
write-host "Still running"
start-sleep 60
} while ($refresh -ine $null)
```

Get Name, IP, Key

ForEach ($Account in $profiles)
    {
       ForEach ($region in $regions)
       {
           $Instances = Get-EC2Instance -ProfileName $Account -Region $region
           ForEach ($InstanceID in $Instances.Instances.InstanceId)
           {
               $Cur_Inst = Get-EC2Instance -InstanceId $InstanceID -ProfileName $Account -Region $region
               $Inst_Name = ''
                $InstID = ''
                $InstIP = ''
                $InstKey = ''
                $Inst_Name = $Cur_Inst.Instances.Tags | ? { $_.key -eq 'Name' } | Select -Expand Value
                $InstID = $Cur_Inst.Instances.InstanceId
                $InstIP = $Cur_Inst.Instances.PrivateIpAddress
                $InstKey = $Cur_Inst.Instances.KeyName
                Write-Host "$InstID,$Inst_Name,$InstIP,$InstKey"
             }
         }
    }


EC2 instance count

```powershell
$filterRunning = [Amazon.EC2.Model.Filter]@{Name="instance-state-code"; Values = "16"}

$regions = "us-east-1","us-west-2","ca-central-1"
$profiles = Get-AWSCredentials -ListProfileDetail | where {$_.ProfileName -like "*.AWS-Org-Full-Access-SSO"}
foreach ($profilename in $profiles.ProfileName){
    foreach ($region in $regions){
        $runningInstances = @(Get-EC2Instance -profilename $profilename -region $region -Filter $filterRunning)
        write-host $profilename $region "has" $runningInstances.Count "running"
    }
}

RDS Instance Count

```powershell
$regions = "us-east-1","us-west-2","ca-central-1"
$profiles = Get-AWSCredentials -ListProfileDetail | where {$_.ProfileName -like "*.AWS-Org-Full-Access-SSO"}
foreach ($profilename in $profiles.ProfileName){
    foreach ($region in $regions){
        $runningRDS = @(Get-RDSDBInstance -profilename $profilename -region $region)
        write-host $profilename $region "has" $runningRDS.Count "running"
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment