[PowerShell PowerBoots] メインウィンドウの各種設定を行う

by HIRO 26. 5月 2009 09:25

今回はPowerBootsのWindowの設定についてです。

これまでの記事

で 

Boots {}

と記述すればWPFのWindowを表示できることを説明しました。

ただし、この記述だけでは外観や表示位置はデフォルトのままです。

そこでWindowプロパティを設定し外観や表示位置を変更してみましょう。

 


プロパティの記述位置

PowerBootsのWindowのプロパティ記述位置は {}の前または後のどちらでも構いません。

Boots プロパティ {}
Boots{} プロパティ 

Window表示位置

Windowの表示位置の設定は Left, Top, WindowStartupLocationなどのプロパティを使用します。

たとえばスクリーン中央に表示する場合は

Boots -WindowStartupLocation "CenterScreen" {} 

スクリーン左端と上端からの位置を指定する場合は

Boots -Left 100 -Top 200 {}

のように記述します

 


Windowの幅と高さの変更

幅はWidth、高さはHeightプロパティを使用します

Boots -Width 200 -Height 100 {}

Windowタイトルの変更

タイトルはTitleプロパティを使用します

Boots -Title "PowerShell from Japan" -Width 300 -Height 100 {}

 

このほかにも設定可能なプロパティは数多くあります。

Windowプロパティについては
http://msdn.microsoft.com/ja-jp/library/system.windows.window_properties(VS.85).aspx

を参照ください。

最新のブログ

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerBoots | PowerShell

[PowerShell PowerBoots] TextBlockを使用する

by HIRO 19. 5月 2009 22:06

今回は PowerBoots で TextBlock を使用する方法について説明します。

PowerBoots の概要とインストールについては [PowerBoots] PowerBootsをインストールするを参照ください。


PowerBoots PowerBootの基本使用方法

PowerBootsでWPFウィンドウを作成するには

Boots { }

のように記述します。

これはコントロールが配置されていない、Windowのみを表します。

Windowに配置するコントロールは Boots{ } の { ~ } の間に記述します。


TextBlockを使用する

TextBlockはWPFアプリケーションで文字を出力する基本的なコントロールです。

PowerBootsでTextBlockを使用するための、基本的な書き方は

Boots { TextBlock -Text ”表示する文字列”}

です。-Textパラメータの後ろに、表示する文字列を記述します。(赤字の部分を書き換えます)

こうして作成した PowerBootsアプリは下の図のようになります。

image

次に表示する文字列を改行して2行表示してみましょう。

PowerShellでは改行は'`nで表すことができるので

Boots { TextBlock -Text "Hello PowerBoots!!`nHIRO" }

image

のようになります。

次にフォントを変えてみましょう。

フォントファミリは-FontFamilyパラメータ、フォントサイズは-FontSizeパラメータを使用します。

Boots { TextBlock -Text "Hello PowerBoots!!`nHIRO" -FontFamily Impact -FontSize 30 }

image


その他のパラメータ

TextBlockでは先ほど紹介した以外にも多くのプロパティを使用することができます。

すべての使用方法は紹介できないので、使用可能なプロパティ一覧を下記に示します。

下記プロパティを使用する場合は、プロパティ名の前に-(ハイフン)を付加して、PowerShellコマンドレットのパラメータと同じ要領で使用してください。

かっこいいアプリができたときには、是非お知らせください!!

 

