Skip to content

Instantly share code, notes, and snippets.

View davegreen's full-sized avatar

Dave Green davegreen

View GitHub Profile
#Requires -Module DHCPServer
Function Get-UnmatchedDhcpServerv4Reservation {
<#
.Synopsis
A function gets DHCP IPv4 reservations from a single scope stretched over two DHCP servers.
.Description
A function gets DHCP IPv4 reservations from a single scope stretched over two DHCP servers
@davegreen
davegreen / Get-ComputerADSite.ps1
Last active February 2, 2016 08:30
A function that will return the AD site the local computer is currently connected to. Useful for PowerShell login scripts that need to target specific sites.
Function Get-ComputerADSite()
{
<#
.Synopsis
Get the computers current AD site from the computer Netlogon information.
.Example
Get-ComputerADSite
.Notes
@davegreen
davegreen / Build-CustomWIMImage.ps1
Created February 1, 2016 21:48
This function mounts and customises a given WIM image by removing pre-provisioned Appx packages and slipstreaming updates. Defaults provided for Appx package removal are based around Windows 10.
Function Build-CustomWIMImage
{
[CmdletBinding(SupportsShouldProcess)]
Param(
[parameter(Mandatory=$true, Position=1)][ValidateScript({ Test-Path $_ })][string]$ImagePath,
[parameter()][ValidateScript({ Test-Path $_ })][string]$UpdatesPath,
[parameter()][ValidateNotNullOrEmpty()][string[]]$RemoveAppxPackages = {
'Microsoft.3DBuilder',
'Microsoft.BingFinance',
'Microsoft.BingNews',
@davegreen
davegreen / CheckIEESC.ps1
Created January 27, 2016 17:07
SCOM monitor script for checking Internet Explorer ESC status. Requires something like the PowerShell script monitor MP by Wei Lim: https://gallery.technet.microsoft.com/Sample-Management-Pack-17b76379
$API = New-Object -ComObject "MOM.ScriptAPI" -ErrorAction Stop
$PropertyBag = $API.CreatePropertyBag()
$Profiles = @{'Admin' = 0; 'User' = 0}
$Profiles.Admin = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}' -Name IsInstalled).IsInstalled
$Profiles.User = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A8-37EF-4b3f-8CFC-4F3A74704073}' -Name IsInstalled).IsInstalled
if ($Profiles.Values -contains 0)
{
$PropertyBag.AddValue('State', 1)
@davegreen
davegreen / CheckFirewall.ps1
Created January 27, 2016 17:06
SCOM monitor script for checking firewall status. Requires something like the PowerShell script monitor MP by Wei Lim: https://gallery.technet.microsoft.com/Sample-Management-Pack-17b76379
$API = New-Object -ComObject "MOM.ScriptAPI" -ErrorAction Stop
$PropertyBag = $API.CreatePropertyBag()
$Profiles = @{'Domain' = '0'; 'Standard' = '0'; 'Public' = '0'}
$Profiles.Domain = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\DomainProfile' -Name EnableFirewall).EnableFirewall
$Profiles.Standard = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile' -Name EnableFirewall).EnableFirewall
$Profiles.Public = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\PublicProfile' -Name EnableFirewall).EnableFirewall
if ($Profiles.Values -contains 0)
{
/// <summary>
/// Gets the DirectoryEntry object of the LDAP distinguishedname passed in, using the current user credentials.
/// </summary>
/// <param name="path">A distinguishedname of the object you wish to get the DirectoryEntry for.</param>
/// <returns></returns>
private static DirectoryEntry GetDirectoryEntry(string path)
{
DirectoryEntry de = new DirectoryEntry();
de.Path = path;
de.AuthenticationType = AuthenticationTypes.Secure;
@davegreen
davegreen / Get-ADDate.ps1
Last active December 19, 2015 12:39
A function that can be passed a date from AD, such as the lastlogon attribute and returns a string of the date, or the string 'Never'.
# Returns a date string based on an AD attribute (like lastlogon, or accountexpires).
# Param1: $addate - AD attribute date value.
Function Get-ADDate($addate)
{
Try
{
$formatteddate = [datetime]::FromFileTime($addate).Date.ToString()
}
Catch
@davegreen
davegreen / Get-Membership.ps1
Last active December 18, 2015 20:49
A quick function to get groups that the current user is a member of.
# Gets a list of groups that the user is a member of from the current Windows identity token.
# Param1: $domain - Boolean value. Return group names with domain or not.
# e.g. true = "BUILTIN\Authenticated Users", false = "Authenticated Users".
Function Get-Membership($domain)
{
$groups = @()
foreach ($group in [System.Security.Principal.WindowsIdentity]::GetCurrent().Groups)
{
$grp = $group.Translate([System.Security.Principal.NTAccount]).ToString()
Set objArgs = Wscript.Arguments
Set oAPI = CreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
Dim ConnString
ConnString = "DRIVER={SQL Server};Server=" & objArgs(0) & ";Database=" & objArgs(1) & ";"
objConnection.Open ConnString
objRecordSet.Open "SELECT TOP(1) CC.CollectionID, CN.CollectionName, CC.TimeUpdated FROM Collection_MemberChg_Notif CC JOIN Collections CN ON CC.CollectionID = CN.SiteID WHERE CN.CollectionName = 'Test Collection' AND CC.TimeUpdated < DATEADD(mi, -20, GETDATE())", objConnection, 3, 3
<?xml version="1.0" encoding="utf-8"?><ManagementPack ContentReadable="true" SchemaVersion="2.0" OriginalSchemaVersion="1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<Manifest>
<Identity>
<ID>Custom.SQLQueries</ID>
<Version>1.0.0.1</Version>
</Identity>
<Name>Custom - SQLQueries</Name>
<References>
<Reference Alias="MicrosoftWindowsLibrary7585010">
<ID>Microsoft.Windows.Library</ID>