Skip to content

Instantly share code, notes, and snippets.

@lockworld
lockworld / 0_reuse_code.js
Created May 31, 2017 14:46
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@lockworld
lockworld / cs_dataset_iteration.cs
Last active February 27, 2018 20:52
Iterating through dataset in C#
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable table1 = new DataTable("patients");
table1.Columns.Add("name");
table1.Columns.Add("id");
@lockworld
lockworld / RecycleBin.cs
Last active June 19, 2017 19:00
Working with files in a C# application
using Shell32;
using System;
namespace RecycleBin
{
class Program
{
static void Main(string[] args)
{
@lockworld
lockworld / logfile.cs
Last active June 23, 2017 18:03
Text formatting in C#
//Ref: https://stackoverflow.com/a/2866650
var timestamp = DateTime.Now.ToString("yyyy'-'MM'-'dd hh':'mm':'ss.fff");
var msgType = "INFORMATION";
var msgText = "This is an example of how to format a text string to align fragments on a line, like a typical log file.";
LogFile.Write(string.Format("{0,-25} {1,-15} {2}", timestamp, msgType, msgText));
@lockworld
lockworld / Find size of specific tables.sql
Last active November 24, 2021 19:31
SQL Server Database and Server commands (These are for when you need to go beyond just table-level access).
SELECT
t.NAME AS TableName,
p.rows AS RowCounts,
CONVERT(DECIMAL,SUM(a.total_pages)) * 8 / 1024 / 1024 AS TotalSpaceGB,
SUM(a.used_pages) * 8 / 1024 / 1024 AS UsedSpaceGB ,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 / 1024 AS UnusedSpaceGB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
@lockworld
lockworld / FindNameInDefinitions.sql
Last active January 9, 2021 03:51
Thse queies can be used to find information in tables, views, or object definitin
/*
Use this query to find the name of a database that has been renamed so you can change the name in all views, stored prcedures, functions, etc.
*/
DECLARE @Search varchar(255)
SET @Search='%Old_DB_Name%'
SELECT DISTINCT
DB_NAME() as DBNAME, schema_name(o.schema_id) as SchName, o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
@lockworld
lockworld / !Run a .bat file with administrative permission without user interaction.md
Last active April 28, 2020 12:29
Run a script with an administrator's account without any user interaction

In this gist, we see how to run any .bat file (or any executable at all) either elevated ("Run as administrator") or with specific user credentials, all without any interaction from the user.

This is particularly useful in creating a GPO to pass out to network users to run a patch or script that requires specific permissions.

Steps

Create a batch file that will be pushed out to users via GPO, following the example in "RunAsOther script.bat" (below)

  • Replace {TaskName} with a unique name for your task. I suggest including a datestamp
  • Replace {\path\to\my.bat} with the path to your .bat file or executable that needs to be run on the user's machines.
  • Replace {domain\username} with the domain and username of the account you want to use to run this task. [OPTIONAL]
# In this gist, we create a PowerShell script that, when run, will prompt the user for the following variables:
# * $SSRSServer [The server name or domain name of the report server]
# * $SSRSPath [The SSRS folder path for the folder containing the .rdl files you wish to download]
# * $Destinationpath [The local folder you wish to store the downloaded .rdl files in]
#
# When run, the user will supply the variables and the script will go out to the report server and individually download every .rdl file from that folder on the report server to the user's local machine in the location specified.
# Set Variables from User Input
$SSRSServer = Read-Host("Enter the report server name or domain name (Without http:// or any subdirectories)")
var myShipVia = (from ShipVia_row in ttShipHead
where ShipVia_row.ShipViaCode=="12345"
select ShipVia_row).FirstOrDefault();
if (myShipVia!=null)
{
ShipViaDisabled = myShipVia.UDField<bool>("Disabled_c");
}
/*
/*
If you want to add a column with random numbers as part of an INSERT statement, you can't use the RAND() function becuase you will always get the same number.
Instead, use the following code, which will return a random number between 0 and one less than the number following the modulus operator
*/
SELECT ABS(CHECKSUM(NewId())) % 6 -- this will return a random number between 0 and 5
SELECT (ABS(CHECKSUM(NewId())) % 5) + 1 -- this will return a random number between 1 and 5