Update sql server operators (redux)

I have a version of this already on here that uses SQLPSX, but since that’s not being updated anymore and that you may not always have SQLPSX available, here is a version that does not use SQLPSX.  I’ve more or less stopped using SQLPSX, but I do miss it at times.


$outputs = @();
$newEmailAddress = 'NewEmailAddress@me.com'
$oldEmailAddress = 'OldEmailAddress@me.com'
$reportPath = 'c:\UpdatedOperators.csv'
$servers = gc -Path 'C:\Servers.txt'

$servers | %{
	$srvName = $_
	$srvConn = New-Object "Microsoft.SqlServer.Management.Common.ServerConnection"
	$srvConn.ServerInstance = $_
	$srv = New-Object Microsoft.SqlServer.Management.Smo.Server $srvConn
	$srv.JobServer.Operators | where{$_.EmailAddress -eq $oldEmailAddress} | %{
		$output = New-Object -TypeName PSObject -Property @{
		    ServerName = $srvName
		    OperatorName = $_.Name
		    OldEmailAddress = $_.EmailAddress
			NewEmailAddress = $newEmailAddress
		$outputs += $output
		$_.EmailAddress = $newEmailAddress
$outputs | SELECT ServerName, OperatorName, OldEmailAddress, NewEmailAddress | Export-Csv -Path $reportPath -NoTypeInformation -Force

Perform Volume Maintenance task in database creation

If you have to do any file growing\restoring\creation at all, best grant the Perform volume maintenance tasks in the group policy edior (run  –>  gpedit.msc  –>  Windows Settings –> Security Settings  –>  Local Policies  –>  User Rights Assignment) to the account that Sql Server is running under.


Just a short totally subjective note on the performance of creating a 5GB database with a 1GB log file.  Without the Perform volume maintenance tasks right, this took about 2 minutes to complete.


After adding the service account to the Perform volume maintenance tasks (and restarting the Sql Server Service via configuration manager [a must]), the database takes 17 seconds to create.


When you add the service account to the Perform volume maintenance tasks it enables sql server to skip zeroing out the data file after it allocates space for it.  Unfortunately, this can’t be done for the log file though.  It must be zero’d out when it is grown or created.

Get Sql Server major version number from serverproperty function

More for me than for you.  I’ve written and re-written this about 6 times this week and can’t seem to remember to save it off, so here I go.  This will return the major version number for the sql server being queried.  Sql Server 2000 = 8, Sql Server 2005 = 9, etc…


Pass a table type parameter to sql server via powershell

Still don’t like the title, but I’m pushing it anyway.  Here is how to pass a DataTable to a Sql Server stored procedure via powershell.  In my case, I’m gathering a boat-load of data from the sys.dm_os_performance_counters DMV for numerous server to be queried and reported upon.  First I query the target server, get the results into a datareader, load said datareader into a DataTable and then pass the DataTable as a parameter to a stored proc to be saved off.  Use at your own risk, this has been only minimally tested.


$servers = gc -Path 'C:\Servers.txt'
$cnString = 'Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DatabaseName;Data Source=ServerName'

$PerfQry = "
FROM sys.dm_os_performance_counters
WHERE object_name NOT IN(
	'SQLServer:Deprecated Features', 
	'SQLServer:Database Replica', 
	'SQLServer:Memory Broker Clerks',
	'SQLServer:User Settable',
	'SQLServer:Broker Statistics',
	'SQLServer:Availability Replica',
	'SQLServer:Access Methods',
	'SQLServer:Broker/DBM Transport',
	'SQLServer:Broker Activation',
	'SQLServer:Broker TO Statistics')
AND instance_name != 'mssqlsystemresource'


	$servers | %{
		#$srvName = $_
		$srvConn = New-Object "Microsoft.SqlServer.Management.Common.ServerConnection"
		$srvConn.ServerInstance = $_

		[System.Data.DataTable]$perfTable = New-Object('system.Data.DataTable')
		[System.Data.SqlClient.SqlCommand]$cmd = New-Object('system.data.sqlclient.sqlcommand')
		[System.Data.SqlClient.SqlConnection]$c = New-Object('system.data.sqlclient.sqlconnection')
		$c.connectionstring = $cnString;
		$dr = $srvConn.ExecuteReader($PerfQry);
		$cmd.Connection = $c;
		$cmd.CommandType = [System.Data.CommandType]::StoredProcedure
		$cmd.commandText = "dbo.InsertPerfCounters"
		$serverIDParam = New-Object('system.data.sqlclient.sqlParameter')
		$perfResultsParam = New-Object('system.data.sqlclient.sqlparameter')
		#serverid param
		$serverIDParam.ParameterName = "ServerID"
		$serverIDParam.SqlDBtype = [System.Data.SqlDbType]::Int
		$serverIDParam.Direction = [System.Data.ParameterDirection]::Input
		$serverIDParam.value = 1
		#sql table type param
		$perfResultsParam.ParameterName = "PerfCounters"
		$perfResultsParam.SqlDBtype = [System.Data.SqlDbType]::Structured
		$perfResultsParam.Direction = [System.Data.ParameterDirection]::Input
		$perfResultsParam.value = $perfTable
	$_ | fl -Force