Skip to content

Instantly share code, notes, and snippets.

@codykonior
codykonior / Find-ModuleUpdate.ps1
Last active February 21, 2018 04:13
Find-ModuleUpdate
# Faster to get them all at once than look up a few dozen individually
$galleryModules = Find-Module -Verbose:$false
# If you only want to update modules sourced from the PowerShell Gallery, you can add a filter by:
# Where-Object { $_.RepositorySourceLocation -eq "https://www.powershellgallery.com/api/v2/" }
Get-Module -ListAvailable -Verbose:$false | Group-Object Name | ForEach-Object {
if (($_.Group | ForEach-Object Version | Select-Object -Unique | Measure-Object).Count -gt 1) {
Write-Verbose "$($_.Name) has multiple versions installed, I will only check the highest version"
}
$module = $_.Group | Sort-Object Version -Descending | Select-Object -First 1
$ErrorActionPreference = "Stop"
Set-StrictMode -Version "Latest"
function Create-Login {
$loginSetup = "
IF EXISTS (SELECT * FROM sys.server_principals WHERE name = 'LAB\CHWK01$') DROP LOGIN [LAB\CHWK01$];
CREATE LOGIN [LAB\CHWK01$] FROM WINDOWS;
GRANT CONTROL SERVER TO [LAB\CHWK01$];
"
"SEC1N1", "SEC1N2", "SEC1N3", "DAC1N1", "DAC1N2" | New-DbConnection | New-DbCommand $loginSetup | Get-DbData
@codykonior
codykonior / gist:29bdaa04eb8aaa7e0d740f78ecd2a777
Last active April 16, 2019 06:20
Download all of the historic Infocom source code from git
(ConvertFrom-Json (Invoke-WebRequest "https://api.github.com/users/historicalsource/repos?page=1&per_page=100").Content).clone_url | ForEach-Object { &git clone $_ }
<#
IsReady is the option of an internal determination of whether an operation needs to execute or is safe to execute.
A switch of -WhatIf will override anything else.
A switch of -Force can override an unsafe operation. It is not prompted.
A prompt for confirmation only occurs if needed, and this will show on interactive sessions, while being denied on
noninteractive sessions.
#>
function Test-ShouldProcess {
[CmdletBinding(SupportsShouldProcess)]
$previousCallStack = $null
$callStackPath = Get-PSCallStack | Where-Object { $_.Command -ne "<ScriptBlock>" } | ForEach-Object {
if (-not $previousCallStack -or $_.InvocationInfo.Line -ne $previousCallStack.InvocationInfo.Line -or $_.InvocationInfo.PipelineLength -ne $previousCallStack.InvocationInfo.PipelineLength -or $_.InvocationInfo.PipelinePosition -ne ($previousCallStack.InvocationInfo.PipelinePosition - 1)) {
$previousCallStack = $_
$_
}
} | Group-Object Command | Select-Object -ExpandProperty Name
$callStackPath = ("> " + ($callStackPath -join "> ")).Trim()
$test = @(
[PSCustomObject] @{ IsReady = $false }
[PSCustomObject] @{ IsReady = $true }
)
if ($test.IsReady) {
"Output 1"
}
if ($true -eq $test.IsReady) {
"Output 2"
}
@codykonior
codykonior / Get-SentryOneLicenseData.ps1
Last active February 10, 2020 08:32
SentryOne License Scrape
$webSession = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$postValues = @{ Email = "..."; Password = "..."; }
$login = Invoke-WebRequest -WebSession $webSession -Uri "https://my.sentryone.com/login" -Method POST -Body $postValues
$page = Invoke-WebRequest -WebSession $webSession -Uri "https://my.sentryone.com/"
$regex = [regex] 'id="(\d+?)" class="list-group-item serverFriendlyNameContainer sqlSentryItem"'
$ids = $regex.Matches($page.RawContent) | %{ $_.Groups[1].Value }
$keys = @()
@codykonior
codykonior / Get-Interactive.ps1
Last active April 29, 2020 09:58
Determine whether something can accept user input or not
function Get-Interactive {
[CmdletBinding()]
param (
)
-not (
<# Jenkins and other services #>
$false -eq [Environment]::UserInteractive -or
<# Invoke-Command -ComputerName is ASSUMED to be non-interactive #>
$Host.Name -eq 'ServerRemoteHost' -or
<# RSJob, Workflow #>
SELECT DISTINCT
CASE
WHEN ss.EngineEdition = 8
THEN NULL
ELSE d.HostName
END
AS ComputerName,
CASE
WHEN ss.EngineEdition = 8
THEN UPPER(esc.ObjectName)
let GetPatchTuesday = (timestamp: datetime) {
case(
dayofweek(startofmonth(timestamp) + 07d) == 2d, startofmonth(timestamp) + 07d,
dayofweek(startofmonth(timestamp) + 08d) == 2d, startofmonth(timestamp) + 08d,
dayofweek(startofmonth(timestamp) + 09d) == 2d, startofmonth(timestamp) + 09d,
dayofweek(startofmonth(timestamp) + 10d) == 2d, startofmonth(timestamp) + 10d,
dayofweek(startofmonth(timestamp) + 11d) == 2d, startofmonth(timestamp) + 11d,
dayofweek(startofmonth(timestamp) + 12d) == 2d, startofmonth(timestamp) + 12d,
startofmonth(timestamp) + 13d
)