qshinoの日記

Powershell関係と徒然なこと

CTRL-C 処理 by PowerShell

CTRL-C by PowerShell

PowerShellでCTRL-Cを処理する方法

  • Polling
  • finally

finally

CTRL-Cや、exit() 実行時でもfinallyが実行される。

polling

CTRL-Cをキー入力処理するフラグをセットし、以後はCTRL-Cをキー入力としてポーリング。

フラグセット

キーのポーリング

if($Host.UI.RawUI.KeyAvailable -and (3 -eq [int] $Host.UI.RawUI.ReadKey(“AllowCtrlC,IncludeKeyUp,NoEcho”).Character))

参考

http://stackoverflow.com/questions/1710698/gracefully-stopping-in-powershell

http://poshcode.org/542

Start-Process by PowerShell

Start-Process使用法

全体フロー

  1. $proc Start-process -passThru
  2. $ejob = Register-ObjectEvent -action $action -MessageData $job
  3. action: unregister-event
  4. remove-job $ejob

PowerShellからの外部コンソールアプリを非同期起動する際にStart-Processを使う方法。

まずは、パラメータから、

Start-Process起動パラメータ

Start-Process [-FilePath] <String> [[-ArgumentList] <String[]>] [-Credential <PSCredential>] [-WorkingDirectory <String>] [-LoadUserProfile] [-NoNewWindow] [-PassThru] [-RedirectStandardError <String>] [-RedirectStandardInput <String>] [-RedirectStandardOutput <String>] [-WindowStyle <ProcessWindowStyle>] [-Wait] [-UseNewEnvironment] [<CommonParameters>]

たくさんありすぎて迷うが、基本は下記。

$proc = Start-Process 起動プログラム(フルパス推奨) -ArgumentList $ag -WorkingDirectory $wd -PassThru

-PassThruがあると、Process型を返すが、ないと何も返さない。次のイベントハンドラ登録に戻り値$procを使うので忘れずに。

$ag は配列を渡せるが、配列を結合した文字列としてプロセスに渡しているので、文字列として渡すと変換処理に悩まされない。

-NoNewWindowを渡していないのでコンソールウインドウが表示される。

標準入出力を指定していないので、コンソールに出力される。

非同期なので終了イベント登録

非同期起動のため制御は直ぐに戻る。以後は起動側をメイン、起動されたアプリをサブ、イベント処理をハンドラと呼ぶ。

$exited = Register-ObjectEvent $proc -EventName -MessageData $data -action $action

イベント登録によりハンドラ$exitedがジョブとして登録される。登録されたハンドラは下記にて確認できる。

Get-EventSubscriber

ハンドラ$actionでは下記5個の自動変数が使える。

  1. $Sender
  2. $Event
  3. $EventArgs
  4. $Args
  5. $EventSubscriber

$EventはPSEventArgs型、$Senderはイベント発行元=$Event.Sender, $EventArgsは元イベントの引数=$Event.SourceEventArgs, $Argsはハンドラへの引数=$Event.SourceArgs=($Sender,$EventArgs), $EventSubscriberはイベントハンドラ。ユニークなのは、$Eventと$EventSubscriberの二つ。後は利便性のための写し?

イベント登録時の$dataは、$Event.MessageDataでアクセスできる。

整理すると、使えるのは下記の4点。

  • $Sender
  • $EventArgs ; イベントが定義
  • $EventSubscriber
  • $Event.MessageData ; ハンドラ登録時定義

$EventはPSEventArgsオブジェクト。下記がプロパティ一覧。

PSEventArgs

  • ComputerName
  • EventIdentifier
  • RunSpaceId
  • SourceIdentifier
  • Sender
  • SourceArgs
  • SourceEventArgs
  • MessageData
  • TimeGenerated

イベントハンドラの$actionではイベントを削除する。

$action = {
  Unregister-Event $EventSubscriber.name
  [Diagnostics.Process]$proc = $Sender
  $ec    = $proc.ExitCode
  $data = $Event.MessageData
  Write-Host "Exit Code: $ec : $data"
}

イベントジョブの廃棄

処理完了or中断時に、$procとイベントジョブ$exitedをメインで廃棄する。ただし、ハンドラが呼ばれなかった場合(サブがストールした時など)を顧慮し、残っていればハンドラも削除する。

function dispose_procs( $proc, $exited){
  if ( $exited -and ($exited -is [system.management.automation.PSEventJoB])){
   Get-EventSubscriber| Where-Object { $_.SourceIdentifier -eq $exited.name } | Unregister-Event -force
    Remove-Job $exited
  }
  if( $proc -and ($proc -is [Diagnostics.Process]])){
    if( -not $proc.hasExited ){
      $proc.kill()
    }
    $proc.dispose()
  }
}

dispose_procs $proc $exited
Remove-Variable proc
Remove-Variable exited

参考

https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.management/start-process

Windows ネットワークの場所

Windowsネットワークの場所変更

Windowsネットワークの場所には、

  • パブリック public
  • プライベート private
  • ドメイン domain

の3種類があり、Windowsファイアーウォールの設定が異なる。

