2017-04-06 74 views
1

我试图从enable_local.ps1运行xTestPlan_1.ps1Powershell:此操作返回,因为超时期限过期

虽然enable_local.ps1位于本地计算机上,但网络驱动器上有xTestPlan_1.ps1。我的本地计算机上只有xTestPlan_1.ps1的快捷方式。

这里是enable_local.ps1

$item = "d:\temp\xTestPlan_1.lnk" 
WriteInLogFile "$item" 
Try 
{ 
    & "$item" 
} 
Catch 
{ 
    WriteInLogFile $Error 
} 

虽然运行此代码,有时,我得到这个错误:

Program "xTestPlan_1.lnk" failed to run: This operation returned because the timeout period expired At D:\Temp\enable_local.ps1.

这个脚本有时正常工作,有时不工作。网络驱动器上确实存在xTestPlan_1.ps1

+0

“有时,我得到这个错误”是否意味着它有时按预期工作? – lit

+0

抱歉误会。是的,这意味着,有时它可以正常工作,有时它不能按预期工作。 –

回答

2

尝试执行快捷方式并不是运行其他脚本的好方法。

更好的做到这一点是调用脚本直接:

$item = "\\server\share\xTestPlan_1.ps1" 
WriteInLogFile "$item" 
Try 
{ 
    & $item 
} 
Catch 
{ 
    WriteInLogFile $Error 
} 

如果你真的必须使用快捷出于某种原因,你可以从快捷方式的目标路径,然后调用脚本本身。

$item = "d:\temp\xTestPlan_1.lnk" 

$ShellObj = New-Object -COM WScript.Shell 
$targetPath = $ShellObj.CreateShortcut($item).TargetPath 

WriteInLogFile "$targetPath" 
Try 
{ 
    & $targetPath 
} 
Catch 
{ 
    WriteInLogFile $Error 
}