プロパティ定義
ActualHeight System.Double ActualHeight {get;}
ActualWidth System.Double ActualWidth {get;}
AllowDrop System.Boolean AllowDrop {get;set;}
Background System.Windows.Media.Brush Background {get;set;}
BaselineOffset System.Double BaselineOffset {get;set;}
BindingGroup System.Windows.Data.BindingGroup BindingGroup {get;set;}
BitmapEffect System.Windows.Media.Effects.BitmapEffect BitmapEffect {get;set;}
BitmapEffectInput System.Windows.Media.Effects.BitmapEffectInput BitmapEffectInput {get;set;}
BreakAfter System.Windows.LineBreakCondition BreakAfter {get;}
BreakBefore System.Windows.LineBreakCondition BreakBefore {get;}
Clip System.Windows.Media.Geometry Clip {get;set;}
ClipToBounds System.Boolean ClipToBounds {get;set;}
CommandBindings System.Windows.Input.CommandBindingCollection CommandBindings {get;}
ContentEnd System.Windows.Documents.TextPointer ContentEnd {get;}
ContentStart System.Windows.Documents.TextPointer ContentStart {get;}
ContextMenu System.Windows.Controls.ContextMenu ContextMenu {get;set;}
Cursor System.Windows.Input.Cursor Cursor {get;set;}
DataContext System.Object DataContext {get;set;}
DependencyObjectType System.Windows.DependencyObjectType DependencyObjectType {get;}
DesiredSize System.Windows.Size DesiredSize {get;}
Dispatcher System.Windows.Threading.Dispatcher Dispatcher {get;}
Effect System.Windows.Media.Effects.Effect Effect {get;set;}
FlowDirection System.Windows.FlowDirection FlowDirection {get;set;}
Focusable System.Boolean Focusable {get;set;}
FocusVisualStyle System.Windows.Style FocusVisualStyle {get;set;}
FontFamily System.Windows.Media.FontFamily FontFamily {get;set;}
FontSize System.Double FontSize {get;set;}
FontStretch System.Windows.FontStretch FontStretch {get;set;}
FontStyle System.Windows.FontStyle FontStyle {get;set;}
FontWeight System.Windows.FontWeight FontWeight {get;set;}
ForceCursor System.Boolean ForceCursor {get;set;}
Foreground System.Windows.Media.Brush Foreground {get;set;}
HasAnimatedProperties System.Boolean HasAnimatedProperties {get;}
Height System.Double Height {get;set;}
HorizontalAlignment System.Windows.HorizontalAlignment HorizontalAlignment {get;set;}
Inlines System.Windows.Documents.InlineCollection Inlines {get;}
InputBindings System.Windows.Input.InputBindingCollection InputBindings {get;}
InputScope System.Windows.Input.InputScope InputScope {get;set;}
IsArrangeValid System.Boolean IsArrangeValid {get;}
IsEnabled System.Boolean IsEnabled {get;set;}
IsFocused System.Boolean IsFocused {get;}
IsHitTestVisible System.Boolean IsHitTestVisible {get;set;}
IsHyphenationEnabled System.Boolean IsHyphenationEnabled {get;set;}
IsInitialized System.Boolean IsInitialized {get;}
IsInputMethodEnabled System.Boolean IsInputMethodEnabled {get;}
IsKeyboardFocused System.Boolean IsKeyboardFocused {get;}
IsKeyboardFocusWithin System.Boolean IsKeyboardFocusWithin {get;}
IsLoaded System.Boolean IsLoaded {get;}
IsMeasureValid System.Boolean IsMeasureValid {get;}
IsMouseCaptured System.Boolean IsMouseCaptured {get;}
IsMouseCaptureWithin System.Boolean IsMouseCaptureWithin {get;}
IsMouseDirectlyOver System.Boolean IsMouseDirectlyOver {get;}
IsMouseOver System.Boolean IsMouseOver {get;}
IsSealed System.Boolean IsSealed {get;}
IsStylusCaptured System.Boolean IsStylusCaptured {get;}
IsStylusCaptureWithin System.Boolean IsStylusCaptureWithin {get;}
IsStylusDirectlyOver System.Boolean IsStylusDirectlyOver {get;}
IsStylusOver System.Boolean IsStylusOver {get;}
IsVisible System.Boolean IsVisible {get;}
Language System.Windows.Markup.XmlLanguage Language {get;set;}
LayoutTransform System.Windows.Media.Transform LayoutTransform {get;set;}
LineHeight System.Double LineHeight {get;set;}
LineStackingStrategy System.Windows.LineStackingStrategy LineStackingStrategy {get;set;}
Margin System.Windows.Thickness Margin {get;set;}
MaxHeight System.Double MaxHeight {get;set;}
MaxWidth System.Double MaxWidth {get;set;}
MinHeight System.Double MinHeight {get;set;}
MinWidth System.Double MinWidth {get;set;}
Name System.String Name {get;set;}
Opacity System.Double Opacity {get;set;}
OpacityMask System.Windows.Media.Brush OpacityMask {get;set;}
OverridesDefaultStyle System.Boolean OverridesDefaultStyle {get;set;}
Padding System.Windows.Thickness Padding {get;set;}
Parent System.Windows.DependencyObject Parent {get;}
PersistId System.Int32 PersistId {get;}
RenderSize System.Windows.Size RenderSize {get;set;}
RenderTransform System.Windows.Media.Transform RenderTransform {get;set;}
RenderTransformOrigin System.Windows.Point RenderTransformOrigin {get;set;}
Resources System.Windows.ResourceDictionary Resources {get;set;}
SnapsToDevicePixels System.Boolean SnapsToDevicePixels {get;set;}
Style System.Windows.Style Style {get;set;}
Tag System.Object Tag {get;set;}
TemplatedParent System.Windows.DependencyObject TemplatedParent {get;}
Text System.String Text {get;set;}
TextAlignment System.Windows.TextAlignment TextAlignment {get;set;}
TextDecorations System.Windows.TextDecorationCollection TextDecorations {get;set;}
TextEffects System.Windows.Media.TextEffectCollection TextEffects {get;set;}
TextTrimming System.Windows.TextTrimming TextTrimming {get;set;}
TextWrapping System.Windows.TextWrapping TextWrapping {get;set;}
ToolTip System.Object ToolTip {get;set;}
Triggers System.Windows.TriggerCollection Triggers {get;}
Typography System.Windows.Documents.Typography Typography {get;}
Uid System.String Uid {get;set;}
VerticalAlignment System.Windows.VerticalAlignment VerticalAlignment {get;set;}
Visibility System.Windows.Visibility Visibility {get;set;}
Width System.Double Width {get;set;}

