Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Exception 在powershell中打印try catch块中的错误_Exception_Try Catch_Powershell 3.0 - Fatal编程技术网

Exception 在powershell中打印try catch块中的错误

Exception 在powershell中打印try catch块中的错误,exception,try-catch,powershell-3.0,Exception,Try Catch,Powershell 3.0,这是我的脚本,它返回一个布尔值 param($fileName, $path, $contextMenuItem, $automationDLLPath) function CloseWindowsExplorer() { (New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()} } Try { Import-Module $automationDLLPath # Open th

这是我的脚本,它返回一个布尔值

 param($fileName, $path, $contextMenuItem, $automationDLLPath)

 function CloseWindowsExplorer()
{
(New-Object -comObject Shell.Application).Windows() | foreach-object {$_.quit()}
}

Try
{ 

Import-Module $automationDLLPath

# Open the explorer window in a maximized form
Start-Process explorer $path -WindowStyle Maximized
Start-Sleep 1
Get-UIAActiveWindow

# Get the "Items View" in Explorer to go through all the lements
$list = Get-UIAList -Name 'Items View' -TimeOut 30000;

# Get the file specified in the feature file from the Items View 
# Added a sleep because the VM takes time to perform the functions
Start-Sleep 1
$file = $list | Get-UIAListItem -Name $fileName;

# Perform a single click on the file to invoke a right click on it
Invoke-UIAListItemSelectItem -InputObject $file -ItemName $fileName;

# Added a sleep because the VM takes time to perform the functions
Start-Sleep 1

# Invoke the right click on the selected file
$menu = Invoke-UIAControlContextMenu -InputObject $file;

Start-Sleep 1

# select our context menu item
$menuItem = Get-UIAMenuItem -InputObject $menu $contextMenuItem -TimeOut 30000;

# Display error if the required item in the context menu is not found
if( $null -eq $menuItem){
%{ Write-Host 'cannot find menuItem' }
}
# Invoke the item if found in the context menu
else{

Invoke-UIAMenuItemClick -InputObject $menuItem 
}

# close the windows explorer and return true   
CloseWindowsExplorer
Write-Output "true"
}

Catch
{ 
# close the explorer window as a part of teardown and return false to reflect test        failure 

Write-Output "false"

CloseWindowsExplorer
}
我希望脚本打印捕获的异常,并返回布尔值,但在本例中,脚本失败时只返回false。谢谢你的帮助
基本上,我需要打印异常,就像try-catch块不存在一样。

您需要使用特殊变量$_

这个小例子展示了它的工作原理:

try {
  testmenow
} catch {
  Write-Host $_
}
$\是一个对象,因此您可以

$_|gm
在catch块中查看可以调用的方法