Skip to content

Instantly share code, notes, and snippets.

@bgshacklett
Last active September 3, 2016 04:02
Show Gist options
  • Save bgshacklett/6ecf01b5c617550b9177 to your computer and use it in GitHub Desktop.
Save bgshacklett/6ecf01b5c617550b9177 to your computer and use it in GitHub Desktop.
Powershell Snippets
# Get all of the OUs in an existing domain and export them to cli-xml
# The path is added to each OU object so that they will be placed properly
# by the New-ADOrganizationalUnit command
Get-ADOrganizationalUnit -Filter * -Properties * |
select *, @{Name="Path";Expression={$_.DistinguishedName -replace "^.*?\,",""}} |
Export-Clixml .\prodOUs.xml
Get-EC2Instance | Select-Object -ExpandProperty instances | Select-Object InstanceId, @{Name='Name'; Expression={$_.Tags | Where-Object { $_.Key -like 'Name'} | Select-Object -ExpandProperty Value }}, InstanceType, @{Name='AMI'; Expression={Get-EC2Image -ImageId $_.ImageID | Select-Object -ExpandProperty name}}, @{Name='StorageConfig'; Expression={"Storage"}}, @{Name='SubnetName'; Expression={Get-Ec2Subnet -SubnetId $_.SubnetId | Select-Object -ExpandProperty Tags | Where-Object { $_.Key -like 'Name' } | Select-Object -ExpandProperty Value }} | Sort-Object -Property Name | Out-GridView
filter Get-FilteredParameter
{
param
(
$ExcludedParameters,
[Switch]$ExcludeCommonParameters,
[Switch]$ExcludeOptionalCommonParameters
)
begin
{
$commonParameters =
[System.Management.Automation.Cmdlet]::CommonParameters
$optionalCommonParameters =
[System.Management.Automation.Cmdlet]::OptionalCommonParameters
$filteredParameters = $()
$filteredParameters += $ExcludedParameters
If ($ExcludeCommonParameters)
{
$filteredParameters += $commonParameters
}
If ($ExcludeOptionalCommonParameters)
{
$filteredParameters += $optionalCommonParameters
}
}
process
{
If ($filteredParameters -notcontains $_.Key) { $_ }
}
}
function Get-FreeSpace
{
param ( $ComputerName='localhost' )
$wmiParams =
@{
'Class' = 'Win32_LogicalDisk';
'ComputerName' = $ComputerName;
}
$sizeProperty =
@{
'Name' = 'GB Free';
'Expression' = {($_.FreeSpace / 1Gb).ToString("#.##")}
}
Get-WmiObject @wmiParams | select Name,Size,$sizeProperty
}
function Get-LocalUser ($Computername = $env:COMPUTERNAME) {
$params =
@{
'Class' = 'Win32_UserAccount';
'Filter' = 'LocalAccount=True';
'ComputerName' = $ComputerName;
}
Get-WmiObject @params
}
Get-AWSRegion `
| ForEach-Object `
{
$_.Region;
$noDefaultFilter = [Amazon.EC2.Model.Filter]@{Name = 'isDefault'; Values = 'false' }
Get-EC2Vpc -Region $_.Region -Filter $noDefaultFilter
}
# After backing up GPOs with Backup-GPO -All -Path $destPath
# the GPOs can be imported to another domain with the following
# snippet
cd $pathToBackedUpGPOs
ls |
% {
$gpoName = $(gc "$_\bkupInfo.xml" |
Select-Xml "//*[name()='GPODisplayName'] " |
% {$_.Node.InnerText})
$params =
@{
'BackupId'=$_.Name;
'Path'=$PWD;
'TargetName'=$gpoName;
'CreateIfNeeded'=$True;
}
Import-GPO @params
}

This is a collection of Powershell snippets that I'll add to when I find something useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment