如何在ps1文件中翻译或运行VBscript函数

本文关键字:运行 VBscript 函数 翻译 ps1 文件 | 更新日期: 2023-09-27 18:25:31

嗨,我有一个旧的VBscript,里面有一个实现的函数。

该函数包含一个对象,该对象可以执行和停止名为QTP的程序以及该程序的其他更高级的部分。

由于我在Powershell中找不到任何方法来实现这一点,我想为什么不重用旧VBscript文件中的那个函数呢。事实证明,这比我想象的稍微复杂一些。

由于我不能使用MSScriptControl.ScriptControl访问我的VBscript,我想也许我可以在.net中找到一些更可取的C#但我发现的执行整个脚本的几种方法,而不仅仅是一个函数。例如:

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

所以我现在在这里问,我如何从我的Powershell文件中执行那个函数,或者将它翻译成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)
}

如何在ps1文件中翻译或运行VBscript函数

我已经快速尝试过为您翻译这个,我没有QuickTest,所以我不能为您测试这个。

function WriteLogToFile
{
    param ($message)
    write-host message
}
function ExecuteTest
{
    param
    (
        $TestPath,
        $TestName
    )
    $qtApp = New-Object -comobject QuickTest.Application -strict
    $qtTest = New-Object -comobject QuickTest.Test -strict
    $qtResultsOpt = New-Object -comobject QuickTest.RunResultsOptions
    $strLog = "Start $TestName" 
    WriteLogToFile($strLog)
    # Start QuickTest
    $qtApp.Launch 
    # Make the QuickTest application invisible
    $qtApp.Visible = $false 
    #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"
    qtApp.Options.Run.RunMode = "Fast"
    qtApp.Options.Run.ViewResults = False
    #This will Open a script
    write-host "TestPath: [$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
    $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
    # WScript.Echo  letter & testresult & TestName & "'Result_"  & GetDateTimeStr
    # The following variables have not been defined
    #letter
    #testresult
    # This function doesn't exist
    #GetDateTimeStr
    $qtResultsOpt.ResultsLocation = join-path -path "c:'results" -childpath $("{0:yyyymmddTHHMMss}" -f $(get-date))
    # Run the test
    $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() 
    #Set the options to nothing
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtResultsOpt)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtTest)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($qtApp)
}