none
オブジェクトのメンバー名と値列挙について RRS feed

  • 質問

  • オブジェクトのメンバ名と値を列挙してファイルへ出力する方法についての質問です。

    ### test1.ps1 ###
    $objIe = New-Object -com InternetExplorer.Application
    $objIe.Navigate("http://www.google.co.jp/")
    do{ Start-Sleep -m 1 } until ($True -ne $objIe.Busy -and 4 -eq $objIe.ReadyState)
    $objIe
    $objIe.Document
    $objIe.Quit()
    #################

    test1.ps1 を行うと、$objIe と $objIe.Document のメンバ名と値が画面に表示されます。
    この内容をテキストファイルへ出力したいです。そこで

    ### test2.ps1 ###
    $objIe = New-Object -com InternetExplorer.Application
    $objIe.Navigate("http://www.google.co.jp/")
    do{ Start-Sleep -m 1 } until ($True -ne $objIe.Busy -and 4 -eq $objIe.ReadyState)
    (([String]$objIe)+"`r`n"+([String]$objIe.Document)) | Out-File tmp.log
    $objIe.Quit()
    #################

    を行うと

    ### test2.ps1 の結果 ###
    System.__ComObject
    mshtml.HTMLDocumentClass
    ########################

    という結果です。(不本意)
    ↓一時ファイルを介してみると、

    ### test3.ps1 ###
    $tmpfile1="tmp.log"
    $objIe = New-Object -com InternetExplorer.Application
    $objIe.Navigate("http://www.google.co.jp/")
    do{ Start-Sleep -m 1 } until ($True -ne $objIe.Busy -and 4 -eq $objIe.ReadyState)
    $objIe | Out-File $tmpfile1
    $str1=[System.IO.File]::ReadAllText($tmpfile1).Trim()
    $objIe.Document | Out-File $tmpfile1
    $str1=$str1+"`r`n`r`n"+[System.IO.File]::ReadAllText($tmpfile1).Trim()
    $str1 | Out-File ie.log
    $objIe.Quit()
    Remove-Item $tmpfile1
    ###################

    意図する結果が ie.log に保存されました。test3.ps1 の方法でも良いのですが、できれば一時ファイルを使わずに処理したいです。
    一時ファイルを使わずに test3.ps1 と同じ結果を得る方法を、教えてください。

    環境 Windows XP Professional SP3 , Powershell 2.0,  IE 8.0

    2010年6月27日 7:00

回答

すべての返信

  • はじめまして、

    こんな感じではどうですか?

    ($objie | out-string ) + ($objie.document | out-string) | out-file ie.log

    2010年6月28日 3:29
  • minminnana207 さん

    意図した結果が得られました。ありがとうございました。
    [get-help out-file]の結果の[関連するリンク]のところに[Out-String]が載っていることに、今気付きました。

    2010年6月28日 10:19