简体   繁体   English

无法将Powershell Test-Path识别为cmdlet

[英]powershell Test-Path is not recognized as a cmdlet

As a follow up to my question powershell if-else does not follow either branch I've found another odd situation. 作为我问题的后续问题, powershell if-else不遵循任何一个分支,我发现了另一种奇怪的情况。 I have a script that is running as a scheduled task once a day. 我有一个脚本,该脚本每天作为计划任务运行一次。 The problem occurs only when run as a scheduled task (never from a command prompt, ide, or powershell window) and even then only on some days. 仅当按计划任务运行时才会出现此问题(永远不要从命令提示符,IDE或Powershell窗口运行),甚至在某些日子之后才发生。 The relevant code is 相关代码是

$target_dir = "\\server\path\"
$tmpStatus = "init"
$err = $false
try { $tmpStatus = Test-Path ( $target_dir + "receivals.tmp") }
catch
{
    $tmpStatus = $error | foreach { $_.Exception }
    $err = $true
}

Some days $tmpStatus reports the problem 某些时候$ 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)

However, later on in the same script on the same run when I use Test-Path again, it works just fine every time. 但是,稍后当我再次使用Test-Path时,在同一脚本中以相同的运行方式运行,每次都可以正常工作。 How can Test-Path not be a valid command? Test-Path如何不能成为有效命令?

尝试使用限定词:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM