Skip to content

Instantly share code, notes, and snippets.

View XPlantefeve's full-sized avatar

Xavier Plantefève XPlantefeve

View GitHub Profile
# The following is an example, it's not a function, it's not meant to be run
# as is, I just wrote it to show how creating a scheduled tak on an event
# can be done in PowerShell. I wrote a small post about how it came to be, that
# can be read at https://xavier.plantefeve.fr/posts/SchdTskOnEvent
$class = Get-cimclass -ClassName MSFT_TaskEventTrigger -Namespace root/Microsoft/Windows/TaskScheduler
$trigger = $class | New-CimInstance -ClientOnly
$trigger.Enabled = $true
$trigger.Subscription = '<QueryList><Query Id="0" Path="Microsoft-Windows-NetworkProfile/Operational"><Select `
Function Get-Profiles
{
<#
.SYNOPSIS
Gives a list of locally saved user profiles.
.DESCRIPTION
Gets a list of locally used profiles according to the registry and
returns an object with information about the profile.
@XPlantefeve
XPlantefeve / Get-LastDOW.ps1
Created July 13, 2017 12:45
Get date for last <day of week>
function Get-LastDOW {
<#
.SYNOPSIS
Returns the date of the last day of week you select.
.DESCRIPTION
Get-LastDOW will return the date of the last day of the week you select,
up to one week in the past (between one week ago and yesterday). Useful,
as an example, to search in logs for events only happening once a week.
@XPlantefeve
XPlantefeve / logonindex.ps1
Created March 19, 2015 16:23
logon index / find a computer's main user
$ComputerName = $env:ComputerName
$test = Import-Csv -Path "log:\computers\${ComputerName}.log" -Delimiter ' ' -header @('user','date','time')
$t2 = $test |
select user,date,@{
name='logonage';
expression={
1 / ((Get-Date) - [DateTime]$_.date).Days}
@XPlantefeve
XPlantefeve / Start-ManagementConsole.ps1
Created February 18, 2015 15:53
Launch Management console easily from the command line.
function Start-ManagementConsole {
Param( [string]$ComputerName = '.' )
Start-Process -FilePath "${env:windir}\System32\mmc.exe" `
-ArgumentList "compmgmt.msc /computer:\\${ComputerName}"
}
New-Alias -Name manage -Value "Start-ManagementConsole" -Scope Global
Topic: NBA All Stars
Owners: Foo Owners
Users: User 1
Users: User 2
Topic: NFL MVPs
Owners: Bar Owners
Users: Tony Stark
Users: Mitch Hedberg
Users: Someone Else
Topic: NBA MVPs
@XPlantefeve
XPlantefeve / get-DomainAdminLastLogon.ps1
Created June 11, 2015 13:04
get-DomainAdminLastLogon
$domainControllers = Get-ADDomainController -Filter *
Get-ADGroupMember -Identity 'Domain Admins' -Recursive | ForEach-Object -Process {
$user = $_
$domainControllers |
ForEach-Object -Process {
Get-ADUser $user.SamAccountName -Server $_.Name -Properties lastLogon, cn, employeeNumber, accountExpires, PasswordNeverExpires, Enabled, LockedOut, pwdLastSet
} |
Sort-Object -Property LastLogon |
Select-Object -Last 1 -Property SamAccountName,
@XPlantefeve
XPlantefeve / New-ExcelDocument.ps1
Created June 15, 2016 11:44
Barebone excel creation + CSV import method. Basis for further work.
#region ExcelFunctions
function Get-NormalizedPath ( $Path ) {
if ( ( Split-Path $Path ) -match '^(\.|)$' ) {
$Path = Join-Path -Path ( Get-Location ) -ChildPath ( Split-Path -Path $Path -Leaf )
}
return $Path
}
$MethodImportCSVtoSheet = {
@XPlantefeve
XPlantefeve / MaintenanceWindowFunctions.ps1
Last active June 13, 2016 09:15
A couple of quickly done functions to handle local CCM maintenance windows.
<#
MIT License
Copyright (c) 2016 Xavier Plantefeve
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@XPlantefeve
XPlantefeve / New-Timer.ps1
Last active May 30, 2016 08:17
Creates a timer that display a progressbar and the estimated remaining time.
<#
.Synopsis
Creates a timer with display
.DESCRIPTION
Creates a timer that writes the progress of the current action and the estimated remaining time.
Suppose you have $Items, an array of items to process.
First, you create a timer initialized to the number of items in the array:
$MyTimer = New-Timer -TotalItems $Items.count -Activity 'Processing items'