====== Powershell サンプルコード集 ====== 中途半端な部分がある場合があります。順次修正したい。 =====ランチャー的なもの===== 起動してちゃんと動くような書き方しかしてないので作法がなってないかもです。 app.iniという設定ファイルを読んでランチャー的なものを起動するコード [app.ini] ;コメント行 ;一番上に表示する TITLE=ボタンから実行したい処理を選択してください ;ボタン表示名と実行コマンド ;引数を付加する場合は「>」を追加で記述し、そのあとに引数を続けること 電卓を起動することができるかもしれないボタンだよ?>cmd.exe 電卓>calc.exe memo>notepad.exe>OpenTest.txt コメントボタン> ;押しても効かないボタン 簡易メモの代わり [SETUP.ps1] $DIR=Split-Path $myInvocation.MyCommand.path -Parent #実行パス取得 $NAME=$MyInvocation.MyCommand.Name -replace ".ps1",".ini" $INIT_FILE= $DIR + "\" + $NAME $ROW=40 $TABLS=@{} #連想配列 #初期化処理 #設定ファイルからの値読み込み function init($form) { $contents=get-content -LiteralPath $INIT_FILE write-host $INIT_FILE if(!$contents) { $msg="設定ファイル "+$INIT_FILE+" が読み込めませんでした。" [System.Windows.Forms.MessageBox]::Show($msg, "実行時エラー") return } $CNT=1 foreach($l in $contents) { if($l[0] -eq ";") #行頭「;」はコメント行 { continue } if($l.Length -eq 0){ #空行はスキップ continue } if($l.StartsWith("TITLE=")) { $title=$($l -split "=")[1] $lb_title=New-Object System.Windows.Forms.Label $form.Controls.Add($lb_title) $lb_title.Text=$title $lb_title.Width=200 $lb_title.Height=$ROW-3 $lb_title.size=New-Object System.Drawing.Size(270,35) $lb_title.Top = 2 $lb_title.Left = 10 $lb_title.Backcolor = "white" continue } $datas = $l -split ">" $btnok= New-Object System.Windows.Forms.Button $btnok.Text=$datas[0] $btnok.Top = $ROW * $CNT $btnok.Add_Click({ev $this $_ }) $btnok.size=New-Object System.Drawing.Size(290,35) #$btnok.Width=250 $indata="" if($datas.Length -gt 2){ #引数対応のため $indata=$datas[1]+">"+$datas[2] } else{ $indata=$datas[1] } $script:TABLS[$datas[0]]=$indata $form.Controls.Add($btnok) if($CNT -gt 7 -And $CNT -lt 15) { $form1.Height=$form1.Height+35+3 } $CNT=$CNT+1 } } #イベント発生時に動作する処理 function ev( $sender, $e) { $var=$TABLS[$sender.Text] # 引数がつく場合の処理 $arg="" if(($var -split ">").Length -ne 1){ $arg=($var -split ">")[1] $var=($var -split ">")[0] } #カギカッコ[]をエスケープ $var=$var -replace "\[","``[" $var=$var -replace "\]","``]" #実行コマンドがないときはボタン押されても実行しない if( $var -eq "" ){ return; } try{ #Start-Process -FilePath "tmp.bat" -Wait -Verb runas #Start-Process $TABLS[$sender.Text] #-Verb runas if($arg.Length -eq 0){ Start-Process $var } else{ Start-Process -FilePath $var -ArgumentList $arg } } Catch [Exception] { $mes="[CODE:99]実行時にエラーが発生しました。" [System.Windows.Forms.MessageBox]::Show($mes, "実行時エラー") } Finally { write-host "[" + $TABLS[$sender.Text] + "] 正常終了" } } Add-Type -AssemblyName System.Windows.Forms $form1 = New-Object System.Windows.Forms.Form $form1.Text="アプリランチャー" $form1.size=New-Object System.Drawing.Size(300,400) init $form1 $form1.ShowDialog() =====EXCEL編集編===== コピペですぐ使えるよう動作サンプル $excel = New-Object -ComObject Excel.Application $excel.Visible = $true $nm="Test1.xls" #新たにファイルを作るとき #$book = $excel.Workbooks.Add() #既存ファイルを読むとき $book = $excel.Workbooks.Open($nm) #読み込むシートを番号で指定 #$sheet = $excel.Worksheets.Item(1) #シート名で指定 $sheet = $excel.Worksheets.Item("ワークブック名") #値を取得したり設定したり .Text とか .Value があるようなので場合によっては試してみる $sheet.Cells.Item(1,1) = "PowerShellで作成" $text1 = $sheet.Cells.Item(1,1).Text #セル結合とかしてる時はこっちの指定のが使いやすいかも!? $sheet.Range("A2").Value()="HelloWorld" #新たにファイルを作ったとき #デフォルトではxlsx形式らしい。いろいろがんばればxls形式も行けるらしい(調査中) #$book.SaveAs($nm) #既存ファイルを上書き保存 ここは形式関係なくそのままのファイル形式で上書きされるようです $book.Save() #分かる人ならなんとなく意味が分かる #修了時の後始末 $excel.Quit() $excel = $null [GC]::Collect() ===== iTextsharp でPDFいろいろ ===== PDFに画像挿入できないかな、と思ってやってみた。端的に言えば、「はんこ」みたいなのをおせたらなと。 ===== D&DでFormのボタンとか動かしたりする ===== #初期化処理 ウインドウ作成 function init($form) { $script:isDraging=$false # ドラッグ中かどうか判定フラグ $script:deffPoint=$null # ドラッグ開始時の座標 #移動させるオブジェクト $script:p=New-Object System.Windows.Forms.Panel -Property @{ backcolor="blue" name="panel" } #移動用の枠 $script:skeleton=New-Object System.Windows.Forms.Panel -Property @{ borderstyle="FixedSingle" visible=$false name="skeleton" } #後々の拡張用 $script:FORMS=@{"panel"=$script:p; "skeleton"=$script:skeleton} $p.Add_MouseDown({Image_MouseDown $this $_}) $p.Add_MouseMove({Image_MouseMove $this $_}) $p.Add_MouseUp({Image_MouseUp $this $_}) $form.Controls.Add($script:skeleton) $form.Controls.Add($script:p) } function Image_MouseDown( $sender, $e ){ if($e.Button -ne "Left"){ return } $script:isDraging = $true ; $script:diffPoint = $e.Location $script:skeleton.Size =$sender.Size $script:skeleton.Location=$sender.Location $script:skeleton.visible =$true } function Image_MouseMove( $sender, $e ){ if($script:isDraging -eq $false){ return } # C#のコード丸パクリで$sender.X としてハマりました。$sender.location.Xとするといいようだ。 $x = $e.location.X + $sender.location.X - $script:diffPoint.X $y = $e.location.Y + $sender.location.Y - $script:diffPoint.Y #if($x -lt 0){ $x=0 } ; if($y -lt 0){ $y=0 } $script:skeleton.Location = New-Object System.Drawing.Point($x,$y) } function Image_MouseUp( $sender, $e ){ $script:isDraging = $false $script:deffPoint=$null $script:skeleton.visible=$false if($e.Button -ne "Left"){ return } $sender.Location = $script:skeleton.Location } #============= エントリーポイント ============= Add-Type -AssemblyName System.Windows.Forms $form1 = New-Object System.Windows.Forms.Form $form1.size=New-Object System.Drawing.Size(480,310) $form1.Text = "TESTツール" init $form1 $form1.ShowDialog()