Skip to content

Instantly share code, notes, and snippets.

View AshFlaw's full-sized avatar

AshFlaw

  • The Full Circle
View GitHub Profile
@AshFlaw
AshFlaw / Install-dbatools.ps1
Created January 21, 2019 08:36
Function to install dbatools PowerShell Module
function Install-dbatools {
Import-Module dbatools -ErrorAction SilentlyContinue
if((Get-Module | Where-Object {$_.Name -eq "dbatools"}) -eq $null) {
Write-Output "dbatools module installing"
Install-Module dbatools -force
}
else {
Write-Output "dbatools module already installed"
}
}
@AshFlaw
AshFlaw / Create-DbaTools-WatchDbLogins-SQL-Table.sql
Created January 10, 2019 21:43
Create table for dbatools.io function WatchDbLogins to log to
USE [DatabaseLogins]
CREATE TABLE [dbo].[DbaTools-WatchDbLogins](
[ComputerName] [nvarchar](max) NULL,
[InstanceName] [nvarchar](max) NULL,
[SqlInstance] [nvarchar](max) NULL,
[LoginTime] [datetime2](7) NULL,
[Login] [nvarchar](max) NULL,
[Host] [nvarchar](max) NULL,
[Program] [nvarchar](max) NULL,
@AshFlaw
AshFlaw / Add-SQLDBUserToRole.ps1
Last active January 9, 2019 09:57
Add an existing SQL user to a role for a specific database
Write-Output "Loaded Add-SQLDBUserToRole function"
Function Add-SQLDBUserToRole {
Param (
[string] $server,
[String] $Database ,
[string]$User,
[string]$Role
)
$Svr = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $server
$db = $svr.Databases[$Database]
@AshFlaw
AshFlaw / Monitor-IISAppPoolState.ps1
Created December 29, 2018 10:11
Use PowerShell to Monitor IIS Websites and Application Pools
$Servers = ''
Invoke-Command -ComputerName $Servers {
Import-Module -Name WebAdministration
$Websites = Get-Website
foreach ($Website in $Websites)
{
$AppPool = $Website.applicationPool
If ((Get-WebAppPoolState -Name $AppPool).Value -eq 'Stopped') {Start-WebAppPool -Name $AppPool}
$SiteState = $Website.State
@AshFlaw
AshFlaw / RenewLetsEncryptSynology.sh
Last active December 29, 2018 04:29
Manually renew a Let's Encrypt certificate on a Synology NAS
ssh user@synology.local
sudo -i
# Method 1
/usr/syno/sbin/syno-letsencrypt renew-all
# Method 2 - Verbose
/usr/syno/sbin/syno-letsencrypt renew-all -vv
@AshFlaw
AshFlaw / Get-TempDB-FileGroupInfo.sql
Created December 29, 2018 04:27
Get TempDB file group information
SELECT DB_NAME(mf.database_id) database_name,
mf.name logical_name,
mf.file_id,
CONVERT (DECIMAL (20,2),
(CONVERT(DECIMAL, size)/128)) as [file_size_MB],
CASE mf.is_percent_growth
WHEN 1
THEN 'Yes'
ELSE 'No'
@AshFlaw
AshFlaw / Set-WindowsFirewallPortsShazamDiscovery.ps1
Created November 16, 2018 16:59
Configure windows firewall to allow Shazam probe discovery
$Servers = Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select-Object -Expand DNSHostName
Foreach ($Server in $Servers)
{
invoke-command -ComputerName $Server -ScriptBlock {Set-NetFirewallRule -DisplayGroup 'Remote Event Log Management' -Enabled True -PassThru -EdgeTraversalPolicy Allow | Select-Object DisplayName, Enabled}
invoke-command -ComputerName $Server -ScriptBlock {Set-NetFirewallRule -DisplayGroup "Windows Management Instrumentation (WMI)" -Enabled True -PassThru -EdgeTraversalPolicy Allow -ErrorAction SilentlyContinue | Select-Object DisplayName, Enabled}
}
@AshFlaw
AshFlaw / Remove-SSLNagScreenPRTG.ps1
Last active October 7, 2018 06:49
Remove the SSL reminder screen in PRTG. Useful for reverse proxy situations when the proxy is SSL but the internal termination is HTTP.
Write-Output "Stopping PRTG Core Service"
Get-Service -Name prtgcoreservice | Stop-Service
Write-Output "Disabling SSL nag message in UI"
Set-ItemProperty -path "HKLM:\SOFTWARE\Wow6432Node\Paessler\PRTG Network Monitor\Server\Webserver" -Name "showsslnagscreen" -value 0
Write-Output "Starting PRTG Core Service"
Get-Service -Name prtgcoreservice | Start-Service
@AshFlaw
AshFlaw / Invoke-DisconnectedRDPSessionLogOff.ps1
Created September 30, 2018 19:19
Remotely log off all disconnected sessions from a server, RDP session hosts or otherwise.
$server = ''
$sessions = quser /server:$server | where-object { $_ -match "Disc" }
Foreach ($Session in $Sessions)
{
$UserName = $Session.split(' +')[1]
$ID = qwinsta /server:$server $UserName | where-object { $_ -match $UserName } | Where { $_ -ne "" } | ForEach { $_.Replace(" ","") } | ForEach { $_.Replace("Disc","") } | ForEach { $_.ToLower() } | ForEach { $_.Replace("$UserName","") }
Write-Output "SessionID $ID Username: $UserName is disconnected"
logoff $ID /server:$server
}
@AshFlaw
AshFlaw / Add-OMSNetworkMonitoringFirewallRules.ps1
Created September 12, 2018 07:12
Bulk add the OMS Log Analytics Network Monitoring rules to all machines in a domain
Function OMSICMPFWRules
{
netsh advfirewall firewall add rule name="NPMDICMPV4Echo" protocol="icmpv4:8,any" dir=in action=allow
netsh advfirewall firewall add rule name="NPMDICMPV6Echo" protocol="icmpv6:128,any" dir=in action=allow
netsh advfirewall firewall add rule name="NPMDICMPV4DestinationUnreachable" protocol="icmpv4:3,any" dir=in action=allow
netsh advfirewall firewall add rule name="NPMDICMPV6DestinationUnreachable" protocol="icmpv6:1,any" dir=in action=allow
netsh advfirewall firewall add rule name="NPMDICMPV4TimeExceeded" protocol="icmpv4:11,any" dir=in action=allow
netsh advfirewall firewall add rule name="NPMDICMPV6TimeExceeded" protocol="icmpv6:3,any" dir=in action=allow
}