powershell测试路径未被识别为cmdlet

powershell测试路径未被识别为cmdlet,powershell,runtime-error,scheduled-tasks,Powershell,Runtime Error,Scheduled Tasks,作为我问题的后续,我发现了另一个奇怪的情况。我有一个脚本,它作为计划任务每天运行一次。该问题仅在作为计划任务运行时(从不从命令提示符、ide或powershell窗口运行)才会发生,甚至仅在某些天才会发生。有关守则如下: $target_dir = "\\server\path\" $tmpStatus = "init" $err = $false try { $tmpStatus = Test-Path ( $target_dir + "receivals.tmp") } catch {

作为我问题的后续,我发现了另一个奇怪的情况。我有一个脚本,它作为计划任务每天运行一次。该问题仅在作为计划任务运行时(从不从命令提示符、ide或powershell窗口运行)才会发生,甚至仅在某些天才会发生。有关守则如下:

$target_dir = "\\server\path\" $tmpStatus = "init" $err = $false try { $tmpStatus = Test-Path ( $target_dir + "receivals.tmp") } catch { $tmpStatus = $error | foreach { $_.Exception } $err = $true } $target_dir=“\\server\path\” $tmpStatus=“init” $err=$false 试试{$tmpStatus=测试路径($target_dir+“receivals.tmp”)} 抓住 { $tmpStatus=$error | foreach{$\.Exception} $err=$true } 有时$tmpStatus会报告问题

System.Management.Automation.CommandNotFoundException: The term 'Test-Path' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) System.Management.Automation.CommandNotFoundException:术语“测试路径”不能识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。 位于System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,Exception Exception) at系统.管理.自动化.解释器.ActionCallInstruction`2.运行(解释器框架) 在System.Management.Automation.Translator.EnterTryCatchFinallyInstruction.Run(解释器框架) 在System.Management.Automation.Translator.EnterTryCatchFinallyInstruction.Run(解释器框架) 然而,稍后在同一次运行的同一脚本中,当我再次使用测试路径时,每次都可以正常工作。测试路径怎么可能不是有效的命令?

请尝试使用限定符:

Microsoft.PowerShell.Management\Test-Path ( $target_dir + "receivals.tmp")

我将其添加到构建脚本中

Import-Module "$env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1"

今天开始在我的构建系统中运行。@您的ranomore是按计划任务运行的,还是以交互方式或其他方式运行的?你能可靠地再现错误吗?但愿我能早点看到你的笔记。我重新启动了开始发生这种情况的机器,问题消失了。但它是通过TeamCity在一个非交互过程中运行的。我尝试过对脚本进行各种重构,但错误依然存在。我的重构似乎与服务器重新启动无关。虽然重新启动比下一个脚本计划早17个小时。好的,它已经工作了一个多月了,而上一个成功连胜最大值大约为1周。为什么您认为这在使用
测试路径时有效?