Powershell System.Management.Automation.MethodInvocationException

问题描述

我有一个小问题。

我尝试使用 Powershell 从 FTP 服务器下载文件并将它们存储在 Cifs 共享中

我的代码看起来像这样(首先我得到了 FTP 上所有文件的列表,然后我使用名称通过文件流下载它)

$savedir_GA = '\\test21\test_data$\AG\100_Bereich TECHNIK - ELE_WA_WAE\130_Planung\010 AV Daten\04_Gro\'
$savedir_KA = "\\test21\test_data$\AG\100_Bereich TECHNIK - ELE_WA_WAE\130_Planung\010 AV Daten\03_Kap\"



$ftp = "ftp://ftp.test-swiss.ch" 
$credentials =New-Object System.Net.NetworkCredential("test","test" ) 
$remotePickupDir = "/Planung/RSW/DXF_Export/AV/"

$ftpuri = $ftp + $remotePickupDir
$uri=[system.URI] $ftpuri
$ftprequest=[system.net.ftpwebrequest]::Create($uri)
$ftprequest.Credentials=$credentials
$ftprequest.Method=[system.net.WebRequestMethods+ftp]::ListDirectory
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false
$response = [System.Net.FtpWebResponse]$ftprequest.GetResponse()
[System.IO.Stream]$stream = $response.GetResponseStream()
$reader = new-object System.IO.StreamReader($stream)

$list = $reader.ReadToEnd() -split "`n"
$reader.Close()
$response.Close();
$stream.Close();

foreach ($line in $list)
    {   
    Write-Host $line
    $ftpuri = $ftp + $remotePickupDir + $line
    $uri=[system.URI] $ftpuri
    $ftprequest=[system.net.ftpwebrequest]::Create($uri)
    $ftprequest.Credentials=$credentials
    $ftprequest.Method=[system.net.WebRequestMethods+ftp]::DownloadFile
    $ftprequest.UseBinary = $true
    $ftprequest.KeepAlive = $false
    $response=[System.Net.FtpWebResponse]$ftprequest.GetResponse()
    [System.IO.Stream]$strm = $response.GetResponseStream()
    $response.Close();

    Write-Host $response
   
    try
    {
        if($line -like "TestFile1*")
        {
            $path =   $savedir_GA + $line
            Write-Host $path
            $targetfile = New-Object IO.FileStream ($path,[IO.FileMode]::Create)
            
             Write-Host $targetfile
        }
        ElseIf($line -like "testdok2*")
        {
            $targetfile = New-Object IO.FileStream ($savedir_KA+$line,[IO.FileMode]::Create)
            Write-Host $targetfile
        }
        
        if($strm -ne $null)
        {
            Write-Host "readbuffer"
            [byte[]]$readbuffer = New-Object byte[] 1024
            do
            {
                Write-Host "Read strm"
                $readlength = $strm.Read($readbuffer,1024)
                Write-Host "Write Target"
                $targetfile.Write($readbuffer,$readlength)
            }
            while ($readlength -ne 0)
        }
        
       
        Write-Host $line

        $strm.Close()
        $targetfile.Close()
        $ftprequest.Close()
    
       
    }
    catch
    {
        $_|fl * -Force
    }
    
}

但我的问题是,我得到以下异常

PSMessageDetails      : 
Exception             : System.Management.Automation.MethodInvocationException: Exception calling ".ctor" with "2" argument(s): "Illegal characters in path." ---> 
                        System.ArgumentException: Illegal characters in path.
                           at System.IO.Path.CheckInvalidpathChars(String path,Boolean checkAdditional)
                           at System.IO.Path.GetFileName(String path)
                           at System.IO.FileStream..ctor(String path,FileMode mode)
                           --- End of inner exception stack trace ---
                           at System.Management.Automation.DotNetAdapter.AuxiliaryConstructorInvoke(Methodinformation methodinformation,Object[] arguments,Object[] originalArguments)
                           at System.Management.Automation.DotNetAdapter.InvokeResolvedConstructor(Methodinformation bestMethod,Object[] newArguments,Object[] 
                        arguments)
                           at System.Management.Automation.DotNetAdapter.ConstructorInvokeDotNet(Type type,ConstructorInfo[] constructors,Object[] arguments)
                           at Microsoft.PowerShell.Commands.NewObjectCommand.CallConstructor(Type type,Object[] args)

首先我认为问题存在是因为用于存储文件的路径中有空格。 在那种情况下,我在每个空格前都添加了 ',但它不起作用。

我是否忽略了什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...