Skip to content

Instantly share code, notes, and snippets.


Christian Sandfeld csandfeld

  • Demant A/S
  • Copenhagen
View GitHub Profile
csandfeld / SGP201507.ps1
Created July 6, 2015 13:08
@csandfeld 2015-July Scripting Games Puzzle
View SGP201507.ps1
gwmi Win32_OperatingSystem -CN((icm{while(Read-Host -Pr 'ComputerName' -OV C){$CN+=$C}$CN}))|Select PSComputerName,ServicePackMajorVersion,Version,@{l='BIOSSerial';e={(gwmi Win32_BIOS -CN $_.PSComputerName).SerialNumber}}
View 2015-AugustScriptingGamesPuzzle.ps1
# One-liner, command and parameter names spelled out
Invoke-RestMethod -Uri '' | Format-Table -Property longitude, latitude, continent_code, timezone
# Advanced function wrapping the JSON endpoint
function Get-GeoInformation {
Get IP address geo information
csandfeld / 2015-SeptemberScriptingGamesPuzzle.ps1
Last active September 6, 2015 15:14
@csandfeld 2015 - September Scripting Games Puzzle
View 2015-SeptemberScriptingGamesPuzzle.ps1
# No semicolons, only one set of curlies, using aliases etc. to keep it as short as possible
ipcsv I*|%{$_|Add-Member OSVERSION (gwmi -CN $_.MACHINENAME Win32_OperatingSystem).Caption -P}|epcsv Output.csv
# Same as the above, but longer as I am not taking shortcuts with aliases and an incomplete filename
Import-Csv Input.csv | %{ $_ | Add-Member OSVERSION (Get-WmiObject -ComputerName $_.MACHINENAME Win32_OperatingSystem).Caption -PassThru } | Export-Csv Output.csv
# How I would have done it, had there been no limitations on lenght, semicolons
csandfeld / TrimCSV.ps1
Created September 26, 2015 12:08
Trim CSV data
View TrimCSV.ps1
# Specify input and output files
$InputCSVFile = '.\input.csv'
$OutputCSVFile = '.\output.csv'
# Import data from CSV
$CSVData = Import-Csv -Path $InputCSVFile
# Run trim() (remove leading and trailing whitespace) on all property values
$CSVTrimmedData = $CSVData | Foreach-Object {
csandfeld / 2015-OctoberScriptingGamesPuzzle.ps1
Last active October 4, 2015 21:35
@csandfeld 2015 - October Scripting Games Puzzle
View 2015-OctoberScriptingGamesPuzzle.ps1
function Get-RSSFeed {
Mandatory = $True
csandfeld / CreateIFScheduledTasks.ps1
Last active October 30, 2015 10:42
Image Factory: Create Scheduled Tasks
View CreateIFScheduledTasks.ps1
$TaskPath = ''
$TaskName = 'ImageFactory - Build {0} images'
csandfeld / VerifyTodaysLogExist.ps1
Last active November 3, 2015 19:37
Script to check if log file created today exist
View VerifyTodaysLogExist.ps1
# Folder where logfiles are stored
$Path = 'C:\TEMP'
# Filename pattern for regex match operator (<guid>.log)
$Pattern = '^.{8}-.{4}-.{4}-.{4}-.{12}\.log$'
$From = ''
$To = ''
$SmtpServer = 'smtpserver.domain.local'
$Subject = 'Todays log file was not found'
View 2015-NovemberScriptingGamesPuzzle.ps1
# Split string by comma, and trim of any whitespace
csandfeld / IsAttributeSetOrNot.ps1
Last active May 2, 2016 10:52
Working with file attributes
View IsAttributeSetOrNot.ps1
$Path = 'C:\TEMP\TEST1'
$Filter = '*.pdf'
$Attribute = [System.IO.FileAttributes]::Archive
# Get all files in $Path matching $Filter
Get-ChildItem -Path $Path -Filter $Filter -Recurse
# Get all files in $Path matching $Filter where $Attribute is set
Get-ChildItem -Path $Path -Filter $Filter -Recurse | Where-Object { $_.Attributes -band $Attribute }
csandfeld / Output.txt
Created January 9, 2019 16:51
Testing Custom Validators and Transforms
View Output.txt
VERBOSE: Loading module from path 'C:\Custom Validators and Transforms\TestMod.psm1'.
VERBOSE: Exporting function 'Test-OwnClass'.
VERBOSE: Exporting function 'Test-ValidatePathExists'.
VERBOSE: Exporting function 'Test-PathTransform'.
VERBOSE: Importing function 'Test-OwnClass'.
VERBOSE: Importing function 'Test-PathTransform'.
VERBOSE: Importing function 'Test-ValidatePathExists'.