Skip to content

Instantly share code, notes, and snippets.

@altrive
altrive / New-IseCustomSnippet.ps1
Last active June 8, 2019 04:44
Utility function to create custom PowerShell ISE code snippets
function New-IseCustomSnippet
{
param (
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory)]
[string] $Title,
[ValidateNotNullOrEmpty()]
[string] $Description = "Description",
[string] $Author = "",
[Parameter(Mandatory)]
using System;
using System.Runtime.InteropServices;
public static class Program
{
[DllImport("libc")]
static extern void printf(string format, int value);
[DllImport("libc")]
@altrive
altrive / XmlElementRemoting.ps1
Created November 23, 2013 18:55
XmlElement can't pass remote command parameter.
$xmlDocument = [xml] "<root><elem>abcd</elem></root>"
$xmlElement = $xmlDocument.root
Invoke-Command localhost -ScriptBlock {
$arg = $using:xmlDocument
Write-Host ("Type={0}, Value={1}" -f $arg.GetType(), $arg.OuterXml)
}
Invoke-Command localhost -ScriptBlock {
$arg = $using:xmlElement
@altrive
altrive / config.ps1
Created November 23, 2013 17:02
Sample config to create Hyper-V VM
#global variable
$global:ctx = @{
VmName = "Windows 8.1"
Operation = ""
Session = $null
ComputerName = "TestPC"
DomainJoin =@{
Name = "TEST.local"
Credential = Get-SavedCredential -Id DomainAdmin
@altrive
altrive / PSModuleLoading.psm1
Created November 23, 2013 14:41
Custom PSmodule sample to load command defined separated files using dot-sourcing.
#Write-Host "Load script files..." -ForegroundColor DarkGray
$scriptFiles = Get-ChildItem "$PSScriptRoot\*.ps1" -Exclude "*.Tests.ps1" -Recurse
foreach ($script in $scriptFiles)
{
#Write-Host "`tLoad script:" $script.Name -ForegroundColor DarkGray
try
{
#TODO: Suppress $Error entry recorded after module loading completed(can't suppress inside psm1 module?)
. $script.FullName
@altrive
altrive / CollectionFilteringPerformance.ps1
Last active December 28, 2015 11:49
Compare PowerShell collection filtering performance
$N = 1000
$services = Get-Service
# 1.Powershell v2 'where' filtering syntax
$sw = [Diagnostics.Stopwatch]::StartNew()
foreach ($i in 1..$N)
{
$services | where { $_.Status -eq 'Running' } > $null
}
@altrive
altrive / Add-ISEAddinWithMultiKeyShortcut
Last active May 23, 2023 13:51
PowerShell ISE don't support multi-sequence keyboard shortcuts like Ctrl+K,Ctrl+D by default. It need to create MultiKeyGesture class inherited from KeyGesture.
$ErrorActionPreference = "Stop"
function Main
{
if ($Host.Name -ne "Windows PowerShell ISE Host"){
return
}
$menus = $psISE.CurrentPowerShellTab.AddOnsMenu.Submenus
@altrive
altrive / Show-GitHubRepositoryChartByMonth.ps1
Last active January 15, 2016 13:58
Create chart, Github PowerShell repository created by month.
$ErrorActionPreference = "Stop"
function Main
{
$list = New-Object Collections.ArrayList
foreach ($year in 2010..2013)
{
foreach ($month in 1..12)
{
$date = New-Object DateTime($year, $month, 1)
@altrive
altrive / PSFeatureRequest.md
Last active April 13, 2018 09:29
PowerShellに追加してほしい機能のメモ書き

PowerShellに追加してほしい機能

  1. varキーワードによる変数宣言。変数宣言の場所がどこなのかわからないので。

  2. classキーワードによるクラス構文。オブジェクト指向としての機能は基本不要だけど、変数と操作メソッドをセットで扱いたいことがある。

  3. usingによるリソース破棄構文。try/finallyはネストが深くなりすぎるので。

  4. 三項演算子(?)とnull合体演算子(??)のサポート。

@altrive
altrive / Add-TypeDefinition
Last active December 24, 2015 07:49
Sample code to to import C# type definition to avoid type name conflict.when Add-Type called multiple times.
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Main
{
Add-TypeDefinition
$sw = [Diagnostics.Stopwatch]::StartNew()
$task1 = [PSContext]::StartNew({ Get-Process; sleep 2;})