作为我之前提出的问题 powershell if-else does not follow either branch 的跟进,我发现了另一个奇怪的情况。我有一个脚本,每天作为计划任务运行一次。问题只会在作为计划任务运行时出现(从命令提示符、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
}
有些时候,$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)
然而,在同一次运行的同一脚本中,稍后再次使用Test-Path时,它每次都可以正常工作。为什么Test-Path不是有效的命令?
有些时候,$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)
然而,在同一次运行的同一脚本中,稍后再次使用Test-Path时,它每次都可以正常工作。为什么Test-Path不是有效的命令?