参考文献:TextBlock クラス (System.Windows.Controls)
http://msdn.microsoft.com/ja-jp/library/system.windows.controls.textblock(VS.80).aspx

現在のレート 1.0 (1人)

  • Currently 1/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerBoots | PowerShell

ながーい文字列を作ってテキストファイルに落とす方法

by 牟田口大介 14. 5月 2009 23:12

お久しぶりです。
最近ようやくPowerShell v2を触り始めました。(えー)
あのISEがよくできてていいですな。

 さて、今回もちょっとしたTipsを。というか試行錯誤を。v1の話です。

1GBのASCII文字列(なんでもいいんですが)をテキストファイルに書き込みたい。

まずためしたこと:

PS C:\Users\daisuke> Set-Content test.txt ("a"*1GB)
'*' 演算子が失敗しました:種類 'System.OutOfMemoryException' の例外がスローされました。
発生場所 行:1 文字:27+ Set-Content test.txt ("a"*1 <<<< GB)

いきなり1GBの文字列を発生させると、メモリが足りないって怒られました。環境にもよるんでしょうけどこれは正道ではなさそうです。

次に試したこと:

PS C:\Users\daisuke> 1..1GB|%{Add-Content test.txt "a"}
範囲の式が正しくありません。1073741823 は、範囲の最大サイズ (=50000 要素) を超えています。
発生場所 行:1 文字:4+ 1..1 <<<< GB|%{Add-Content test.txt "a"}

ループは5万回までしか回せないらしいです。むむむ。じゃあループ回数を減らそう。

