👴

[PowerShell]共通化したFunctionのサンプルコードを紹介

に公開

概要

主にZennの記事で自作したFunctionやサンプルとしてPowerShellスクリプトを紹介してきました。
それら記事の中で共通化(汎用的に使用)したFunctionをこの記事に集約。

PowerShellスクリプトを作成する際の参考にどうぞ!

この記事のターゲット

  • PowerShellユーザーの方
  • 頻繁に書く処理を共通化したい方
  • 共通化したFunctionのサンプルコードを参考にしたい方

Function一覧

個別の記事で紹介したFunction

詳細は下記の記事をご確認ください。

https://haretokidoki-blog.com/pasocon_collection-of-custom-ps-functions-on-zenn/

サンプルプログラムより抜粋

[PowerShell]CDなど記憶媒体にあるファイルの文字列をチェックするツール

https://zenn.dev/haretokidoki/articles/f1634c386ffc1d
https://github.com/akiGAMEBOY/PowerShell_CD-DetermineStringTool

CEトレイ(メディアトレイ)を自動でオープン

https://github.com/akiGAMEBOY/PowerShell_CD-DetermineStringTool/blob/master/Main.ps1#L46-L74

CLI操作でYes/Noの入力待ち

https://github.com/akiGAMEBOY/PowerShell_CD-DetermineStringTool/blob/master/Main.ps1#L108-L160

CDドライブ(メディアドライブ)が認識されているか検証

https://github.com/akiGAMEBOY/PowerShell_CD-DetermineStringTool/blob/master/Main.ps1#L161-L228

ファイル名と拡張子の検証

https://github.com/akiGAMEBOY/PowerShell_CD-DetermineStringTool/blob/master/Main.ps1#L229-L263

[PowerShell]コピーバックアップ&ローテーションの方法 - Local to NAS

https://zenn.dev/haretokidoki/articles/d93f7608754bff
https://github.com/akiGAMEBOY/PowerShell_FileCopybackupTool

バックアップローテーション

https://github.com/akiGAMEBOY/PowerShell_FileCopybackupTool/blob/master/source/powershell/Main.ps1#L124-L232

ファイルのコピーバックアップ

https://github.com/akiGAMEBOY/PowerShell_FileCopybackupTool/blob/master/source/powershell/Main.ps1#L233-L321

[PowerShell]“RPA画面操作:IME辞書をテキスト出力”と“単語登録済みかチェック”を自動実行する方法

https://zenn.dev/haretokidoki/articles/a1a89282137299
https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool

ダブルクォーテーション囲みを削除

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L91-L128

利用中のモニターサイズから指定の数値で除算しウィンドウサイズを取得

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L177-L214

GUI操作(Windowsフォーム)でYes/Noの入力待ち

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L215-L300

メッセージコード(enum)を検索キーにメッセージを取得

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L66-L85

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L553-L608

メッセージボックスの表示

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L609-L640

テキストファイルを検索対象に複数キーワードごとにカウント

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L641-L710

上記で取得した配列をコンソール上に表示

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L711-L783

一時的に保存する出力ファイルを格納するためのフォルダーを作成

https://github.com/akiGAMEBOY/PowerShell_IMEdictionarySearchTool/blob/master/source/powershell/Main.ps1#L834-L895

[PowerShell]MySQLデータをCSVファイルで取得する方法 - DB to CSV

https://zenn.dev/haretokidoki/articles/28a7467dedabfc
https://github.com/akiGAMEBOY/PowerShell_mysql-to-csv

文字列形式の日付(yyyymmdd)が正しい値か検証

https://github.com/akiGAMEBOY/PowerShell_mysql-to-csv/blob/master/source/Main.ps1#L39-L80

開始日付と終了日付が日付の期間として正しいか検証

https://github.com/akiGAMEBOY/PowerShell_mysql-to-csv/blob/master/source/Main.ps1#L81-L100

MySQLデータベースへの接続処理

https://github.com/akiGAMEBOY/PowerShell_mysql-to-csv/blob/master/source/Main.ps1#L101-L133

MySQLのSQLコマンドを実行

https://github.com/akiGAMEBOY/PowerShell_mysql-to-csv/blob/master/source/Main.ps1#L134-L164

画像をリサイズしてWebP形式に変換するPowerShellスクリプト

まだ記事として公開していないPowerShellスクリプト。
https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool

文字列がASCII文字だけで構成(英数字だけでマルチバイト文字がない)かの有無

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L845-L859

使用するファイル名を取得

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L860-L917

使用するフォルダー名を取得

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L1161-L1222

OCR(TesseractOCR)で出力したテキストをキーワード検索しカウント

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L918-L1018

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L1019-L1160

指定ファイルの拡張子を変更

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L1223-L1266

指定された画像ファイルのファイル形式をWebP形式に変換

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L1267-L1361

指定された画像ファイルのリサイズ処理

https://github.com/akiGAMEBOY/PowerShell_ResizeImageTool/blob/master/source/powershell/Main.ps1#L1362-L1555

[PowerShell]PDFファイル内の文字列を検索し存否を判定するツール

https://zenn.dev/haretokidoki/articles/cad8b141202136
https://github.com/akiGAMEBOY/PowerShell_SearchForPdffileStrings

注意事項:使用しているライブラリ“iTextSharp 5”のライセンスは「AGPLv3

https://github.com/akiGAMEBOY/PowerShell_SearchForPdffileStrings/blob/master/source/powershell/Main.ps1#L13-L15

指定キーワードでPDFファイル内を検索

https://github.com/akiGAMEBOY/PowerShell_SearchForPdffileStrings/blob/master/source/powershell/Main.ps1#L161-L207

[PowerShell]XMLファイルを整形(加工)するツール

https://zenn.dev/haretokidoki/articles/a77765dd56f202
https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool

XMLファイルの属性を一括チェック

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L32-L64

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L207-L234

XML属性の並び替え処理

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L235-L270

XMLで不要な属性の削除

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L271-L293

2つのテキスト形式ファイルを比較しコンソール上に出力

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L294-L354

XMLファイル内の文字列置換

https://github.com/akiGAMEBOY/PowerShell_ShapingXMLfileTool/blob/master/source/powershell/Main.ps1#L355-L393

Windows OSで一括アップデートするPSスクリプト(Win Update/MS Defender/アプリ)

https://zenn.dev/haretokidoki/articles/a5d1a471a5699a
https://github.com/akiGAMEBOY/PowerShell_WindowsUpdateTool

アクティブウィンドウの切り替え

https://github.com/akiGAMEBOY/PowerShell_WindowsUpdateTool/blob/master/source/powershell/Main.ps1#L432-L458

https://github.com/akiGAMEBOY/PowerShell_WindowsUpdateTool/blob/master/source/csharp/ActiveWindow.cs#L1-L136

引数で指定のモジュールをインストール

https://github.com/akiGAMEBOY/PowerShell_WindowsUpdateTool/blob/master/source/powershell/Main.ps1#L878-L933

まとめ

わたし自身が新規でPowerShellスクリプトを作成する際に便利そうなので、この記事にまとめてみました。
これらFunctionをプロファイルに登録するとより便利かも。

関連記事

https://haretokidoki-blog.com/pasocon_powershell-startup/
https://zenn.dev/haretokidoki/articles/7e6924ff0cc960

GitHubで編集を提案

Discussion