全3件 (3件中 1-3件目)
1
powershellにget-wmiobjectなんて便利なコマンド(レット)があるなんて知らなかった。Comオブジェクトを操作してえらく面倒な手続きを経て使っていたんだけど、get-wmiobjectならえらく簡単。時間も早い。以下自分のIPを調べるコード。行数も圧倒的に短く、速い。 function getMyIp(){ $locator =new-object -ComObject "WbemScripting.SWbemLocator" $service =$locator.ConnectServer( "." ) $adaptors =$service.ExecQuery( "SELECT * FROM Win32_NetworkAdapterConfiguration" ) for ( $i = 0; $i -lt $adaptors.count; $i++ ){ $properties =$adaptors.itemindex( $i ).properties_ foreach( $p in $properties ){ if ( ( $p.name -eq "IPEnabled" ) -and ( $p.value ) ) { $itemindex =$i break } } } if ( !$itemindex ){ return "none" } $properties =$adaptors.itemindex( $itemindex ).properties_ foreach( $p in $properties ){ if ( $p.name -eq "IPAddress" ) { $p.value[ 0 ] } }}function getMyIp(){ $query =get-wmiobject -query "SELECT * FROM Win32_NetworkAdapterConfiguration" $query|%{ if ( $_.IpEnabled ){ $_.IpAddress[ 0 ] } }}getMyIp
2015.12.18
コメント(1)
ここらへんを参考にクリップボード上の画像データをファイル化するってのをやってみた。うまくいったと喜んでたんだけど、クリップボードに入っているデータがブラウザなどでコピーしたものではなく、ペイントソフトなどで切り貼りしたものだと、なぜか生成画像が真っ黒けになってしまう。丸一日悩んでたんだけど、ようやく解決法を発見しました。 それが、おんなじデータを $pic =[System.Windows.Clipboard]::GetImage(); #読んで [System.Windows.Clipboard]::SetImage($pic); #書いて $pic =[System.Windows.Clipboard]::GetImage(); #また読んで というイミフな冗長手順。なぜこれでうまくいくのかはサッパリ。OSや.Netのバージョンが上がると解消されているかもしれないけど。 以下うまくいったコード #set-ExecutionPolicy Bypass -scope process[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.ClipBoard") | out-null[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Media") | out-nullfunction ClippedImageToFile( $filepath, $wait=0 ){ $start =date while ( ![System.Windows.Clipboard]::ContainsImage() ){ $seconds =( date ).Subtract( $start ).TotalSeconds if ( ( $wait -ne -1 ) -and ( $seconds -gt $wait )){ "No Image" return "" } } $ext =( split-path $filepath -leaf ) -replace "^.*[.]","" $pic =[System.Windows.Clipboard]::GetImage(); switch ( $ext ){ "bmp" { $enc=New-Object System.Windows.Media.Imaging.BmpBitmapEncoder;break } "tif" { $enc=New-Object System.Windows.Media.Imaging.TiffBitmapEncoder;break } "tiff" { $enc=New-Object System.Windows.Media.Imaging.TiffBitmapEncoder;break } "png" { $enc=New-Object System.Windows.Media.Imaging.PngBitmapEncoder;break } "gif" { $enc=New-Object System.Windows.Media.Imaging.GifBitmapEncoder;break } default { $enc=New-Object System.Windows.Media.Imaging.JpegBitmapEncoder;break } } [System.Windows.Clipboard]::SetImage($pic); $pic =[System.Windows.Clipboard]::GetImage(); $enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($pic)); $fs =New-Object system.IO.FileStream -ArgumentList "$filepath",'Create','Write' $enc.Save( $fs ); $fs.Close(); return}
2015.12.08
コメント(0)
ウインドウの出し方など、できた範囲でメモ。#set-ExecutionPolicy Bypass -scope process[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | out-null[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") | out-nullfunction setPosition( $Obj, $Direction, $Margin=@( 5; 5 ) ){ #呼ぶ前に親を設定しておくこと $BaseSize =$Obj.Parent.ClientRectangle.Width, $Obj.Parent.ClientRectangle.Height $BaseHalf =[int]( $BaseSize[ 0 ] / 2 ), [int]( $BaseSize[ 1 ] / 2 ) $SelfSize =( $Obj.Width, $Obj.Height ) $SelfHalf =[int]( $SelfSize[ 0 ] / 2 ), [int]( $SelfSize[ 1 ] / 2 ) switch ( $Direction ){ "northwest" { $Location=( 0 + $Margin[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "nw" { $Location=( 0 + $Margin[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "north" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "n" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "northeast" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "ne" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( 0 + $Margin[ 1 ] ); break } "west" { $Location=( 0 + $Margin[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "w" { $Location=( 0 + $Margin[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "center" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "c" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "east" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "e" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( $BaseHalf[ 1 ] - $SelfHalf[ 1 ] ); break } "southwest" { $Location=( 0 + $Margin[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } "sw" { $Location=( 0 + $Margin[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } "south" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } "s" { $Location=( $BaseHalf[ 0 ] - $SelfHalf[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } "southeast" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } "se" { $Location=( $BaseSize[ 0 ] - $SelfSize[ 0 ] - $Margin[ 0 ] ), ( $BaseSize[ 1 ] - $SelfSize[ 1 ] - $Margin[ 1 ] ); break } default { "位置文字列の誤り";exit } } $Obj.Location =New-Object Drawing.Point( $Location ) }$form1 =new-object Windows.Forms.form$form1.text ="ベースウインドウ"#$form1.AutoSize$form1.Size =new-object Drawing.Size( 720, 540 )$form1.BackColor="Azure"$label =new-object Windows.Forms.label$label.text ="ここにテキストを打ち込む"$label.AutoSize =$true$form1.Controls.Add( $label )$button =new-object Windows.Forms.button$button.text ="終了"$button.Add_Click( { close $form1 } )$button.AutoSize=$true$form1.Controls.Add( $button )$form2 =new-object Windows.Forms.form$form2.text ="子ウインドウ"$form2.AutoSize$form2.BackColor="white"$form2.TopLevel =$false$form2.Visible =$true$form1.Controls.Add( $form2 )function placeCildren(){ setPosition $label "nw" setPosition $button "se" setPosition $form2 "center"}placeCildren$form1.Add_Resize( { placeCildren } )$form1.showDialog()
2015.12.03
コメント(0)
全3件 (3件中 1-3件目)
1


![]()