PS C:\Users\daisuke> 1..1KB | %{Add-Content test.txt ("a"*1MB)}

うまく行きました!1GBのテキストファイルができあがりました。やった。

1MBも一回のループで書き込むのがいいのか分からないし、厳密にはちゃんとループ回数も考えてあげたほうがいいんだろうけど、1GB=1KB*1MBなのを利用してしまいました。

ただ、インデックスサーチ?かなんかが割り込みかけるみたいで、たまに

Add-Content : 別のプロセスで使用されているため、プロセスはファイル 'C:\Users\da
isuke\test.txt' にアクセスできません。
発生場所 行:1 文字:23
+ 1..1KB | %{Add-Content  <<<< test.txt ("a"*1MB)}

とか例外吐きます。ちゃんと排他制御も考えるとこれじゃダメっぽいです。ファイルにロックとかかけられないのかなー?

最新のブログ

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerShell ISE | Tips

[PowerBoots] PowerBootsをインストールする

by HIRO 12. 5月 2009 23:34

PowerBootsとは、PowerShell で WPFアプリを開発するための、PowerShell Moduleです。

以前から PowerBootsに興味があったのですが、やっとインストールをしたので覚え書きとして残しておきます。

(エバンジェリストの田辺さんのところで取り上げられており、試してみたくなりました)

PowerBootsのインストールは

OS:Windows Vista 32bit Ultimate

PowerShell V2 CTP3インストール済み

の環境で行いました。

WPFアプリなので.NET Framework3.0以上が必要です。

まずは PowerBootsをダウンロードします

http://powerboots.codeplex.comからDownloadのリンクを探し、ダウンロードを行うのですが、

今回、PowerBoots.zip というファイルをダウンロードしました。

image

 

ダウンロードが完了したら、解凍しましょう。

解答すると、PowerBootsというフォルダができ、その中にいろいろなファイルがあります。

で、このフォルダをモジュール置き場に配置します。

モジュール置き場はPowerShellを起動して、$Env:PSMODULEPATH で確認できます。

以下、私の環境での実行結果です。2つのパスがセミコロン(;)で連結されて表示されています。

どちらかのパスに、先ほど解凍したPowerBootsのフォルダを配置します。

(パスが存在しない場合には作成してください)

PS> $env:PSMODULEPATH
C:\Users\HIRO\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules

 

次に、Get-ExecutionPolicyコマンドレットで、現在の実行ポリシーを確認しておきましょう。

PS> Get-ExecutionPoliscyRestricted

 

PowerBootsを使用するには、実行ポリシーをrestricted以外にする必要があります。

実行ポリシーを設定するにはSet-Executionコマンドレットを使用するのですが、このコマンドレットはPowerShellを「管理者として実行」で起動する必要があります。

とりあえずRemoteSignedに設定します。設定済みの方は次へ進んでください。

PS> Set-ExecutionPoliscy RemoteSigned

ここまでで、ほぼ設定は完了です。

次にPowerBootsを使用するわけですが、PowerShell を STAモードで起動しなければいけないようです。

STAモードで起動するには、「ファイル名を指定して実行」で “PowerShell –STA” と入力して起動するか

image

PowerShellからPowerShellを起動してください。

image

次に、PowerBootsを保存していたディレクトリに移動し、Import-Module -Name PowerBoots を実行します。

PS> cd C:\Users\HIRO\Documents\WindowsPowerShell\Modules\PowerBoots
PS>C:\Users\HIRO\Documents\WindowsPowerShell\Modules\PowerBoots> Import-Module -Name PowerBoots

このコマンドはプロファイルに記述しておくとよいかもしれませんね。

ということで、ここまででPowerBootsの設定が完了です。

せっかくなので田辺さんのところで紹介されているサンプル"HelloWorld"を試してみます。

PS> Boots { TextBlock "ハロー、ワールド。" }

image

