2010-11-11 33 views
0

我正在编写一个Powershell脚本,它将从ZIP文件中提取一组数据文件,然后将它们附加到服务器上。我写了一个函数,它的解压缩的照顾,因为我需要抓住所有的文件,让我知道我安装我返回从功能:Powershell中的数据类型转换问题

function Unzip-Files 
{ 
    param([string]$zip_path, [string]$zip_filename, [string]$target_path, [string]$filename_pattern) 

    # Append a \ if the path doesn't already end with one 
    if (!$zip_path.EndsWith("\")) {$zip_path = $zip_path + "\"} 
    if (!$target_path.EndsWith("\")) {$target_path = $target_path + "\"} 

    # We'll need a string collection to return the files that were extracted 
    $extracted_file_names = New-Object System.Collections.Specialized.StringCollection 

    # We'll need a Shell Application for some file movement 
    $shell_application = New-Object -com shell.Application 

    # Get a handle for the target folder 
    $target_folder = $shell_application.NameSpace($target_path) 

    $zip_full_path = $zip_path + $zip_filename 

    if (Test-Path($zip_full_path)) 
    { 
     $target_folder = $shell_application.NameSpace($target_path) 
     $zip_folder = $shell_application.NameSpace($zip_full_path) 

     foreach ($zipped_file in $zip_folder.Items() | Where {$_.Name -like $filename_pattern}) 
     { 
      $extracted_file_names.Add($zipped_file.Name) | Out-Null 
      $target_folder.CopyHere($zipped_file, 16) 
     } 
    } 

    $extracted_file_names 
} 

我然后调用另一个函数实际上附加数据库(我已经删除了一些代码,检查数据库的存在,但不应该在这里影响的东西):

function Attach-Database 
{ 
    param([object]$server, [string]$database_name, [object]$datafile_names) 

    $database = $server.Databases[$database_name] 

    $server.AttachDatabase($database_name, $datafile_names) 

    $database = $server.Databases[$database_name] 

    Return $database 
} 

我不断收到一个错误,虽然,“不能转换参数” 1 “,值为”System.Object []“,”AttachDatabase“用于键入”System.Collections.Specialized.StringCollection“”。

我试过在各个点显式声明数据类型,但这只是改变我得到错误(或类似于它的一个位置)的位置。我也改变了参数声明来使用字符串集合而不是没有运气的对象。

我从一个字符串集合开始,最终想要消费一个字符串集合。我似乎无法让Powershell在某个时刻停止尝试将其转换为通用对象。

有什么建议吗?

谢谢!

回答

1

看起来你应该返回使用逗号操作符的名字:

... 
    , $extracted_file_names 
} 

避免“展开”收集到的物品,并保留原始集合对象。

有这样几个问题,这里只是一对夫妇:

Strange behavior in PowerShell function returning DataSet/DataTable

Loading a serialized DataTable in PowerShell - Gives back array of DataRows not a DataTable

UPDATE:

此类似代码的工作:

Add-Type @' 
using System; 
using System.Collections.Specialized; 

public static class TestClass 
{ 
    public static string TestMethod(StringCollection data) 
    { 
     string result = ""; 
     foreach (string s in data) 
     result += s; 
     return result; 
    } 
} 
'@ 

function Unzip-Files 
{ 
    $extracted_file_names = New-Object System.Collections.Specialized.StringCollection 

    foreach ($zipped_file in 'xxx', 'yyy', 'zzz') 
    { 
     $extracted_file_names.Add($zipped_file) | Out-Null 
    } 

    , $extracted_file_names 
} 

function Attach-Database 
{ 
    param([object]$datafile_names) 

    # write the result 
    Write-Host ([TestClass]::TestMethod($datafile_names)) 
} 

# get the collection 
$names = Unzip-Files 

# write its type 
Write-Host $names.GetType() 

# pass it in the function 
Attach-Database $names 

由于预计,它的输出是:

System.Collections.Specialized.StringCollection 
xxxyyyzzz 

如果我删除的建议逗号,然后我们得到:

System.Object[] 
Cannot convert argument "0", with value: "System.Object[]", 
for "TestMethod" to type "System.Collections.Specialized.StringCollection"... 

症状看起来是一样的,因此该解决方案想必应该工作也是如此,如果没有其他不必要的转换/在Unzip-FilesAttach-Database调用之间的省略代码中展开。

+0

我建议已经(我的答案在下面删除),它没有工作。 – Joey 2010-11-11 23:44:09

+0

嗯,我看不到删除的答案:)。为什么它不工作? – 2010-11-11 23:50:30

+0

对不起,它看起来确实解决了问题。乔伊,如果你取消了你的回答,那么我可以接受它。 +1罗马。感谢你们俩! – 2010-11-12 15:55:03