Skip to content

Instantly share code, notes, and snippets.

# Azure runbook running under an automation account.
#Requires -Modules MSOnline
Import-Module -Name MSOnline
$creds = Get-AutomationPSCredential -Name 'AzureADConnectSyncAccount'
Connect-MsolService -Credential $creds
<#
.SYNOPSIS
@MatthewJDavis
MatthewJDavis / Find-InstalledSoftware.ps1
Last active May 22, 2019 01:55
Ways to find installed software with PowerShell
# Find installed software via registry
Get-ChildItem -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\ | Get-ItemProperty |
Select-Object DisplayName, UninstallString | Format-List
Get-ChildItem -Path HKLM:\SOFTWARE\Wow6432node\Microsoft\Windows\CurrentVersion\Uninstall | Get-ItemProperty |
Select-Object DisplayName, UninstallString | Format-List
# Chocolatey - check chocolatey is installed and then list install packages
if($env:Path -like '*chocolatey*') {
@MatthewJDavis
MatthewJDavis / Get-MDEC2InstanceDetails.ps1
Created June 27, 2019 02:01
Gets basic EC2 instance details along with the ami that it was created from.
# Get EC2 basic details along with the name of the ami the instance was created from (where this is available).
# Need appropriate permissions to read EC2 details
$ec2List = Get-EC2Instance -Filter @{'name'='instance-state-name';'values'='running'}
# Remove TeamCity Agents - not needed.
$noAgentList = $ec2List.Instances | Where-Object {($_ | Select-Object -ExpandProperty tags | Where-Object -Property Key -eq Name ).value -notlike "TeamCityAgent*"}
$ec2DetailsList = $noAgentList| ForEach-Object {
@MatthewJDavis
MatthewJDavis / New-AWSAgentIAMProfile.ps1
Created July 20, 2019 13:06
Create an IAM profile for the AWS EC2 agent install
# Create an IAM profile with the policy that allows the EC2 agent to access the correct resources for cloudwatch monitoring.
# https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/create-iam-roles-for-cloudwatch-agent-commandline.html
$InstanceProfileName = 'CloudWatchAgentServerRole'
$RoleName = 'CloudWatchAgentServerRole'
$RoleDescription = 'AWS EC2 Instance Agent role. Allow access to cloudwatch to put logs for monitoring'
# trust policy for the EC2 service
$TrustPolicy = @'
{
<#
.SYNOPSIS
Create an AWS multiregion CloudTrail and S3 bucket logging all data events for S3 and lambda services.
.DESCRIPTION
This script creates an S3 bucket with public access blocked for CloudTrail logs.
The CloudTrail created is a multiregion trail that logs all data events for S3 and Lambda.
An IAM user or role is required to have permissions to create a CloudTrail and S3 bucket.
.NOTES
Requires the AWS PowerShell module: https://aws.amazon.com/powershell/
Install-Module -Name AWSPowerShell -Scope CurrentUser
@MatthewJDavis
MatthewJDavis / Get-ADUserUPNLength.ps1
Last active August 30, 2019 01:24
Gets the length of UserPrincipalNames of AD users.
# Gets all the AD users under the users OU and sorts them by length of UserPrincipalName.
$upnList = (Get-ADUser -Filter * -Properties userprincipalname -SearchBase 'OU=Users,DC=matthewdavis111,DC=com').userprincipalname
$upnDetails = foreach($upn in $upnList){
[pscustomobject]@{
'Name' = $upn
'Count' = $upn.ToCharArray().count
}
}
@MatthewJDavis
MatthewJDavis / azure-ubuntu-packer.json
Created October 14, 2019 00:29
Packer file to create ubuntu image in Azure
{
"variables": {
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
"managed_image_name": "{{env `MANAGED_IMAGE_NAME`}}",
"resource_group_name": "{{env `RESOURCE_GROUP_NAME`}}"
},
"builders": [
{
"type": "azure-arm",
"subscription_id": "{{user `subscription_id`}}",
@MatthewJDavis
MatthewJDavis / azure-ubuntu-nginx-packer.json
Created October 15, 2019 22:45
Packer build for simple nginx installation
{
"variables": {
"client_id": "{{env `ARM_CLIENT_ID`}}",
"client_secret": "{{env `ARM_CLIENT_SECRET`}}",
"subscription_id": "{{env `ARM_SUBSCRIPTION_ID`}}",
"tenant_id": "{{env `ARM_TENANT_ID`}}",
"managed_image_name": "{{env `MANAGED_IMAGE_NAME`}}",
"resource_group_name": "{{env `RESOURCE_GROUP_NAME`}}"
},
"builders": [
@MatthewJDavis
MatthewJDavis / azure-ubuntu-nginx-packer-image-gallery.json
Created October 19, 2019 16:35
Packer build for simple nginx installation with shared image gallery
@MatthewJDavis
MatthewJDavis / Send-MDAzureADGuestInvitation.ps1
Last active October 25, 2020 06:00
Azure automation runbook to send guest invites to Azure AD
<#
.SYNOPSIS
Invite guest users to Azure Active Directory for demo application.
.DESCRIPTION
Users to be provided in CSV file with the headings 'username,email'.
Users will be be checked to see if they have been invited to Azure AD as a guest user previously. If there is already an invite, then no action will be take, if a user doesn't exist in Azure AD, then an invite will be sent.
Output will be logged to the job output in Azure.
.NOTES
https://docs.microsoft.com/en-us/azure/active-directory/b2b/b2b-quickstart-invite-powershell
#>