作为我的问题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如何不是一个有效的命令?
答案 0 :(得分:1)
尝试使用限定符:
Microsoft.PowerShell.Management\Test-Path ( $target_dir + "receivals.tmp")
答案 1 :(得分:0)
我将此添加到我的构建脚本中。
Import-Module "$env:SystemRoot\System32\WindowsPowerShell\v1.0\Modules\Microsoft.PowerShell.Management\Microsoft.PowerShell.Management.psd1"