New-Object を使うのは、インスタンス作成の為です。コンストラクタに引き渡す為に使用します。その為に、-ArgumentList を使用しますが、[IO.FileMode]::Open 等の定数を使用する場合に直接指定するとエラーになったので、一旦変数にセットして使用しています。 定数の値が知りたい場合は、[IO.FileMode]::Open.value__ で表示されました。そして、[IO.FileMode]::Open という使用方法は、静的なクラスとメソッドの使用方法です。 冒頭の、HttpWebRequest インスタンスの作成でも使用しています。
# ****************************
# HttpWebRequest インスタンス
# ****************************
$myReq = [Net.WebRequest]::Create("http://yourdomain/put/put.php")
$myReq = [Net.HttpWebRequest]$myReq
# ****************************
# FileStream インスタンス
# ****************************
$FileMode = [IO.FileMode]::Open
$FileAccess = [IO.FileAccess]::Read
$fs = New-Object IO.FileStream -ArgumentList "C:\\user\\winofsql.png",$FileMode,$FileAccess
$length = $fs.Length
# ****************************
# バイト配列の作成
# ****************************
[byte[]]$data = New-Object byte[] $length
# ****************************
# 読み込み
# ****************************
$fs.Read($data, 0, $length)
$fs.Close()
# ****************************
# 送信準備
# ****************************
$myReq.Method = "POST"
$myReq.ContentLength = $length
# ****************************
# データを書き込む
# ****************************
$reqStream = $myReq.GetRequestStream()
$reqStream.Write($data, 0, $length)
$reqStream.Close()
# ****************************
# 結果を取得
# ****************************
$myRes = $myReq.GetResponse()
$resStream = $myRes.GetResponseStream()
$sr = New-Object IO.StreamReader -ArgumentList $resStream, [Encoding]::UTF8
$sr.ReadToEnd() # この行で表示
$sr.Close()
関連する記事
|
|
【PowerShellの最新記事】
- PowerShell のバージョンを確認して、最新の PowerShell(pwsh)を実行する方法
- PowerShell で System.Data.Odbc を使用して MySQL のデータを一覧表示( csv )
- PowerShell : COM 経由(New-Object) + MySQL Connector/ODBC でループ処理をしながら更新
- PowerShell でエクスプローラでコピーしたファイルリストを取得して、テキストのクリップボードに再度コピーしなおす
- PowerShell : ファイルを開くダイアログを使うのに System.Windows.Forms を参照する二つの方法
- PowerShell2.0 : PowerShell のコードだけで TKMP.dll を使用してメールを送信する
- PowerShell2.0 : PowerShell 内で VBのコードを記述(TKMP.dllを使用)して、exe を作成した後実行してメールを送信する
- PowerShell で PNG 画像にフォントを指定してテキストを書き込む
- PowerShell で、SQLServer2012 の SMO を使用してテーブルの create 用のスクリプトを出力する
- PowerShell より COM オブジェクトを使用して、簡単にバイナリファイルをアップロードする
- PowerShell2.0 : here-string と呼ばれるヒアドキュメント( here-string )の構文
- PowerShell2.0 : 初めての PowerShell / ファイルのダウンロード (.NET Framework の利用)
- PowerShell2.0 : ファイルを開くダイアログを使う
- PowerShell2.0 : イベントの処理とタイマーと、関数と変数のスコープ
- PowerShell(スクリプト)の引数を格納する配列変数 $args の扱い
- PowerShell2.0 : Shell.Application でディレクトリ内のファイルとディレクトリの個数( .NET の文字列フォーマット )
- PowerShell2.0 : PowerShell ISE で Excel のオブジェクトを利用して完全に終了(メモリから解放)させるには
- PowerShell2.0 : 全ての日本語ヘルプファイル(テキスト)を作成するスクリプト
- PowerShell2.0 : プロンプトの変更
- PowerShell2.0 : .NET Framework のスタティックメソッドの実行






