#author("2017-11-27T14:38:24+00:00","default:admin","admin")
#author("2022-03-09T06:38:15+00:00","default:admin","admin")
-[[powershell チートシート:https://qiita.com/jca02266/items/a0c71ae90d055ab3893c]]
-[[Windows PowerShell のヘルプ:https://forsenergy.com/ja-jp/windowspowershellhelp/]]

-[[Windows/PowerShell:https://wiki.takeash.net/Windows/PowerShell]]
-[[PowerShell - WIN.JUST4FUN.BIZ:https://win.just4fun.biz/?PowerShell]]

-[[Windows PowerShell でのスクリプティング:http://www.microsoft.com/japan/technet/scriptcenter/hubs/msh.mspx]]
-[[Windowsスクリプティング環境比較:PowerShell vs WSH:http://www.atmarkit.co.jp/fwin2k/operation/pshvswsh/pshvswsh_01.html]]
-[[Windows PowerShell徹底解説:http://itpro.nikkeibp.co.jp/article/COLUMN/20061106/252598/]]

-[[それ PowerShell でできるよ:https://qiita.com/cd01/items/da9a36582372e7d0a7f6]]
-[[PowerShell 使い方メモ:http://qiita.com/opengl-8080/items/bb0f5e4f1c7ce045cc57]]

-[[なぜPowerShellが危険なのか? 増え続ける悪用の現状:http://www.itmedia.co.jp/enterprise/articles/1702/08/news018.html]]
-[[MicrosoftがPowerShellをオープンソース化しLinuxやOS Xにも提供…Bash on Windowsとの差別化は?:http://jp.techcrunch.com/2016/08/19/20160818microsoft-open-sources-powershell-brings-it-to-linux-and-os-x/]]

-[[ナンバーズ予想で学ぶ PowerShell によるデータ分析:https://qiita.com/mwmsnn/items/0e3e5cc1980272858ad8]]

-[[PowerShellでファイルにリダイレクトしたときに勝手に改行される場合の対処:https://qiita.com/gimKondo/items/20e1a5a1b2898c5c5fbd]]
-[[PowerShellでSendKeysを使ってみた:https://qiita.com/nimzo6689/items/488467dbe0c4e5645745]]

-[[PowerShell のスコープ完全に理解した:https://tech.blog.aerie.jp/entry/powershell-advent-calendar-2018-18]]

-[[PowerShell「全員が全員 /bin/bash だと思うなよ」:https://chot-inc.com/blog/w_6zn69es0/]]

*PowerShellの罠 [#fc83b573]
-[[PowerShellの至る所に潜む罠:https://www.pageone.ne.jp/blog/notes/trap-in-powershell/]]
-[[PowerShellの"罠"と呼ばれるモノについて:https://blog.shibata.tech/entry/2018/05/31/214439]]

*PowerShell 7 [#w2891f33]
**7.2 [#k5f87665]
-[[Microsoft、「PowerShell 7.2」の一般提供を開始:https://atmarkit.itmedia.co.jp/ait/articles/2111/12/news121.html]]

**7.l [#a6f2c01e]
-[[PowerShell 7.1がリリースされました:https://dev.classmethod.jp/articles/powershell-7-1-generally-available/]]

**7.0 [#n84dd2dd]
-[[Microsoft、「PowerShell 7.0」を一般公開 ~三項演算子やnull結合演算子をサポート:https://forest.watch.impress.co.jp/docs/news/1238950.html]]
-[[PowerShell CoreからPowerShell 7に至る道:https://www.slideshare.net/stknohg/powershell-corepowershell-7]]

*PowerShell 6 [#v07a75b7]
-[[PowerShell Core 6 の入門メモ:https://qiita.com/rubytomato@github/items/988885d3e9860239ab88]]

*制御 [#ga96153f]
-[[powerShellで無限ループと待機処理:http://harikofu.blog.fc2.com/blog-entry-77.html]]

**スリープ [#aea9b2e8]
-[[【PowerShell】指定した期間動作を一時停止する:https://hosopro.blogspot.com/2017/01/powershell-start-sleep.html]]

-繰り返し実行
 while (1) {Start-Process C:\bin\test.bat -WindowStyle Hidden; Start-Sleep -s 300}

*コマンド [#b2dddd24]
**Start-Process [#sa3d5baa]
-[[【Start-Process】PowerShellでプロセスをバックグラウンド起動する方法:https://cheshire-wara.com/powershell/ps-cmdlets/system-service/start-process-window/]]

**Select-Object [#t343706d]
-[[Select-Object コマンドレットを復習:https://qiita.com/miyamiya/items/059430d5499f0fe336ff]]

*CommonParameters [#p371832a]
-[[about_CommonParameters:https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.2]]
-[[共有パラメーター名:https://docs.microsoft.com/ja-jp/powershell/scripting/developer/cmdlet/common-parameter-names?view=powershell-7.2]]

*自動変数 [#l04af21f]
-[[about_Automatic_Variables:https://docs.microsoft.com/ja-jp/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.2]]

*外部スクリプトファイル [#f726b8dd]
**外部スクリプトを実行 [#gaed5d84]
-[[【PowerShell】別の.ps1スクリプトや外部プログラムを引数付きで呼び出す:https://pig-log.com/powershell-call-program/]]

**クラスファイルの読み込み [#y33fd879]
-[[クラスを別ファイルに定義して読み込む方法:https://bayashita.com/p/entry/show/220]]

 > . .\SomeClass.ps1

*色 [#r269523d]
-[[10分でWindows PowerShellの配色とフォントを変更して”使える”ツールに変貌させる:https://mimimopu.com/windows-powershell-color-font-change/]]

*Tips [#h5b822ef]
-[[Powershell で省略しないで表示する:http://tooljp.com/qa/Powershell-not-Omit-735E.html]]
--以下のオプションを使用します。フォーマットとして自動サイズ調整、および自動改行のオプションを使用します。
 Format-Table -AutoSize -Wrap

-ウィンドウを表示しない
--[[[PowerShell] スクリプトファイルの実行方法について:https://letspowershell.blogspot.com/2016/03/powershell.html]]

**PowerShellのバージョン確認 [#r70be8e4]
 > $PSVersionTable
 
 Name                           Value
 ----                           -----
 PSVersion                      5.1.19041.1320
 PSEdition                      Desktop
 PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
 BuildVersion                   10.0.19041.1320
 CLRVersion                     4.0.30319.42000
 WSManStackVersion              3.0
 PSRemotingProtocolVersion      2.3
 SerializationVersion           1.1.0.1

-上記の「PSVersion」がPowerShellのバージョン

**コマンドの詳細表示 [#m10b6ba2]
 > Get-Help -Full [コマンド]
 > help -full [コマンド]

**プロパティ表示 [#v11b4dc6]
 > $someObject | Select-Object -Property *

**プロパティの有無 [#l6372cbd]
-[[オブジェクトに特定のプロパティがあるかどうかをテストするにはどうすればよいですか?:https://www.webdevqa.jp.net/ja/powershell/%E3%82%AA%E3%83%96%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%81%AB%E7%89%B9%E5%AE%9A%E3%81%AE%E3%83%97%E3%83%AD%E3%83%91%E3%83%86%E3%82%A3%E3%81%8C%E3%81%82%E3%82%8B%E3%81%8B%E3%81%A9%E3%81%86%E3%81%8B%E3%82%92%E3%83%86%E3%82%B9%E3%83%88%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%99%E3%81%8B%EF%BC%9F/1050654503/]]

 > [bool]($myObject.PSobject.Properties.name -match "myPropertyNameToTest")
--[bool]を付けないと、マッチした文字列が返されるので注意

**オブジェクトの型名表示 [#yeae6542]
 > $someObject.GetType().FullName

**オブジェクトの構造を表示 [#x2fe51ca]
 > $someObject | Get-Member | Out-Host -Paging

**コンピュータ名 [#f2397051]
-[[【PowerShell】ローカルのホスト名(コンピューター名)を取得する方法:https://buralog.jp/powershell-get-hostname-or-computername/]]
 Get-WMIObject -Class Win32_Bios | Select-Object PSComputername

**出力結果の非表示 [#ld0e7e9d]
-[[【PowerShell】出力結果を無視する方法:https://buralog.jp/powershell-ignore-output/]]
-[[【PowerShell】結果の出力を非表示にする方法3選:https://chi-kou.com/ps-result-invisible/]]

 > [スクリプト] | Out-Null

**スクリプトの実行時間を計測 [#f7d5c34d]
 Measure-Command { ****.ps1 }

*Unixコマンドの代わり [#h9c22df5]
**cut [#a7427c45]
-[[Windows PowerShell : UNIXの cut コマンド相当の作業をする。:https://www.engineer-memo.net/20110122-2750]]
 Get-Content “.\list.txt” | Select-Object {$($_-split(“,”))[0]}

**diff [#w3fc8d4b]
-[[PowerShell で diff (テキスト ファイルを Compare-Object):https://qiita.com/ktrarai/items/8bbc7566c82a3fc6d063]]

-2つのテキストファイルの差分表示
 diff (cat [ファイルA]) (cat [ファイルB])
-2つのテキストファイルの差分表示(行番号付き)
 diff (cat [ファイルA]) (cat [ファイルB]) | Select-Object -Property @{Name = 'ReadCount'; Expression = {$_.InputObject.ReadCount}}, * | Sort-Object -Property ReadCount

**find [#y97cb6c4]
-[[PowerShellではbashでいうfindコマンドってないの?:https://bitto.jp/posts/%E6%8A%80%E8%A1%93/Windows/pwsh-get-childitem/]]
 Get-ChildItem -r -Filter "*.txt" -Name 

**grep [#m20ce9b6]
-[[grepコマンドとPowerShellのsls (Select-String)の比較:http://tech.sanwasystem.com/entry/2016/07/05/185717]]
-[[Windowsでgrep:http://d.hatena.ne.jp/ys4145/20110408/p1]]
-[[powershellでCSV等の区切り文字があるファイルの数値列の合計を計算する:https://qiita.com/gyoon/items/a74fe70959e51d584951]]
-[[Powershell > 再帰的grep > dir -Recurse | Select-String -Pattern "XPAR_LED4":https://qiita.com/7of9/items/fcaca74596286226d321]]

-実行例
 type target.txt | sls "foo" | sls "bar" | % { $_.ToString() } > hoge.txt

-「ファイル名:行番号」を出力しない
--これが一番良さそう
 cat [ファイル名] | sls [検索文字列] | Out-String -Width 4096
--select Line はイマイチ
 select-string [検索文字列] | select Line | Out-String -Width 4096
 select-string [検索文字列] | select Line | Format-Table -Property * -AutoSize | Out-String -Width 4096

-OR検索
 Select-String -Path "*.txt" -Pattern "hello","world"

-Not検索
 Select-String -Path "*.txt" -NotMatch -Pattern "hello","world"

-空行削除
 (gc [ファイル名] | ? {$_.trim() -ne "" }

**tail [#f090da93]
 > Get-Content C:\var\log\nlog-own-2017-11-27.log -wait -tail 10 -Encoding UTF8

**touch [#g0a801f1]
-作成日時
 > Set-ItemProperty .\test.txt -Name CreationTime -Value "2018/06/12 15:30:00"
-更新日時
 > Set-ItemProperty .\test.txt -Name LastWriteTime -Value "2018/06/12 15:30:00"

**uniq [#z76e4217]
-[[オブジェクトをユニークにして存在数を付加する PowerShell フィルター:https://www.vwnet.jp/Windows/PowerShell/2016092401/CountUnique.htm]]
-[[PowerShell で、多項目中、複数項目の数の集計:https://qiita.com/bunzaemon/items/43e515bf07379a5ba2af]]

-項目毎の件数を求める(uniq -c)
 cat hoge.txt | group $_ | select name,count

**wc -l [#se7047d4]
 (Get-Content [ファイル] | Measure-Object).Count

-カレントディレクトリ以下のソースファイルの総行数をカウント
 (dir -include *.cs,*.cpp,*.h,*.idl,*.asmx,*.js,*.css -recurse | select-string .).Count

**which [#w0f4a109]
-[[PowerShellでwhichコマンド:https://qiita.com/Hiraku/items/e42bc5756157949a9742]]

*C# [#c4ee1ff3]
-[[PowerShellからC#, SQL Serverのコードをとっても便利に呼び出す:http://threeprogramming.lolipop.jp/blog/?p=812]]

*ログ監視 [#dcfd0263]
-[[作業が捗る!Powershellでログ監視ができるコマンドまとめ:https://www.geekfeed.co.jp/geekblog/powershell-Get-Content/]]
-[[PowerShellでログファイルを監視してWebhookで通知する:https://qiita.com/Brutus/items/cf96123d1ba4c328eb4a]]
-[[PowerShell+tailでログファイルのエラー監視を行う:https://kbn48.com/technical/302]]
-[[PowerShellでtail -fを再現する:https://tech.guitarrapc.com/entry/2013/04/15/220408]]

-5秒お気にGet-Processを実行して./test.logに追記
 1..100 | %{Get-Process | Out-File ./test.log -Append; sleep 5}

*メール送信 [#m4bbfd5c]
-[[PowerShellでメール送信:https://qiita.com/arachan@github/items/e93c2fec8fe5f7f66bfa]]

*SQL Server [#laa48f99]
-[[PowerShellからC#, SQL Serverのコードをとっても便利に呼び出す:http://threeprogramming.lolipop.jp/blog/?p=812]]
-[[SQL Server に接続してテーブルの内容を SELECT する方法:https://bayashita.com/p/entry/show/107]]

*WCF [#o9a513ce]
-[[PowerShell で WCF:http://csharper.blog57.fc2.com/blog-entry-117.html]]
-[[WCF サービス開発には PowerShell を活用しよう:http://csharper.blog57.fc2.com/blog-entry-185.html]]

*サンプル [#jcb548fd]
-[[スクリーンセーバ抑止PowerShell:https://qiita.com/kfjt/items/d383b5ebec27c3e1dd42]]

*トラブルシューティング [#yb037a4d]
-[[Windows PowerShell から NuGet プロバイダーがインストールできなくなった原因と対処方法について:https://kogelog.com/2020/05/18/20200516-01/]]

**このシステムではスクリプトの実行が無効になっているため... [#u0af09cf]
-[[PowerShellでこのシステムではスクリプトの実行が無効になっているため、ファイル hoge.ps1 を読み込むことができません。となったときの対応方法:https://qiita.com/ponsuke0531/items/4629626a3e84bcd9398f]]

**実行ポリシー [#z2553ab8]
-[[PowerShell のスクリプトが実行できない場合の対処方法:https://qiita.com/Targityen/items/3d2e0b5b0b7b04963750]]
-[[【PowerShell】外部ファイル実行時にポリシーエラーが発生する際の対処方法:https://kaede.jp/2017/01/23005300/]]

-指定スクリプトだけポリシー変更
 PowerShell -ExecutionPolicy Unrestricted ./test.ps1


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS