Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am davops on github.
  • I am adminator (https://keybase.io/adminator) on keybase.
  • I have a public key ASB-a6og_L_IAehBFXhOvx-i-vZaA4BNe4uM0yfO9tEs9wo

To claim this, I am signing this object:

@davops
davops / Resolve-IP.ps1
Created August 8, 2016 19:00
Converts a server's DNS name to an IP address using the IPv4 address.
$dnsServer = "Server1.domain.com"
([System.Net.Dns]::GetHostAddresses($dnsServer) | where {$_.AddressFamily -eq 'InterNetwork'}).IPaddressToString
@davops
davops / Start-SQLSync.ps1
Created August 3, 2016 14:05
Starts the replication synchronization in SQL for a given subscription using PowerShell. It's the equivalent of "Start Synchronization" in the GUI of Replication Monitor or starting the job that corresponds to that particular subscription. The names of the jobs it finds are written to a file.
$serverName = "SQL1" ###name of the SQL instance, not the server name
$jobQuery = "select jobs.name from msdb.dbo.sysjobs jobs (nolock)
join msdb.dbo.syscategories categories (nolock)
on jobs.category_id = categories.category_id
where jobs.name like '$servername-YourReplicationName%' AND categories.name = 'REPL-Merge'"###Used for merge replication but can be changed
$jobQuery = (Invoke-Sqlcmd -ServerInstance $serverName -Query $jobQuery | Select-Object -Property name).name
$jobQuery | Out-File c:\logs\database\replication__sync_log.txt -Append
foreach ($row in $jobQuery)
{
@davops
davops / Get-CertSettings.ps1
Created August 2, 2016 13:47
Retrieves all websites from IIS. Prints the hostname of the target server, the website name, whether or not SSL is enabled, the certificate expiration date, the certificate subject and the certificate subject alternative names (for multi-domain certificates).
<#
.NOTES
Name: Get-CertSettings.ps1
Author: Davina Fournier
Requires: PowerShell v3 or higher. The account running this script needs to have rights to access IIS settings on the target servers. Tested on 2012 servers in a single domain.
Last Updated: 7/31/2016
.SYNOPSIS
Retrieves all websites from IIS. Prints the hostname of the target server, the website name, whether or not SSL is enabled, the certificate expiration date, the certificate subject and the certificate subject alternative names (for multi-domain certificates).
.DESCRIPTION
A list of remote computer names should be entered into the Server_list.txt file currently located in the "Tools\Scripts" directory. The script creates a session to each remote computer, then queries IIS for the binding settings of each website. If the website has no SSL binding, then the "SSL Enabled" setting will be set to "Not Enabled" and the remaining values will be empty. If there is a SSL binding, the Local Machine stores are searched for the corresponding certif
@davops
davops / StartJob.ps1
Created June 28, 2016 19:50
Using PowerShell, start background jobs on multiple servers at the same time and wait until they finish.
#Purpose: Start background jobs on multiple servers at the same time and wait until they finish
$servers = "server1","server2","server3"
$buildNumber = "1.2"
$servers | %{
$script =
{
param($server)
Invoke-Command -ComputerName $server -ScriptBlock {param($buildNumber) New-Item c:\Deployment\$buildNumber -type directory -force}
@davops
davops / delete_from_IIS.ps1
Created June 14, 2016 15:50
Deleting sites and application pools from Microsoft IIS using PowerShell
#Deleting Sites and app pools in IIS 7 with PowerShell
$appCmd = "C:\windows\system32\inetsrv\appcmd.exe"
#lists all the sites
& $appcmd list site
#deletes a specific site
& $appcmd delete site "Name of site"
#deletes a specific application pool
& $appcmd delete apppool "Name of app pool"
#delete any app pools that use a certain username (in the identity column of the GUI)
@davops
davops / Set_Remote_Execution_Policy.ps1
Last active July 31, 2016 14:54
Set Powershell's Execution Policy for a remote computer when it is currently set to "RemoteSigned"
$thisPsSession = New-PSSession SERVER1
Invoke-Command -Session $thisPsSession -ScriptBlock {
Set-ExecutionPolicy RemoteSigned
}
@davops
davops / Send_Email.sql
Created June 2, 2016 15:16
Query to search for an existing email profile in SQL & send an email using it
--Find a prexisting email profile to be inserted into the "@profile_name" variable below
EXEC msdb.dbo.sysmail_help_profile_sp;
--Define the text you will email
DECLARE @body NVARCHAR(MAX)
SET @body = 'Whatever you want to send yourself'
--Send the email using the email profile found
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'Administrator Profile',
@recipients = '[email protected]',
@body = @body,
@davops
davops / v2queries.ps1
Created April 21, 2016 19:38
Run SQL queries remotely in PowerShell v2
$thisPsSession = New-PSSession "Server1"
Invoke-Command -Session $thisPsSession -ScriptBlock {
sqlcmd.exe -q "SELECT yourcolumn FROM yourdatabase"
}
Remove-PSSession $thisPsSession
@davops
davops / xp_cmdshell.sql
Created April 18, 2016 18:54
This query looks through all databases for objects using the xp_cmdshell command. The original select query originally came from: http://stackoverflow.com/questions/2833389/finding-all-stored-procedures-calling-a-function
EXEC sp_MSForEachDB ' Use [?]; SELECT DB_NAME() AS ''Database Name'';
Select *
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
INNER JOIN sys.schemas s ON o.schema_id=s.schema_id
WHERE m.definition Like ''%xp_cmdshell%''
ORDER BY 1'