LoginSignup
3
4

More than 5 years have passed since last update.

PowerShell備忘録(オプション、CSVファイル読み込み、他プログラム実行)

Last updated at Posted at 2018-09-15

インストール

PowrtShellはwindows7以降なら入ってるみたい(古いかもだけど)。
知らなかったけどMacやらLinuxやらも導入できるらしい1

install
brew tap
brew update
brew cask install powershell #PowerShellインストール
pwsh #PowerShell起動

オプション

PowerShellをWindows環境で簡単に実行できるようにBATファイルでやるつもりなので、対して意味がない気もするが一応使う。
このサイト2しか基本的に見なかった。
型指定できたり、必須にしたり、色々できるみたい。

param.ps
Param(
       [parameter(mandatory)][string]$filePath, #対象ファイルパス(必須)
       [int]$number, #なんらかの数字
       [array]$others, #配列もいけるらしい
       [switch]$flag #真偽値
)

csvファイルの読み込み

PowerShellだと簡単にCSVファイルを読み込めるらしい。
MSのサイト3みたり、ブログ4見たりした。

$csvFile = Import-Csv -Encoding UTF8 -Delimiter "," -Path $filePath

PowerShellから他プログラムを実行

今回、前作ったPowerShell、perlとかを呼び出そうと思ってるので、その辺について書かれたサイト5を参考にした。

プログラムのひながた

結果こんなんにした。$debugがわかりやすいと思ったけど使えなかったので諦めた。

test.ps1(mac)
Param(
    [parameter(mandatory)][String]$filePath, # csvファイルのパス
    [switch]$de # debugモードのスイッチ
    )

# パラメータを表示
Write-Output "入力パラメータ:filePath(CSV): $filePath"

# CSVファイルを読み込み、表示
$csvFile = Import-Csv -Encoding UTF8 -Delimiter "," -Path $filePath

# 他プログラム
$programA = "testA.ps1"
$programB = "testB.ps1"

foreach($currentData in $csvFile) {
    $Type = $currentData.Type
    $Name = $currentData.Name
    $Height = $currentData.Height
    $Weight = $currentData.Weight
    # Printf("Name:%s, Height:%s, Weight:%s\n",$Name,$Height,$Weight)
    $options = ""
    $options += "-Name '$Name'" # $変数名でもおおむね使えるよ
    $options += " -Height '$($Height)'" # ()で括ると英語続きとかでもOKだけど$が要るよ
    $options += " -Weight " + $Weight
    if ($de ) {
        printf("オプション:%s\n", $options) # オプション表示
    }
    if ($Type -eq "A") {
        if($de) {
            printf("実行コマンド:Start-Process -Wait -NoNewWindow -f $programA $options\n") # 実行コマンド表示
        }
        # プロセスの実行を待ち、新しいウィンドウを開かない
        #Start-Process  -Wait -NoNewWindow -f $programA $options
    } elseif($Type -eq "B") {
        if($de) {
            printf("実行コマンド:Start-Process -Wait -NoNewWindow -f $programB $options\n") # 実行コマンド表示
        }
        # プロセスの実行を待ち、新しいウィンドウを開かない
        #Start-Process  -Wait -NoNewWindow -f $programB $options
    } else {
        "★★この行にはオプションがないよー★★"
    }
}
./csv
Type,Name,Height,Weight
A,tako,96,92
B,neko,60,54
,sako,100,34
C,naiko,100,34
結果
PS > ./test.ps1 -filePath ./csv
入力パラメータ:filePath(CSV): ./csv
★★この行にはオプションがないよー★★
★★この行にはオプションがないよー★★

PS > ./test.ps1 -filePath ./csv -de
入力パラメータ:filePath(CSV): ./csv
オプション:-Name 'tako' -Height '96' -Weight 92
実行コマンド:Start-Process -Wait -NoNewWindow -f testA.ps1 -Name 'tako' -Height '96' -Weight 92
オプション:-Name 'neko' -Height '60' -Weight 54
実行コマンド:Start-Process -Wait -NoNewWindow -f testB.ps1 -Name 'neko' -Height '60' -Weight 54
オプション:-Name 'sako' -Height '100' -Weight 34
★★この行にはオプションがないよー★★
オプション:-Name 'naiko' -Height '100' -Weight 34
★★この行にはオプションがないよー★★

WindowsのPowerShellでPrintfが使えない

Windows10で先日試してみたらprintfが使えなかったので、
別の書き方6に直した。こっちはこっちでMacじゃ使えない><。。。

test1.ps1(Windows)
Param(
    [parameter(mandatory)][String]$filePath, # csvファイルのパス
    [switch]$de # debugモードのスイッチ
    )

# パラメータを表示
Write-Output "入力パラメータ:filePath(CSV): $filePath"

# CSVファイルを読み込み、表示
$csvFile = Import-Csv -Encoding UTF8 -Delimiter "," -Path $filePath

# 他プログラム
$programA = "testA.ps1"
$programB = "testB.ps1"

foreach($currentData in $csvFile) {
    $Type = $currentData.Type
    $Name = $currentData.Name
    $Height = $currentData.Height
    $Weight = $currentData.Weight
    # Write-Output "Name:{0}, Height:{1}, Weight:{2}", -f $Name,$Height,$Weight
    $options = ""
    $options += "-Name '$Name'" # $変数名でもおおむね使えるよ
    $options += " -Height '$($Height)'" # ()で括ると英語続きとかでもOKだけど$が要るよ
    $options += " -Weight " + $Weight
    if ($de ) {
        Write-Output "オプション:{0}" -F $options # オプション表示
    }
    if ($Type -eq "A") {
        if($de) {
            Write-Output "実行コマンド:Start-Process -Wait -NoNewWindow -f $programA $options`n" # 実行コマンド表示
        }
        # プロセスの実行を待ち、新しいウィンドウを開かない
        #Start-Process  -Wait -NoNewWindow -f $programA $options
    } elseif($Type -eq "B") {
        if($de) {
            Write-Output "実行コマンド:Start-Process -Wait -NoNewWindow -f $programB $options`n" # 実行コマンド表示
        }
        # プロセスの実行を待ち、新しいウィンドウを開かない
        #Start-Process  -Wait -NoNewWindow -f $programB $options
    } else {
        "★★この行にはオプションがないよー★★" # Write-Outputって書かなくても同じように実行されるよ
    }
}
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4