今天早上,我将一个目录从我的本地网络驱动器复制到临时文件夹进行测试。出现此错误。
Get-Content : Cannot find path 'C:\users\xxxxx\desktop\cgc\Automatic_Post-Call_Survey_-_BC,_CC.txt' because it does no
t exist.
At C:\users\xxxxx\desktop\cgc\testcountexcl1.ps1:55 char:12
+ Get-Content <<<< $txtfile | Get-WordCount -Exclude (Get-Content c:\temp\exclude.txt) | select -First 15
+ CategoryInfo : ObjectNotFound: (C:\users\xxxxx...ey_-_BC,_CC.txt:String) [Get-Content], ItemNotFoundEx
ception
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
这可以通过移动来预期... PS找不到引用的路径...但是在运行脚本之前我做了以下更改(旧的注释掉了新的):
$input = Get-Content c:\temp\wordCount.txt
<# $inpath = "C:\users\xxxxx\desktop\cgc\tx" #>
$inpath = "C:\temp\tx"
$srcfiles = Get-ChildItem $inpath -filter "*.txt"
$notPermittedWords = Get-Content c:\temp\exclude.txt
我的第一个暗示是,某种缓存持有我上次运行的$inpath
变量...但是无法确定是否存在预期的PowerShell行为。我误解了错误或解决方案吗?如何刷新缓存或内存中可能存储的任何可变数据?
答案 0 :(得分:15)
我不喜欢每次需要清除缓存时都必须关闭并重新打开的想法。
这适用于PowerShell
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host
答案 1 :(得分:13)
变量存储在会话中,因此如果关闭PowerShell控制台并打开一个新变量,则所有自定义变量都将消失。如果要查看存在哪些变量,请使用Get-Variable
。要删除特定变量(以确保它已消失),您可以使用:
Remove-Variable varname
。
至于你的问题。全局(会话)范围中的变量存储在会话中,直到删除或覆盖为止。如果您使用相同的powershell控制台运行该代码两次,那么$inpath
应该第二次设置为"C:\temp\tx"
,$srcfiles
将使用新文件列表进行更新。
答案 2 :(得分:0)
除了@Patrick's answer,您还可以删除指定的模块,例如:
,@("MyModule1","MyModule2") | %{&remove-module -erroraction:silentlycontinue $_}
灵感来自here
的管道所以帕特里克的一线将成为Remove-Variable * -ErrorAction SilentlyContinue; ,@("MyModule1","MyModule2") | %{&Remove-Module -ErrorAction:SilentlyContinue $_}; $error.Clear(); Clear-Host