PowerShellの罠

PowerShell 7

7.2

7.l

7.0

PowerShell 6

制御

スリープ

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

コマンド

Start-Process

Select-Object

CommonParameters?

自動変数

外部スクリプトファイル

外部スクリプトを実行

クラスファイルの読み込み

> . .\SomeClass.ps1

Tips

  • Powershell で省略しないで表示する
    • 以下のオプションを使用します。フォーマットとして自動サイズ調整、および自動改行のオプションを使用します。
      Format-Table -AutoSize -Wrap

PowerShellのバージョン確認

> $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のバージョン

コマンドの詳細表示

> Get-Help -Full [コマンド]
> help -full [コマンド]

プロパティ表示

> $someObject | Select-Object -Property *

プロパティの有無

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

オブジェクトの型名表示

> $someObject.GetType().FullName

オブジェクトの構造を表示

> $someObject | Get-Member | Out-Host -Paging

コンピュータ名

出力結果の非表示

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

スクリプトの実行時間を計測

Measure-Command { ****.ps1 }

Unixコマンドの代わり

cut

diff

  • 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

grep

  • 実行例
    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

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

touch

  • 作成日時
    > 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

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

wc -l

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

which

C#

ログ監視

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

メール送信

SQL Server

WCF

サンプル

トラブルシューティング

このシステムではスクリプトの実行が無効になっているため...

実行ポリシー

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

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-03-09 (水) 15:38:15 (779d)