powershell 测试路径未被识别为 cmdlet [英] powershell Test-Path is not recognized as a cmdlet

查看:50
本文介绍了powershell 测试路径未被识别为 cmdlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为对我的问题的跟进 powershell if-else 没有按照任一分支 我发现了另一种奇怪的情况.我有一个每天作为计划任务运行的脚本.该问题仅在作为计划任务运行时出现(从不从命令提示符、ide 或 powershell 窗口运行),甚至仅在某些日子出现.相关代码是

<前>$target_dir = "\\服务器\路径\"$tmpStatus = "初始化"$err = $falsetry { $tmpStatus = Test-Path ( $target_dir + "receivals.tmp") }抓住{$tmpStatus = $error |foreach { $_.Exception }$错误= $真}

有些日子 $tmpStatus 报告问题

<前>System.Management.Automation.CommandNotFoundException:术语Test-Path"未被识别为 cmdlet、函数、脚本文件或可运行程序的名称.检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试.在 System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,异常异常)在 System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

但是,稍后当我再次使用 Test-Path 时,在同一次运行的同一个脚本中,它每次都能正常工作.Test-Path 怎么可能不是一个有效的命令?

解决方案

尝试使用限定符:

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

As a follow up to my question powershell if-else does not follow either branch I've found another odd situation. 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. 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

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. How can Test-Path not be a valid command?

解决方案

try with qualifier:

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

这篇关于powershell 测试路径未被识别为 cmdlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