Skip to content

Instantly share code, notes, and snippets.

View JohnLBevan's full-sized avatar
🏠
Working from home

John Bevan JohnLBevan

🏠
Working from home
View GitHub Profile
@JohnLBevan
JohnLBevan / CreateServiceNowSqlTable.sql
Last active February 23, 2018 11:56
Create an MS SQL Server table definition from Service Now using the ODBC driver. Inspired by discussion on this thread: https://community.servicenow.com/community?id=community_question&sys_id=50705b29dbdcdbc01dcaf3231f9619e6
with sn (tableName, columnName, columnNameUI, max_length, internal_type) as
(
--See https://docs.servicenow.com/bundle/kingston-application-development/page/integrate/odbc-driver/concept/c_ODBCDrvrSQL20082012.html for info on ODBC driver & setting up a linked server
select name, element, column_label, max_length, internal_type
from openquery(SERVICENOW , '
select name
, element --database column name
, column_label --UI name
,CAST(max_length as Decimal(38,0)) max_length --could convert to string here; but this makes it more reusable
,internal_type
@JohnLBevan
JohnLBevan / CastToUniqueIdentifier.sql
Created February 22, 2018 18:13
Cast varchar GUID without hyphens to unique identifier
select cast(stuff(stuff(stuff(stuff('94b01c2b1386ae00a5383927d144b0c0',21,0,'-'),17,0,'-'),13,0,'-'),9,0,'-') as uniqueidentifier)
@JohnLBevan
JohnLBevan / Increase-ScreenBufferSize.ps1
Created February 14, 2018 08:14
When `Format-Table` has to truncate data / you need more screen width to output to, run this to create more space / enable horizontal scrolling
#show the current screen buffer size
$Host.UI.RawUI.BufferSize
#update the current screen buffer size
$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 0) #(width, height)
@JohnLBevan
JohnLBevan / Start-Computer.ps1
Created December 21, 2017 18:27
Initiate a wake on lan for a machine (or multiple machines).
function Start-Computer { #broadcasts a magic packet hoping to invoke the target machine's wake on lan function. NB: uses target computer's mac address rather than name / requires WOL to be enabled, and the device's network card to be listenning to broadcasts on the local subnet
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateScript({$_ -match '^(?:[^a-fA-F0-9]*[a-fA-F0-9][^a-fA-F0-9]*){12}$'})] #i.e. 12 hex digits (6 pairs) optionaly separated by any non-hex character/string. NB: this does mean 01:01 cannot be written as 1:1, for example.
[string]$MacAddress
,
[Parameter()]
[ValidateSet('IPv4','IPv6')]
[string]$IPVersion = 'IPv4'
@JohnLBevan
JohnLBevan / Clear-WindowsInstallerPatchInfo.ps1
Last active April 1, 2026 11:49
Clear-WindowsInstallerPatchInfo; cleanly/safely remove files from c:\windows\installer
#based on
# - http://blogs.msdn.com/b/heaths/archive/2007/01/31/how-to-safely-delete-orphaned-patches.aspx
# - http://www.bryanvine.com/2015/06/powershell-script-cleaning-up.html
# - https://p0w3rsh3ll.wordpress.com/2012/01/10/working-with-the-windowsinstaller-installer-object/
clear-host
function Get-WindowsInstallerPatchInfo {
[CmdletBinding()]
Param ()
@JohnLBevan
JohnLBevan / GetAxElementModelInfo.sql
Created November 13, 2017 17:26
MS Dynamics AX 2012 R2 :: Determine which model(s) an element is in.
use AX2012Db_Model
go
select l.Name LayerName
, mm.Name ModelName
, me.Name ElementName
from Model m
inner join Layer l on l.Id = m.LayerId
inner join ModelManifest mm on mm.ModelId = m.Id
inner join ModelElementData med on med.ModelId = m.id
@JohnLBevan
JohnLBevan / Get-PipelineParameter.ps1
Created October 26, 2017 11:47
Quick way to find which elements take input via the pipeline (it's not as simple as one would expect of PS)
#value from pipeline
Get-Help 'Get-Process' -Parameter * | ? PipeLineInput -like '*ByValue*'
#value from pipeline by property name
Get-Help 'Get-Process' -Parameter * | ? PipeLineInput -like '*ByPropertyName*'
#see the PSObjects returned by help, instead of the blasphemous "nicely formatted" output
Get-Help 'Get-Process' -Parameter * | select *
@JohnLBevan
JohnLBevan / PortScanner.ps1
Created October 25, 2017 16:10
Scan a given range of ports for a specific server. NB: Optimum group size is ~250
cls
workflow Test-PortOpenQuickly {
[cmdletbinding()]
param(
[Parameter(Mandatory = $true)]
[string]$ComputerName
,
[Parameter(Mandatory = $true)]
[int[]]$Ports
,
@JohnLBevan
JohnLBevan / Remove-NullProperty.ps1
Created October 25, 2017 12:17
Remove any entries from a hash table which hold null values. Useful if splatting where parameters are optional but not nullable. Bit of a generic approach (i.e. better to state exactly which properties we're interested in), but demonstrates the concept simply / can easily be adjusted by filtering for those key names we're interested in.
clear-host
$test = @{
One = 'hi'
Two = @{
A = 'a'
B = 'b'
}
Three = $null
Four = $false
}
@JohnLBevan
JohnLBevan / Invoke-AxQuery.ps1
Created October 9, 2017 13:12
Export microsoft dynamics ax 2009 object ids (i.e. contents of UtilIdElements "table") to CSV, using Business Connector to contact AX.
function Invoke-AxQuery {
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
$Company
,
[Parameter(Mandatory = $false)]
$Language
,