Skip to content

Instantly share code, notes, and snippets.

View AshFlaw's full-sized avatar

AshFlaw

  • The Full Circle
View GitHub Profile
@AshFlaw
AshFlaw / Set-SQLDBMultiUser.sql
Created September 20, 2017 12:06
Set a given DB back to multi user mode when locked by a given SPID
kill <SPID>
USE [master]
ALTER DATABASE <DBName>
SET MULTI_USER
GO
@AshFlaw
AshFlaw / Uninstall-OMSAgent.ps1
Created September 20, 2017 12:09
Uninstall the OMS Microsoft Monitoring Agent.
$server = ""
$app = Get-WmiObject -Class Win32_Product -computer $server | Where-Object {$_.Name -match "Microsoft Monitoring Agent"}
$app.Uninstall()
@AshFlaw
AshFlaw / Set-SQLCleanupDBO_Users_QS.ps1
Created September 20, 2017 12:11
Quick cleanup of a SQL 2016 instance for DB Owners, Orphaned users and enabling Query Store
$Instance = ""
Set-DbaDatabaseOwner -SqlServer $Instance
Remove-SqlOrphanUser -SqlServer $Instance
Set-DbaQueryStoreConfig -SqlServer $Instance `
-State ReadWrite `
-FlushInterval 600 `
-CollectionInterval 10 `
-MaxSize 100 `
-CaptureMode All `
-CleanupMode Auto `
@AshFlaw
AshFlaw / Create-SQLUserAndIPLoggingTrigger.sql
Created October 16, 2017 12:43
Create the trigger to log connection details to the User IP Audit table.
USE UserIPAudit
GO
CREATE TRIGGER LogonTrigger ON ALL SERVER FOR LOGON
AS
BEGIN
DECLARE @data XML
SET @data = EVENTDATA()
INSERT INTO UserIPAudit.dbo.[UserIPLog]
(
@AshFlaw
AshFlaw / Create-SQLUserAndIPLoggingTable.sql
Created October 16, 2017 12:42
Create SQL table for logging user connections with permissions and compression on the logging table.
CREATE DATABASE UserIPAudit
GO
USE UserIPAudit
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
@AshFlaw
AshFlaw / Disable-ScheduledDefrag.ps1
Created October 23, 2017 08:12
Disable Windows scheduled defrag on all computers in a domain
$ErrorActionPreference = "Continue"
$computers = Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select-Object -Expand DNSHostName
Function DisableDefrag
{
If ((Get-ScheduledTask -TaskName 'ScheduledDefrag').State -eq 'Ready')
{
    Disable-ScheduledTask -TaskName 'ScheduledDefrag' -TaskPath '\Microsoft\Windows\Defrag'
Write-Output "$env:computername Scheduled defrag disabled"
}
@AshFlaw
AshFlaw / Install-EXE.ps1
Created January 18, 2018 19:16
Install executable with command line switches
Function Install-EXE
{
Param
(
$Command
)
$processID = (Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $Command -ErrorAction Stop).processid
Wait-Process -Id $processID
}
@AshFlaw
AshFlaw / Set-SSISServiceAccountDCOMPermissions.ps1
Created February 1, 2018 13:27
# Set SQL 2016 SSIS Service Account DCOM Permissions on Windows Server 2012 R2 Core
$ServiceName = "MsDtsServer130" #SQL 2016 SSIS Service Name
$SSIS_Service = Get-WmiObject win32_service | where-object {$_.Name -eq $ServiceName}
If ($SSIS_Service -ne $null)
{
$SSIS_AppDesc = "Microsoft " + (Get-Service $ServiceName).DisplayName # Add the prefix which is not present in the service description
$SSISAccount = $SSIS_Service.StartName.Split("\")
$user = $SSISAccount[1]
$domain = $SSISAccount[0]
$appdesc = $SSIS_AppDesc
$app = get-wmiobject -query ('SELECT * FROM Win32_DCOMApplicationSetting WHERE Description = "' + $appdesc + '"') -enableallprivileges
@AshFlaw
AshFlaw / Get-ADUserGroupMebership.ps1
Created April 4, 2018 16:32
Get the AD groups a user is a member of.
Get-ADPrincipalGroupMembership username | select name
function Test-SQLTableExists
{
param ($Instance,$Database,$TableName)
$Return = $SQL = $dataTable = $null
$sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = N'$TableName'"
$dataTable = Invoke-Sqlcmd2 -ServerInstance $Instance -Database $Database -Query $sql
if ($dataTable) {$return = $true}
else {$return = $false}
$Return
}