プライベートへの変更方法

接続確認

Get-NetConnectionProfile

変更

Set-NetConnectionProfile -InterfaceIndex XX -NetworkCategory private

XX には、getで得たInterfaceIndexを使用する。

課題

接続済みのネットワークしか、profileがないので、ネットワークが落ちている場合には使えない。

使用メモリ追加 by Powershell

使用メモリ追加

クオータが不足していますメッセージが出るようになったため、使用メモリを追加する。

winrm

winrmを使用するので設定。管理者権限が必要なので、管理者になってから。

Start-Process powershell -verb runas
winrm quickconfig

現設定を確認

Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
Get-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell

変更

変更にはアドミン権限が必要。ここでは2GBに変更。

Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 2048
Set-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell 2048
Restart-Service WinRM

変更確認

Get-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB
Get-Item WSMan:localhost\Plugin\microsoft.powershell\Quotas\MaxConcurrentCommandsPerShell

参考

http://tech.guitarrapc.com/entry/2013/08/02/000842

WPF簡単文書表示/HTML

WPF簡単文書表示HTML編

XAML

早速XAML

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="HTMLVIEW" 
  Height="100" 
  Width="200">
  <StackPanel>
    <WebBrowser 
      Source="file:///c:/tmp/a.html" 
      Name="bs1" />
  </StackPanel>
</Window>

コード

上記Xamlを読んでShowDialog()

注意

WebBrowserのSourceプロパティは、文字列型ではなく、URI型なので注意。

$winがWindowとすると、

$bs = $win.FindName("HTMLVIEW")
if($bs){$bs.Source=[uri]"file:///c:/z.html"}

以上。

EtherIP + pppoeサーバー

EtherIP + pppoeサーバー

装置

  • 端点1: ルータrt
  • 端点2: sv
  • 中間点: gw

ソフト/sv上

  • EtherIPドライバ
  • pppoeサーバー

設定/gw

接続

(rt local-nw)rt - gw - sv

rt-local側を追加

DUT -(eth) - gw2 -(eip2)-rt -(eip) gw -(DNAT:eip) - sv - eipドライバ - pppoeサーバー - sv内ルーティング

sv内構成

  • EtherIPドライバ
  • pppoeサーバー
  • 内部lan用デバイス

In eip packet

  • eipドライバ
  • EtherPacket
  • pppoesv
  • ipパケット
  • sv内ルーティング

out sv内ルーティング

  • ターゲットネットワーク行をppp0へ

第一段階

まずはEIPの開通を目指す。

rtからsvのeipドライバへの通信。

rt側はsvに向けてeipパケットを発行。 svはeipドライバを使う。

eipドライバ

今の所下記が候補。古いので難度が高そう。

https://github.com/kjmkznr/etherip

第二段階

eipが開通したら、次はpppoeサーバー

pppoeサーバーは下記が候補

rp-pppoe

こちらはyumモジュールもありそうなので、期待している。

第三段階

検証

検証機能

  1. ping導通
  2. ppoe-client

検証装置

  1. rt上のpppoe-client
  2. rtの反対側eip端点のpppoe-client
  3. 更に奥のpppoe-client こいつの稼働が本来の目的

Plan B

etheripドライバが当たらない場合、Plan BとしてSoftetherSVでトライ。但し、その場合はEtherIP/IPsecとなり、少し面倒。

rt →(eip/ipsec)→ gw/SESV →(eth)→pppoesv

gwのfirewall設定をipsec用に。

FW ip esp, udp 500, - NAT-Traversal - ike ? - ipsec ?

SESVがWindowsの場合

  1. Install SESV
  2. Open port for FW udp500, esp
  3. Connect from rt
  4. Install pppoesv
  5. Connect Bridge to pppoesv-if/eth1?
  6. Check connection rt-pppoe

SESV to rt

EtherIP/IPsecにて接続。SESV側の手順。

VPNServerManagerのサーバー管理ウインドウから、IPSec/L2TP設定→IPsec/L2TP/EtherIP設定を選択。後はマニュアルを見つつホゲホゲ。

http://ja.softether.org/4-docs/2-howto/Other_VPN_Appliance_Setup_Guide/7

LocalBridge

pppoesvのethXと、SESVの仮想HUBをローカルブリッジ接続する。

http://ja.softether.org/4-docs/1-manual/3/3.6

ppoe-client→rt → gw/sesv -bridge-pppoesv → 戻り

pppoesv

  • eth0 : main path
  • eth1 : bridge from sesv
  • ppp0 : ppp端点

WPF visibility

WPF visibility

WPF UIElementが持つVisibilityプロパティ。

要素名:下記の三種類

  • Visible
  • Hidden
  • Collapsed

参考

Visibilityプロパティを変更する

https://msdn.microsoft.com/ja-jp/library/ms748821(v=vs.80).aspx

Visibility列挙体

https://msdn.microsoft.com/ja-jp/library/system.windows.visibility(v=vs.80).aspx

Visibilityプロパティ

https://msdn.microsoft.com/ja-jp/library/system.windows.uielement.visibility(v=vs.80).aspx