Skip to content

Instantly share code, notes, and snippets.

View turboBasic's full-sized avatar
🔮
Focusing

andriy melnyk turboBasic

🔮
Focusing
View GitHub Profile
@turboBasic
turboBasic / twitter.svg
Last active June 24, 2017 12:21
Twitter bird logo, new SVG version
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@turboBasic
turboBasic / restore_database.sh
Last active June 23, 2017 13:57 — forked from maoizm/restore_database.sh
mysql database: restore from sql dump command
#!/bin/bash
gunzip < $1 | mysql -uUSER -pPASSWORD --database=DATABASENAME
@turboBasic
turboBasic / facebook_f.svg
Last active June 24, 2017 12:03
Facebook f SVG logo
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@turboBasic
turboBasic / hexagram3D.png
Last active June 24, 2017 14:19
hexagram3D
hexagram3D.png
@turboBasic
turboBasic / Abstract-FunctionWithConfigBlockTemplate.ps1
Last active July 3, 2017 13:24
Template for Powershell function with constants block which you can override while invoking and add parameters of any kind from Command line
<#
.SYNOPSIS
Template of Function which takes any number of parameters
.DESCRIPTION
Template of Function which takes any number of parameters.
Function has default parameter block $defaults which can be overridden by command line parameters.
Function accumulates command line parameters and merges them with default parameters to the $__ENV.env hashtable.
@turboBasic
turboBasic / Merge-Hashtables.ps1
Last active July 8, 2017 23:43
Powershell function which merges Hashtables similarly to how lodash _.merge does. Answers question "How do I merge several hashtables while being able both add new keys and overwrite old ones?"
<# .SYNOPSIS
Merges any number of hashtables into one
.DESCRIPTION
Merges any number of hashtables taken both from pipeline and arguments, with the hashtables in the right overwriting the keys with the same names from hastables in the left
.EXAMPLE
$a = @{a = 'a1'; b = 'a2'}
$b = @{b = 'b1'; c = 'b2'}
$c = @{c = 'c1'; d = 'c2'}
@turboBasic
turboBasic / ConvertFrom-TextToHashtable.ps1
Last active July 3, 2017 20:45
Powershell function which reads hashtable text representation from file and converts it to hashtable object
# .SYNOPSIS
# Reads and evaluates Hastable object from file
#
# .DESCRIPTION
# Reads and evaluates Hastable object from file. As example you can read Module manifest
# file (*.psd1) as Hashtable object
#
# .EXAMPLE
# PS C:> Get-Module | Where { [IO.Path]::GetExtension($_) -match '.psd1' } |
# Select -Last 1 |
@turboBasic
turboBasic / Test-InvocationMethod.ps1
Last active July 8, 2017 23:35
Check invocation method inside script. Answers question "How do I check inside script if it was called or dot sourced?"
<# Determine invocation method: . | & | <script path >
https://poshoholic.com/2008/03/18/powershell-deep-dive-using-myinvocation-and-invoke-expression-to-support-dot-sourcing-and-direct-invocation-in-shared-powershell-scripts/
if ($MyInvocation.InvocationName -eq '&') {
"Called using operator"
} elseif ($MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq '') {
"Dot sourced"
} elseif ((Resolve-Path -Path $MyInvocation.InvocationName).ProviderPath -eq $MyInvocation.MyCommand.Path) {
"Called using path $($MyInvocation.InvocationName)"
}
@turboBasic
turboBasic / Get-Matches.ps1
Last active May 28, 2018 19:40
[Get-RegexMatchExamples] Using regex matches in powershell. Get-Matches() gets all regex matches. Answers question "How do I iterate through text file and print all matched regex groups for each line in file?" #powershell #regex
Get-Content SomeFile.ps1 |
ForEach { Select-String '(?ix) ^ \s* function \s+ ( [^{( ]+ ) \s* ( \( [^()]+ \) )? \{' -input $_ -Allmatch } |
ForEach { $_.Matches.Groups[1].Value }
Get-Content SomeFile.ps1 | Select-String '(?i)^function\s+((\w|-)+)' | ForEach { $_.Matches.Groups[1].Value }
Get-Content SomeFile.ps1 |
ForEach-Object { $_ -match '(?ix) ^ \s* function \s+ ( [^{( ]+ ) \s* ( \( [^()]+ \) )? \{' } |
@turboBasic
turboBasic / Test-FilenameValid.ps1
Last active July 8, 2017 23:37
Test if $fileName is valid file name. Answers question "How do I check if $fileName is completely valid file name in Windows 10?"
Function Test-FilenameValid {
PARAM( [PARAMETER( Mandatory )]
[String]
$filename
)
# IndexOfAny returns -1 if there is no any characters from argument in the string
$filename.IndexOfAny( [System.IO.Path]::GetInvalidFileNameChars() ) -eq -1