■環境
widows2012R2にリモート接続
PowerShellを使用
■目的
・下記のコマンドを短くしたいです。
■コマンドの詳細
1.対象のフォルダから最新のファイルを検索
2.上記のファイル名を文字列検索し、3通りの処理分岐をする。
3.上記のファイル内で文字列検索(検索する文字列は3つ)と一致文字列の個数を数える。
4.事前に用意している3種類の文字列の個数が上記で数えた文字列の個数と一致しているかどうか精査する。
PowerShell
1# 対象ファイルのあるフォルダ 2$search = "<対象フォルダパス>" 3 4## サービスの文字列の環境別個数ト(サービス名,教育,検証,ステージングと本番) 5# 実際カウント数は $A0102,$Apache,$Shibboleth に格納 6$word_1 = @("<文字列1>","a1","a2","a3") 7$word_2 = @("<文字列2>","b1","b2","b3") 8$word_3 = @("<文字列3>","c1","c2","c3") 9 10# 最終更新のファイルのフルパスを取得 11$path = (Get-ChildItem $search | Sort-Object LastWriteTime -Desc)[0].FullName 12 13# ファイル名に文字列が含まれるかどうかの関数 14function word ($a){$path.Contains($a)} 15 16# ファイル内の文字列を数える 17function count ($b,$c){ 18 (Select-String -Path $b -Pattern $c).Count 19} 20$x1 = (count $path $word_1[0]) 21$x2 = (count $path $word_2[0]) 22$x3 = (count $path $word_3[0]) 23 24# 数えた文字列の個数が正規の個数と一致するかどうか判別する関数 25function seisa ($1,$2,$3,$4,$5,$6){ 26 if($1 -eq $2 -and $3 -eq $4 -and $5 -eq $6){ 27 Write-Output ("件数が一致しています。") 28 }else{ 29 Write-Output ("件数が一致していません。") 30 } 31} 32 33# 数えた結果を出力 34function message ($1,$2,$3,$4,$5,$6){ 35 Write-Output ($word_1[0]+"は"+$1+"/"+$2+"個あります。") 36 Write-Output ($word_2[0]+"は"+$3+"/"+$4+"個あります。") 37 Write-Output ($word_1+"は"+$5+"/"+$6+"個あります。") 38} 39 40if(word "ED1" -eq "True"){ 41 seisa $word_1[1] $x1 $word_2[2] $x2 $word31[1] $x3 42 message $x1 $word_1[1] $x2 $word_2[1] $x3 $word_3[1] 43}elseif(word "KT1" -eq "True"){ 44 seisa $word_1[2] $x1 $word_2[3] $x2 $word_3[2] $x3 45 message $x1 $word_1[2] $x2 $word_2[2] $x3 $word_3[2] 46}elseif(word "STA"] -eq "True"){ 47 seisa $word_1[1] $x1 $word_2[2] $x2 $word_3[3] $x3 48 message $x1 $word_1[3] $x2 $word_2[4] $x3 $word_3[3] 49}elseif(word "HON" -eq "True"){ 50 seisa $word_1[1] $x1 $word_2[2] $x2 $word_3[3] $x3 51 message $x1 $word_1[3] $x2 $word_2[4] $x3 $word_3[3] 52}Else{ 53 Write-Output ("ファイルが見つかりません。") 54} 55
自分なりに短くしようと試みましたが、他に分かりやすく便利なコマンドがあれば教えていただけると幸いです。
あと、タイトルにどう書けばいいか悩んでいますので違うようであればご指摘お願い致します。
あなたの回答
tips
プレビュー