Skip to content

Instantly share code, notes, and snippets.

@Packet-Lost
Packet-Lost / CreateSecurityGroups.ps1
Created April 9, 2017 23:26
Create AWS security groups and configure ingress rules via lookups with PowerShell
#Requires –Modules AWSPowerShell
$myonlyvpc = (Get-EC2Vpc).VpcId
$elbsg = New-EC2SecurityGroup -VpcId $myonlyvpc -GroupName "My ELB Security Group" -Description "Created by script on $((Get-Date).tostring('u'))"
New-EC2Tag -ResourceId $elbsg -Tag @{Key="Name"; Value="My ELB Security Group"}
$httpallowall = New-Object Amazon.EC2.Model.IpPermission -Property @{IpProtocol=”tcp”;FromPort=80;ToPort=80;IpRanges="0.0.0.0/0"}
$httpsallowall = New-Object Amazon.EC2.Model.IpPermission -Property @{IpProtocol=”tcp”;FromPort=443;ToPort=443;IpRanges="0.0.0.0/0"}
@Packet-Lost
Packet-Lost / Test-ReadRouting.ps1
Created August 26, 2017 23:46
Test AlwaysOn Application Read Intent Routing with PowerShell
##replace variables as needed##
$Server = 'AvailabilityGroupListener.fqdn.com'
$Database = 'DatabaseInsideAvailabilityGroup'
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = "Server=$($Server);Database=$($Database);Integrated Security=True;MultiSubnetFailover=True"
$Connection.Open()
$Command = New-Object System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
@Packet-Lost
Packet-Lost / us-east-1-dynamic-vpc-20170904-102113.template
Created September 4, 2017 18:26
Example CloudFormation Template
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"IGWAttachment": {
"Properties": {
"InternetGatewayId": {
"Ref": "InternetGateway"
},
"VpcId": {
"Ref": "VPC"
@Packet-Lost
Packet-Lost / DynamicPublicVPC.py
Last active September 28, 2018 12:37
Troposphere / boto3 dynamic vpc cloudformation template creation
import boto3
import time
import os
from troposphere import Base64, FindInMap, GetAtt, Join, Output
from troposphere import Parameter, Ref, Tags, Template
from troposphere.ec2 import PortRange, NetworkAcl, Route, \
SubnetRouteTableAssociation, Subnet, RouteTable, \
VPCGatewayAttachment, VPC, NetworkInterfaceProperty, NetworkAclEntry, \
SubnetNetworkAclAssociation, EIP, Instance, InternetGateway
@Packet-Lost
Packet-Lost / Set-S3BucketEncryption.ps1
Created October 16, 2018 23:34
Set default server side encryption on an s3 bucket with powershell
Set-S3BucketEncryption -BucketName BucketNameGoesHere -ServerSideEncryptionConfiguration_ServerSideEncryptionRule @{ServerSideEncryptionByDefault=@{ServerSideEncryptionAlgorithm="AES256"}}
@Packet-Lost
Packet-Lost / EnsurePlex.ps1
Last active October 9, 2019 01:32
EnsurePlex.ps1
$plexstatus = (systemctl | grep plex) | Out-String
if($plexstatus.length -eq 0 ){ $startplex = $true }
try{ $plexfind = $plexstatus.trim().Split('loaded ')[1].Split(' ')[1]}
catch { $plexfind = $null }
if ($plexfind -ne 'running') { $startplex = $true }
if($startplex){
systemctl start plexmediaserver.service
Add-Content /srv/logs/plexstart.log "$(Get-Date) Started plex."
} else { Write-Output 'Plex check completed, no need to start plex. Exiting.' }
@Packet-Lost
Packet-Lost / Show-ThisPSOddity.ps1
Last active January 18, 2020 18:29
A function that displays errors during validated parameter type changes
function Show-ThisPsOddity {
param(
[Parameter(Mandatory=$true)][ValidateNotNullOrEmpty()][ValidateSet("foo", "bar", "baz", "match")][String]$UserInput,
[ValidateRange(1,2)][int]$Variant = 1
)
switch($variant) {
1 { if($UserInput -eq "match"){ $UserInput = @("foo", "bar", "baz") } }
@Packet-Lost
Packet-Lost / solution.py
Created April 12, 2017 02:47
Python solution to google's foobar the cake is not a lie
from __future__ import division
from __future__ import print_function
def answer(s):
if not bool(s):
return 0
result = 0
howlong = len(s)
i = howlong
while i > 0:
@Packet-Lost
Packet-Lost / pipeline-example.yml
Created January 31, 2021 19:51
Speed up Azure Devops Pipelines with npm caching
variables:
npm_config_cache: $(Pipeline.Workspace)/.npm
steps:
#shallow depth git checkout for faster checkout and artifact download speeds
- checkout: self
fetchDepth: 10
- task: Cache@2
@Packet-Lost
Packet-Lost / Fargate_Container_Override.ps1
Last active August 27, 2021 06:50
ECS / Fargate Container Override with PowerShell
$envkeypair = New-Object Amazon.ECS.Model.KeyValuePair
$envkeypair.Name = "ENVIRONMENT_KEY"
$envkeypair.Value = "ENVIRONMENT_VALUE"
$containeroverride = New-Object Amazon.ECS.Model.ContainerOverride
$containeroverride.Name = "CONTAINER_NAME"
$containeroverride.Environment.Add($envkeypair)