-whatif on powershell
-whatif
powershell のコモンパラメータの一つ、-whatif は、いわゆるドライランのオプション。Remove-itemやCopy-Itemなどで実行せずに、実行確認をする。
さて、実装するには?
-whatif実装
肝は、
if( $PSCmdlet.ShouldProcess( $target ) ){ -whatif時に実行しないコマンド }
こうすると、-whatif時に、
$targetに対し、xxx
の様なメッセージが表示される。-confirmでNoが選択された場合も同様。
else節に、実行しない時の処理を書けば詳細を表示できる?それとも別の動き?
-whatifを伝播させる
伝達させるには各関数に下記を記載する。
[CmdletBinding(SupportsShouldProcess=$True)]
伝播経路全てに上記を書くのはどうなの?スクリプトの場合は、スクリプトレベルにも必要?
参考
msdn/ShouldProcess
https://msdn.microsoft.com/en-us/library/ms568267(v=vs.85).aspx
http://stackoverflow.com/questions/11211518/how-to-use-dry-run-in-powershell
https://stuncloud.wordpress.com/2015/12/15/powershell_shouldprocess/
WebClient
WebClient by PowerShell
Webclient classを使ってダウンロード
function download ( [String]$dir = "c:\wk\poi", [uri]$uri = "https://www.google.com/index.html") { $file = Split-Path $uri.AbsolutePath -Leaf $bs = New-Object System.Net.WebClient $ret = $bs.DownloadFile( $uri, (Join-Path $dir $file)) return $ret }
Basic認証1
その一
function upload($user,$pw, $file, $uri){ $cred = [system.Net]::NetworkCredential($user,$pw) $bs = New-Object system.Net.WebClient $bs.Credentials = $cred $site= $bs.DownloadFile($uri) return $site }
Basic認証2
認証1でエラー401になる場合
function upload($user,$pw, $file, $uri){ $bs = New-Object system.Net.WebClient $upw = "{0}:{1}" -f $user, $pw $bytes = [System.Text.Encoding]::ASCII.GetBytes($upw) $b64 = [System.Convert]::ToBase64String($bytes) $auth = [system.Net.HttpRequestHeader]::Authorization $bs.Headers[$auth] = "Basic " + $b64 $bs.UploadFile($uri, $file) }
参考
http://qiita.com/muro/items/8b9f5886c56f7b6afd3c
http://qiita.com/ki02/items/575fc79c0acd3811902e
download
http://www.atmarkit.co.jp/fwin2k/win2ktips/995dlfile/dlfile.html
madn webclient
https://msdn.microsoft.com/ja-jp/library/system.net.webclient(v=vs.110).aspx
msdn httpRequestHeader
https://msdn.microsoft.com/ja-jp/library/system.net.httprequestheader(v=vs.110).aspx
WPF ComboBox
WPF ComboBox
方法としては二つ。
- XAML内にデータを書く。
- コード側のクラスをバインドする。
XAML版
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Combo" Height="10" Width="100"> <StackPanel> <ComboBox SelectedIndex="0"> <ComboBoxItem>その一</ComboBoxItem> <ComboBoxItem>その二</ComboBoxItem> <ComboBoxItem>その三</ComboBoxItem> </ComboBox> </StackPanel> </Window>
参考
http://www.infilic.co.jp/tech/?p=542
https://code.msdn.microsoft.com/windowsdesktop/XAMLVBC-ComboBox-WPF-9431ffc3
図解
配列性能 on PowerShell
配列性能 on PowerShell
PowerShell配列に += で要素を追加する性能が良くないらしい。固定長配列として実装されており、追加時に配列を作り直しているのが原因とのこと。
性能面ではArrayList or List
$x = New-Object 'System.Collections.Generic.List[System.String]'
$x.add( "poi" )
ArrayListの場合、
$l = New-Object System.Collections.ArrayList
$l.add( "poi" )
参考
性能測定
http://tech.guitarrapc.com/entry/2013/09/22/100203
各種罠、自動変換など
http://tech.guitarrapc.com/entry/2015/09/05/012733
配列の使い方
http://capm-network.com/?tag=PowerShell変数
http://qiita.com/jca02266/items/a0c71ae90d055ab3893c
List
https://msdn.microsoft.com/ja-jp/library/6sh2ey19(v=vs.110).aspx
以上