Last active
July 18, 2020 04:51
-
-
Save SilkyFowl/9fa3ba40ff77bac51b6793f3d91a0a37 to your computer and use it in GitHub Desktop.
Powershell7.xのEncodingパラメータを拡張するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 7.0 | |
#Requires -Module PSParseHTML | |
using namespace System.Management.Automation | |
Import-Module PSParseHTML | |
# MSDNのHTMLを取得 | |
$rest = Invoke-RestMethod 'https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=net-5.0#remarks' | |
# 目的のテーブルを取得 | |
$tbl = ConvertFrom-HtmlTable -Content $rest | Where-Object { $_ | Get-Member 'Code page' } | |
# 既存のパラメータに候補に加えたいコードページを抽出して追加 | |
$Codepages = @( | |
'ascii' | |
'bigendianunicode' | |
'bigendianutf32' | |
'oem' | |
'unicode' | |
'utf7' | |
'utf8' | |
'utf8BOM' | |
'utf8NoBOM' | |
'utf32' | |
).ForEach{ | |
[PSCustomObject]@{ | |
Name = $_ | |
'Code page' = $_ | |
'Display name' = 'PowershellDefaultValue' | |
'.NET Framework support' = '✓' | |
'.NET Core support' = '✓' | |
} | |
} + $tbl | Where-Object { | |
($_.'.NET Framework support' -eq '✓') -or ($_.'Display name' -match 'Japanese') | |
} | |
# ↓でEncodingパラメータがあるコマンドレットを抽出、整形してクリップボードにセット | |
# Get-Command -Type Cmdlet -ParameterName Encoding | % {"`'{0}`'" -f $_.name} | Set-Clipboard | |
$hashArgs = @{ | |
CommandName = @( | |
'Add-Content' | |
'Export-Clixml' | |
'Export-Csv' | |
'Export-PSSession' | |
'Format-Hex' | |
'Get-Content' | |
'Import-Csv' | |
'Out-File' | |
'Select-String' | |
'Send-MailMessage' | |
'Set-Content' | |
) | |
ParameterName = 'Encoding' | |
ScriptBlock = { | |
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters) | |
# 入力状態でモードを切り替える | |
switch -Regex ($wordToComplete) { | |
# 数字が入力されている場合はコードページIDを列挙する | |
'^\d' { | |
$Codepages | Where-Object 'Code page' -Match "$wordToComplete" | ForEach-Object { | |
[CompletionResult]::new( | |
$_.'Code page', | |
("{0} ({1})" -f $_.'Code page', $_.Name), | |
[CompletionResultType]::Text, | |
$_.'Display name') | |
} | |
} | |
# それ以外の場合は名称を列挙する | |
Default { | |
$Codepages | Where-Object Name -Match "$wordToComplete" | ForEach-Object { | |
[CompletionResult]::new( | |
$_.Name, | |
("{0} ({1})" -f $_.Name,$_.'Code page'), | |
[CompletionResultType]::Text, | |
$_.'Display name') | |
} | |
} | |
} | |
} | |
} | |
Register-ArgumentCompleter @hashArgs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment