2017-04-06 45 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 
}