#author("2021-12-03T00:21:30+00:00","default:admin","admin") -[[powershell チートシート:https://qiita.com/jca02266/items/a0c71ae90d055ab3893c]] -[[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 7 [#w2891f33] **7.2 [#k5f87665] -[[Microsoft、「PowerShell 7.2 Preview 2」を発表:https://www.atmarkit.co.jp/ait/articles/2012/25/news110.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]] *外部スクリプト呼び出し [#waf27a49] -[[【PowerShell】別の.ps1スクリプトや外部プログラムを引数付きで呼び出す:https://pig-log.com/powershell-call-program/]] *ファイル操作 [#s70b474c] -[[[PowerShell] フォルダ内のファイル一覧を取得し、一括でファイル操作をおこなう。:https://mseeeen.msen.jp/how-to-get-list-of-files-in-folder-with-powershell/]] -[[【Powershell】テキストファイルを分割する:http://souegg2.hatenablog.com/entry/2017/10/16/202230]] -カレントフォルダにある *.zip の中身を カレントフォルダに全て展開する Get-ChildItem -Recurse -File -Include *.zip | ForEach { Expand-Archive $_ -DestinationPath . } -指定した行数でファイル分割 $i=0; cat [分割対象ファイル] -ReadCount [行数] | % { $_ > [分割ファイル名]$i.txt;$i++ } *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]] **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" **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 "" } **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 **wc -l [#se7047d4] (Get-Content [ファイル] | Measure-Object).Count -カレントディレクトリ以下のソースファイルの総行数をカウント (dir -include *.cs,*.cpp,*.h,*.idl,*.asmx,*.js,*.css -recurse | select-string .).Count **cut [#a7427c45] -[[Windows PowerShell : UNIXの cut コマンド相当の作業をする。:https://www.engineer-memo.net/20110122-2750]] Get-Content “.\list.txt” | Select-Object {$($_-split(“,”))[0]} **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 **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/]] **実行ポリシー [#z2553ab8] -[[PowerShell のスクリプトが実行できない場合の対処方法:https://qiita.com/Targityen/items/3d2e0b5b0b7b04963750]] -[[【PowerShell】外部ファイル実行時にポリシーエラーが発生する際の対処方法:https://kaede.jp/2017/01/23005300/]] -指定スクリプトだけポリシー変更 PowerShell -ExecutionPolicy Unrestricted ./test.ps1