【问题标题】:powershell not seeing filesystem change - "cannot find path"powershell 没有看到文件系统更改 - “找不到路径”
【发布时间】:2015-03-19 11:18:30
【问题描述】:

我编写了一个 powershell 脚本,可以从 Dynamics NAV 实例中导出一堆文件。它调用我也编写的 perl 脚本,然后将所有文件拆分为单独的对象,并将它们粘贴在我在 perl 中创建的目录下的子目录中。然后 powershell 脚本尝试将文件复制到不同的目录,但失败了。

Powershell 生成目录名:

$datestamp = get-date -f MM-dd-yyyy_HH_mm_ss
$dumpdir = "\temp\nav_export\" + $env:username + "\" + $servicetier + "~" + $database + "~" + $datestamp;

然后powershell做了一堆可以正常工作的东西,并调用perl脚本(脚本前面定义了$servicetier和$database):

& c:\navgit\split-objects.pl $servicetier $database $datestamp

perl 继续创建目录并正确拆分文件:

use strict;
use warnings;
use File::Path qw(make_path remove_tree);

my $username = getlogin || getpwuid($<);
my $servicetier = $ARGV[0];
my $database = $ARGV[1];
my $datestamp = $ARGV[2];
undef @ARGV;

my $work_dir = "/temp/nav_export";
my $objects_dir = "$work_dir/$username/objects";
my $export_dir = "$work_dir/$username/$servicetier~$database~$datestamp";

print "Objects from $servicetier~$database being exported to $export_dir\n";

make_path("$export_dir/Page", "$export_dir/Codeunit", "$export_dir/MenuSuite", "$export_dir/Query", "$export_dir/Report", "$export_dir/Table", "$export_dir/XMLport");

chdir $objects_dir or die "Could not change to $objects_dir: $!";

<does all of the filehandling and parsing>

控制返回到 powershell 脚本,该脚本尝试以以下方式结束:

Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse

但这会引发错误:

Copy-Item : Cannot find path 'C:\temp\nav_export\danielj\cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50' because it does not exist.
At C:\navgit\nav-export.ps1:175 char:1
+ Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\temp\nav_exp...0-2015_19_26_50:String) [Copy-Item], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyItemCommand

我试图从中复制的目录确实存在。但是powershell看不到!我添加了一些代码来列出父目录的内容:

Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse
Write-Host "Copy-Item -Path $dumpdir -Destination $cwd -Force -Recurse"
$test = "C:\temp\nav_export\$env:username"
Get-ChildItem $test -Force

Copy-Item -Path \temp\nav_export\danielj\cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50 -Destination C:\Users\danielj\erp\ -Force -Recurse

    Directory: C:\temp\nav_export\danielj

Mode                LastWriteTime     Length Name                                                                                                                        
----                -------------     ------ ----                                                                                                                        
d----         1/20/2015   6:32 PM            cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_18_32_33                                                                       
d----         1/20/2015   7:08 PM            cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_08_49                                                                       
d----         1/19/2015   1:07 PM            cen-dev-erp-st1~JustFoodERP-PROTO~20150119-130747                                                                           
d----         1/20/2015   7:26 PM            logs                                                                                                                        
d----         1/20/2015   7:26 PM            objects                                                                                                                     
-a---         1/20/2015   7:26 PM        309 objects.bat                                                                                                                 
-a---         1/20/2015   1:41 PM        436 soap_envelope.txt

如果我从脚本外部列出目录,那就是:

PS C:\Users\danielj\erp> $test = "C:\temp\nav_export\$env:username"
Get-ChildItem $test -Force

Directory: C:\temp\nav_export\danielj


Mode                LastWriteTime     Length Name                                                                                                                        
----                -------------     ------ ----                                                                                                                        
d----         1/20/2015   6:32 PM            cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_18_32_33                                                                       
d----         1/20/2015   7:08 PM            cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_08_49                                                                       
d----         1/20/2015   7:26 PM            cen-dev-erp-st1~JustFoodERP-PROTO~01-20-2015_19_26_50                                                                       
d----         1/19/2015   1:07 PM            cen-dev-erp-st1~JustFoodERP-PROTO~20150119-130747                                                                           
d----         1/20/2015   7:26 PM            logs                                                                                                                        
d----         1/20/2015   7:26 PM            objects                                                                                                                     
-a---         1/20/2015   7:26 PM        309 objects.bat                                                                                                                 
-a---         1/20/2015   1:41 PM        436 soap_envelope.txt                                                                                                           

我试过在perl完成后从主powershell脚本调用外部脚本,结果是一样的。

为什么 powershell 看不到 perl 脚本创建的目录或文件?更重要的是,我怎样才能让它这样做?

【问题讨论】:

  • 在黑暗中拍摄 - 它与波浪号有​​关吗?我不知道是不是这个角色有问题。它曾经意味着一些特别的东西......它仍然是......旧的 8.3 文件名
  • @matt,不。我很欣赏这个建议,但我已经用谷歌搜索了非法的 Windows 文件名字符:\ / : * ? " &lt; &gt; |
  • 哦,这不是非法的……如果是这样,你就不会走到那一步……只是觉得可能有什么问题。
  • @Matt,为了彻底,我只是用#尝试过。结果相同。你永远不知道...

标签: windows perl powershell powershell-3.0


【解决方案1】:

您确定一切都按照您假设的确切顺序进行吗?

尝试像这样启动你的 perl 脚本:

& c:\navgit\split-objects.pl $servicetier $database $datestamp | Out-Null

它确保在 PowerShell 执行脚本的其余部分时终止您的 Perl 部分。

【讨论】:

  • 感谢您的建议,但这给了我错误`无法在管道中间运行文档:C:\navgit\split-objects.pl。在 C:\navgit\nav-export.ps1:102 char:1 + & c:\navgit\split-objects.pl $servicetier $database $datestamp $startdir | Out-N ...`
【解决方案2】:

我最终只是首先在 powershell 中创建了目录。没有理由必须在 perl 脚本中创建它。

很高兴知道为什么它没有按我预期的方式工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2015-12-04
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多