Skip to content

Instantly share code, notes, and snippets.

@poshcodebear
poshcodebear / sg-august.2015.ps1
Last active August 29, 2015 14:26
Scripting Games August 2015 entry
# Works with PowerShell v2, but only works with very simple JSON
function Get-SimpleJSON {
param(
[Parameter(Mandatory=$true,
ValueFromPipeline=$True)]
[Alias('Uri')]
[string]$JSONUri,
[Alias('CamelCase')]
[switch]$TitleCase
@poshcodebear
poshcodebear / measure-command-sample-output.txt
Created July 31, 2015 06:19
Test execution speed with Measure-Command - Sample output
# Get-WMIObject:
Days : 0
Hours : 0
Minutes : 7
Seconds : 11
Milliseconds : 568
Ticks : 4315681996
TotalDays : 0.00499500231018519
TotalHours : 0.119880055444444
TotalMinutes : 7.19280332666667
@poshcodebear
poshcodebear / test-speed-measure-command.ps1
Created July 31, 2015 06:17
Test execution speed with Measure-Command
$computers = Get-ADComputer -SearchBase (Get-ADOrganizationalUnit -Filter {Name -eq 'Servers'} ) -Filter * | Select-Object -ExpandProperty Name
Measure-Command {
Get-WmiObject Win32_ComputerSystem -ComputerName $computers -ErrorAction ignore
}
Measure-Command {
Get-CimInstance Win32_ComputerSystem -ComputerName $computers -ErrorAction ignore
}
function Get-PCBUptime {
<#
.SYNOPSIS
A simple tool for checking how long a system on the network has been running since last boot.
.DESCRIPTION
Get-PCBUptime is a quick WMI-based tool for determinging a remote system's uptime. It is useful for verifying
what a user says when asked if they've rebooted their machine recently.
Get-PCBUptime is written by and copyright of Christopher R. Lowery, aka The PowerShell Bear (poshcodebear.com; Twitter: @poshcodebear)
It is free for all to use, and free to distribute with attribution to the original author.
@poshcodebear
poshcodebear / discover-severed-inheritance.ps1
Created July 31, 2015 06:07
Discovering folders with severed inheritance
$Server = 'server'
$Shares = Get-Content -Path C:\temp\server-shares.txt
$output = foreach ($Share in $Shares) {
$Path = "\\$Server\$Share"
Get-ChildItem -Path $Path -Recurse | `
where attributes -like *directory* | `
Get-Acl | `
Where-Object {$_.AreAccessRulesProtected -eq $True} | `
Select-Object @{Label='Directory';Expression={$_.path.split(':')[-1]}}
@poshcodebear
poshcodebear / 1-billion-seconds-old.ps1
Created July 31, 2015 06:04
When will you be 1 billion seconds old?
$BirthDay = Get-Date "8-16-1989 17:55"
$BirthDay.AddSeconds(1000000000)
@poshcodebear
poshcodebear / block-ie-updates.ps1
Last active August 29, 2015 14:26
Block updates to Internet Explorer
$Setup = 'HKLM:\SOFTWARE\Microsoft\Internet Explorer\Setup'
$Main = 'HKLM:\SOFTWARE\Microsoft\Internet Explorer\Main'
$Versions = '9', '10', '11'
#Disable upgrading to Versions listed above
try { Get-Item -Path $Setup -ErrorAction Stop }
catch { New-Item -Path $Setup }
foreach ($ver in $Versions) {
if ([int]$ver -lt 10) { $dna = $ver + '0' }
@poshcodebear
poshcodebear / list-dns-forwarders.ps1
Created July 31, 2015 05:56
Listing Forwarders for all domain DNS Servers
$NS = Resolve-DnsName -Name domain.tld -Type NS | `
Where {$_.Type -eq 'NS'} | `
Select-Object -ExpandProperty NameHost
$output = foreach ($server in $NS) {
$forwarders = Get-DNSServer -ComputerName $server | `
Select-Object -ExpandProperty ServerForwarder | `
Select-Object -ExpandProperty IPAddress | `
Select-Object -ExpandProperty IPAddressToString
@poshcodebear
poshcodebear / protect-all-ous.ps1
Last active October 4, 2015 05:32
Protecting all OUs from accidental deletion
Get-ADOrganizationalUnit -Filter * -Properties ProtectedFromAccidentalDeletion | `
where {$_.ProtectedFromAccidentalDeletion -eq $False} | `
Set-ADOrganizationalUnit -ProtectedFromAccidentalDeletion $True
@poshcodebear
poshcodebear / get-vmware-net-adaptors.ps1
Created July 31, 2015 05:53
Get VMWare network adapter types
$vms = Get-VM
$output = foreach ($vm in $vms) {
foreach ($adapter in $vm.NetworkAdapters) {
$props = @{'Name' = $vm.Name;
'Type' = $adapter.Type}
$obj = New-Object -TypeName PSObject -Property $props
Write-Output $obj
}