次回から、様々な使用方法についてお伝えしていきたいと思います。

最新のブログ

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

PowerBoots | PowerShell

[PowerShell] : SQL Server 2008 でバックアップを取る方法(Agent版あり)

by けろ-みお 11. 5月 2009 02:18

 

実は、SQL Server 2008 のバックアップもPowerShellで簡単に実行することができます。

こんな、バックアップスクリプトをSQLで作っておき、それをInvoke-SqlCmd で、ファイル名を指定して実行するだけ。

SQLスクリプト

DECLARE @backupSetId as int 
 
DECLARE @now as nvarchar(10)
DECLARE @backupfile nvarchar(max)
SELECT 	@now = CONVERT(varchar, getdate(), 112), 
@backupfile = N'D:\backup\SampleDb_' + @now + '.bak'
BACKUP DATABASE [SampleDb] TO  DISK = @backupfile 
WITH NOFORMAT, 
INIT,  
NAME = N'SampleDb-完全 データベース バックアップ', 
SKIP, 
NOREWIND, 
NOUNLOAD,  
STATS = 10
SELECT 	@backupSetId = position 
FROM 
msdb..backupset 
WHERE 
database_name = N'SampleDb' 
AND 	backup_set_id = (select max(backup_set_id) from msdb..backupset where database_name=N'SampleDb' )
IF @backupSetId is null 
BEGIN 
raiserror(N'確認に失敗しました。データベース ''SampleDb'' のバックアップ情報が見つかりません。', 16, 1) 
END
RESTORE VERIFYONLY FROM  DISK = @backupfile WITH  FILE = @backupSetId, NOUNLOAD,  NOREWIND
 
 

 

上記バックアップスクリプト実行するPowerShellのコマンドライン

 # Invoke-SqlCmd -InputFile 'バックアップ用SQLスクリプト'
Invoke-SqlCmd -InputFile 'D:\backup\SampleDb_Backup.sql'

たった、これだけなんですが、1つだけ注意点!

それは、Invoke-SqlCmd コマンドをSQL Server Agent で動かす時、このままだとエラーになります。

ちなみに、SQL Server Agent で、PowerShellを動かしたい場合は、下記で行けます。

 

手順1:Management Studio で、[管理]→[SQL Server Agent] を選択する

手順2:新規ジョブを作成し、下記のように設定する

 

手順3:ジョブを作成する際、[種類]にPowerShellを選択し、コマンド欄には、下記のように入力します。(SQL Server 認証の例)

  
# ユーザIDとパスワードを指定する
#(コマンドライン実行と違い、SQL Server Agentは接続情報をもっていないため)
Invoke-SqlCmd -InputFile 'D:\backup\SampleDb_Backup.sql' 
-ServerInstance "サーバ名\SQL Serverのインスタンス名" -U ユーザID -P パスワード 

 

手順4:後は、ジョブに対するエラー発生時の対処方法などを設定すればOKです。

手順5:作成したジョブを右クリックし、[ステップでジョブを開始] します。

     成功メッセージが返ってきて、所定のフォルダにDBバックアップファイルが無事格納されていればOKです。

 

ただ、もう1つわからないことがあります。(T-SQL を私が忘れているだけかもしれませんが)

それは、Invoke-SqlCmd コマンドって、SQL Server でいう、EXEC(正式名はEXECUTE) ですよね?

ストアド実行時に関しては戻り値がとれるんで、OKなんですが、SQLスクリプトファイルで実行したものって

戻り値とれないんでしたっけ?

もし、それができれば、Invoke-SqlCmd でスクリプトファイルをSQL Server Agent で実行しても、

きちんとした戻り値処理ができるんですけどね・・・ うーん、知っている方、教えてください。

現在のレート 3.0 (1人)

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL Server

Calendar

<<  3月 2010  >>
月曜火曜水曜木曜金曜土曜日曜
22232425262728
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 PowerShell from Japan!!