1

こんにちは、内部に関数を実装した古い VBscript があります。

この関数には、QTP と呼ばれるプログラムを実行および停止できるオブジェクトと、このプログラムに対するその他のより高度な部分が含まれています。

Powershell でこれを行う方法を見つけることができなかったので、古い VBscript ファイルからその 1 つの関数を再利用しない理由を考えました。結局のところ、これは私が思っていたよりも少し複雑です。

MSScriptControl.ScriptControl を使用して VBscript にアクセスできないため、.net で望ましい C# で何かを見つけることができると思いましたが、その 1 つの関数だけでなく、スクリプト全体を実行するいくつかの方法を見つけました。例えば:

System.Diagnostics.Process.Start(@"cscript //B //Nologo c:\yourfile.vbs");

だから私は今ここで尋ねています.Powershellファイルからその1つの関数を実行するか、それをPowershellに変換するにはどうすればよいですか?関数コードは次のとおりです:

Public Sub ExecuteTest(ByVal TestPath , ByVal TestName)
 Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
 Dim qtTest 'As QuickTest.Test ' Declare a Test object variable
 Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable
 strLog = "Start " & TestName
 WriteLogToFile(strLog)
 Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
 qtApp.Launch ' Start QuickTest
 qtApp.Visible = False ' Make the QuickTest application invisible
 qtApp.Visible = True ' Make the QuickTest application visible

 ' Set QuickTest run options: 
 ' Always--Captures images for all steps in the run. 
 ' OnError--Captures images only for failed steps. 
 ' OnWarning--Captures images only for steps that return a failed or warning status. 
 ' Never--Never captures images. 

   qtApp.Options.Run.ImageCaptureForTestResults = "Always"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "OnWarning"
 ' qtApp.Options.Run.ImageCaptureForTestResults = "Never"


 qtApp.Options.Run.RunMode = "Fast"
 qtApp.Options.Run.ViewResults = False

 'This will Open a script
 ' Wscript.Echo  TestPath
  qtApp.Open TestPath , False ' Open the test in edit mode
 ' qtApp.Open TestPath , True ' Open the test in read-only mode

 ' set run settings for the test

 Set qtTest = qtApp.Test

 ' qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
 ' qtTest.Settings.Run.StartIteration = 1
 ' qtTest.Settings.Run.EndIteration = 1
 ' qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

 'For Viewing Results
 Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object

 ' WScript.Echo  letter & testresult & TestName & "\Result_"  & GetDateTimeStr

 qtResultsOpt.ResultsLocation = letter & testresult & TestName & "\Result_"  & GetDateTimeStr ' Set the results location
 qtTest.Run qtResultsOpt ' Run the test

 While qtTest.IsRunning
   'Wait For Test To Finish
 Wend

 strLog = "Slut " & TestName  &  qtTest.LastRunResults.Status 

 ' qtApp.Options.Run.ViewResults = True
 WriteLogToFile(strLog)


 'Close QTP
 qtTest.Close() ' Close the test

 'Set the options to nothing
 Set qtResultsOpt = Nothing ' Release the Run Results Options object
 Set qtTest = Nothing ' Release the Test object
 Set qtApp = Nothing ' Release the Application object

End Sub 

