15

今天早上,我从本地网络驱动器复制了一个目录到临时文件夹进行测试。出现了这个错误。

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 行为。我是否误解了错误或解决方案?如何刷新缓存或任何可能存储在内存中的变量?

4

3 回答 3

21

我不喜欢每次需要清除缓存时都必须关闭并重新打开的想法。

这适用于 PowerShell

Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host

于 2014-04-22T17:48:53.057 回答
13

变量存储在会话中,因此如果您关闭 powershell 控制台并打开一个新控制台,所有自定义变量都将消失。如果要查看存在哪些变量,请使用Get-Variable. 要删除特定变量(以确保它消失),您可以使用:

Remove-Variable varname.

至于你的问题。全局(会话)范围内的变量存储在会话中,直到它被删除或覆盖。如果您使用相同的 powershell 控制台两次运行该代码,则$inpath应该设置为"C:\temp\tx"第二次,并且$srcfiles将使用新的文件列表进行更新。

于 2013-01-24T13:50:37.733 回答
0

除了@Patrick 的回答,您还可以像这样删除指定的模块:

,@("MyModule1","MyModule2") | %{&remove-module -erroraction:silentlycontinue $_}

管道灵感来自这里

所以帕特里克的单线会变成Remove-Variable * -ErrorAction SilentlyContinue; ,@("MyModule1","MyModule2") | %{&Remove-Module -ErrorAction:SilentlyContinue $_}; $error.Clear(); Clear-Host

于 2018-08-17T09:37:43.567 回答