我正在编写一个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在某个时刻停止尝试将其转换为通用对象。
有什么建议吗?
谢谢!
我建议已经(我的答案在下面删除),它没有工作。 – Joey 2010-11-11 23:44:09
嗯,我看不到删除的答案:)。为什么它不工作? – 2010-11-11 23:50:30
对不起,它看起来确实解决了问题。乔伊,如果你取消了你的回答,那么我可以接受它。 +1罗马。感谢你们俩! – 2010-11-12 15:55:03