ここでDavid Martinから得たものを使用して助けてくれてありがとう、目に見えないメソッドとプロパティの問題の解決策を見つけました。作業方法は次のとおりです。

  function ExecuteTest($TestPath,$letter,$testresult,$TestName)
{
    #Update-TypeData -Prepend .\ComObject.Types.ps1xml
    #param($TestPath=[string],$TestName=[string])

    $qtApp = New-Object -comobject QuickTest.Application -strict

    $strLog = "Start $TestName" 
    WriteLogToFile($strLog)
    # Start QuickTest
    [System.__ComObject].InvokeMember(“Launch”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtApp,$null,$null,$null,$null)

    Start-Sleep -Seconds 10
    $obj = New-Object System.Object
    # Make the QuickTest application invisible
    $obj = $False
    #Make the QuickTest application visible
    $obj = $True
    $myArray = @($nul)
    $myArray[0] =$obj
    #Wait for the aplication to be initialized 

    #Apply the Visibility Variable to the QuickTest application
    [System.__ComObject].InvokeMember("Visible",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp,$myArray)

    #Make the QuickTest application visible
    #$qtApp.Visible = $True 



    # Set QuickTest run options: 
    # Always--Captures images for all steps in the run. 
    # OnError--Captures images only for failed steps. 
    # OnWarning--Captures images only for steps that return a failed or warning status. 
    # Never--Never captures images. 

    $qtApp.Options.Run.ImageCaptureForTestResults = "Always"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "OnWarning"
    # $qtApp.Options.Run.ImageCaptureForTestResults = "Never"
    $obj = "Fast"
    $myArray[0] =$obj
    [System.__ComObject].InvokeMember("RunMode",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp.Options.Run,$myArray)

    #$qtApp.Options.Run.RunMode = "Fast"

    #$qtApp.Options.Run.ViewResults = $False
    $obj = $False
    $myArray[0] =$obj
    [System.__ComObject].InvokeMember("ViewResults",[System.Reflection.BindingFlags]::SetProperty,$null,$qtApp.Options.Run,$myArray)

    #This will Open a script
    # Wscript.Echo  TestPath
    $obj = $False
    $myArray[0] = $TestPath
    $myArray += $obj
    #Invoke-Method - inputobject $qtApp -MethodName Open -MethodParameters $myArray -Static
    $qtApp.GetType().InvokeMember(“Open”,#Method Name
        [System.Reflection.BindingFlags]::InvokeMethod,#BindingFlag
        $null,#Binder
        $qtApp,#Target
        ($TestPath),#Args
        $null,#Modifiers
        $null,#culture
        $null) #[string[]]("TestPath"))#namedParameters
  # [System.__ComObject].InvokeMember(“Open”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtApp,([Object[]]$myArray),$null,$null,$null)
    #$qtApp.Open #$TestPath, $False #' Open the test in edit mode
    # $qtApp.Open $TestPath , $True #' Open the test in read-only mode
    # set run settings for the test
    $qtTest = $qtApp.GetType().InvokeMember("Test",[System.Reflection.BindingFlags]::GetProperty,$null,$qtApp,$null)

    # $qtTest.Settings.Run.IterationMode = "rngIterations" ' Run only iterations 2 to 4
    # $qtTest.Settings.Run.StartIteration = 1
    # $qtTest.Settings.Run.EndIteration = 1
    # $qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs

    # WScript.Echo  letter & testresult & TestName & "\Result_"  & GetDateTimeStr

    # The following variables have not been defined
    #letter
    #testresult
    # This function doesn't exist
    #GetDateTimeStr
    $qtResultsOpt = New-Object -comobject QuickTest.RunResultsOptions -strict
    #$qtResultsOpt = FileSystemObject QuickTest.RunResultsOptions
    $ParentPath = $letter + $testresult + $TestName 
    $ChildPath = "\Result_" + $("{0:yyyymmddTHHMMss}" -f $(get-date))
    $ResultOptArgs = join-path -path $ParentPath -childpath  $ChildPath
    [System.__ComObject].InvokeMember(“ResultsLocation”,[System.Reflection.BindingFlags]::SetProperty,$null,$qtResultsOpt,$ResultOptArgs)
    #$qtResultsOpt.GetType().InvokeMember(“ResultsLocation”,#Method Name
     #   [System.Reflection.BindingFlags]::InvokeMethod,#BindingFlag
      #  $null,#Binder
       # $qtResultsOpt,#Target
        #($ResultOptArgs),#Args
       # $null,#Modifiers
        #$null,#culture
       # $null) #[string[]]("TestPath"))#namedParameters


    # Run the test
    $qtTest.GetType().InvokeMember(“Run”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtTest,($qtResultsOpt),$null,$null,$null)
    #$qtTest.Run # $qtResultsOpt 


    do
    {
        Start-Sleep -m 100
    }
    while($qtTest.IsRunning)

    $strLog = "Slut $TestName $(qtTest.LastRunResults.Status)"

    # qtApp.Options.Run.ViewResults = True
    WriteLogToFile($strLog)


    #Close QTP
    #$qtTest.Close() 
    [System.__ComObject].InvokeMember(“Close”,[System.Reflection.BindingFlags]::InvokeMethod,$null,$qtTest,$null,$null,$null,$null)
    #Set the options to nothing
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtResultsOpt)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtTest)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtApp)
}
4

1 に答える 1