2013-07-31 138 views
0

通过StackOverflow和其他地方的各种不同的帖子,我可以将FTP上传文件的PowerShell脚本放在一起,并且效果很好。不过,我想给它增加一些更多的冗长。请参见下面的代码:将字节数组转换为true/false

foreach ($file in $uploadfiles) 
{ 

# create the full path to the file on remote server (odd but okay!) 
$ftp_command = $ftp + $file 

# for debugging 
#$ftp_command 

# create a new URI object for the full path of the file 
$uri = New-Object System.URI($ftp_command) 

#for debugging 
#$uri 

# finally do our upload to the remote server - URI object, full path to local file 
#$responseArray = $ftpclient.UploadFile($uri,$file.Fullname) 



$result = $ftpclient.UploadFile($uri,$file.Fullname) 




if ($result) { 
    $file.Fullname + " uploaded successfully" 
} else { 
    $file.Fullname + " not uploaded successfully" 
} 

} 

文件上传后基本上,我想检查,看看它是否是成功还是失败。上传文件应该返回一个字节数组(http://msdn.microsoft.com/en-us/library/36s52zhs(v=vs.80).aspx; 一个包含来自资源响应主体的字节数组,)。我是新来的PowerShell,所以这可能是我的问题所在,但在我的生活中,我无法从$ result得到任何东西,因此我可以测试。是否有可能服务器没有返回任何东西,或者我只是没有正确访问/设置字节数组?我尝试了各种不同的东西,但还没有弄清楚什么。

感谢,

回答

0

我不会在你的情况begining PowerShell 2.0中可以使用的try/catch部分使用$结果。

try 
{ 
    $ftpclient.UploadFile($uri,$file.Fullname) 
} 
catch [System.Net.WebException] 
{ 
# here $_ gives you the exception details 
} 
+0

谢谢。我认为这会让我得到我需要的地方! –