无法在Powershell中第二次转换参数

时间:2017-11-28 23:25:51

标签: powershell

我正在尝试调用Web服务。如果我刚刚启动Powershell,则以下代码运行完美。任何后续运行都会出现以下错误。

Cannot convert argument "fieldsWithValues", with value: 
"FileHold.LibraryManager.FieldWithValue[]", for "SetMetadata" to type 
"FileHold.LibraryManager.FieldWithValue[]": "Cannot convert the 
"FileHold.LibraryManager.FieldWithValue" value of type 
"FileHold.LibraryManager.FieldWithValue" to type 
"FileHold.LibraryManager.FieldWithValue"."
At Z:\VM_Transfer\Customers\Oregon\UpdateMetadata\CannotConvertTest.ps1:19 char:1

以下是有问题的代码。

$fh = "http://fileholdtest8/fh/filehold"

$uri = $fh + "/UserRoleManager/SessionManager.asmx?WSDL"
$SessionManager = New-WebServiceProxy -Uri $uri -Namespace FileHold.SessionManager
$sessionId = $SessionManager.StartSession( 'sysadm', '12345', 4 )

$uri = $fh + "/LibraryManager/DocumentManager.asmx?WSDL"
$dm = New-WebServiceProxy -Uri $uri -Namespace FileHold.LibraryManager

$dm.CookieContainer = New-Object System.Net.CookieContainer
$cookie = New-Object System.Net.Cookie( 'FHLSID', $sessionId, "/", ([System.Uri]$dm.Url).Host )
$dm.CookieContainer.Add( $cookie )    

$newFieldValue = [FileHold.LibraryManager.FieldWithValue]::new()
$newFieldValue.FieldId = 1007
$newFieldValue.FieldValue = $true
[FileHold.LibraryManager.FieldWithValue[]]$fieldsWithValues = @( $newFieldValue )

$dm.SetMetadata( 4033, 1002, "EmployeeInfo_filled1-test", $fieldsWithValues, $false, $null )

如果不重新启动ISE,我每次都能运行此操作需要做什么。

顺便说一下。只要参数是简单类型,我就可以使用这种技术来调用其他方法而没有任何问题。似乎问题只发生在我有一个像 FileHold.LibraryManager.FieldWithValue 这样的复杂对象时。

PSVersion = 5.1.14393.1532

1 个答案:

答案 0 :(得分:0)

我通过确保不重用变量找到了解决方案。当然,我需要处理潜在的过期cookie,但这是另一个问题。

$fh = "http://fileholdtest8/fh/filehold"

$uri = $fh + "/UserRoleManager/SessionManager.asmx?WSDL"
$SessionManager = New-WebServiceProxy -Uri $uri -Namespace FileHold.SessionManager
$sessionId = $SessionManager.StartSession( 'sysadm', '12345', 4 )

if ( !(Test-Path variable:dm) )
{
    $uri = $fh + "/LibraryManager/DocumentManager.asmx?WSDL"
    $dm = New-WebServiceProxy -Uri $uri -Namespace FileHold.Library

    $FieldWithValueType = "{0}.FieldWithValue" -f $dm.GetType().Namespace

    $dm.CookieContainer = New-Object System.Net.CookieContainer
    $cookie = New-Object System.Net.Cookie( 'FHLSID', $sessionId, "/", ([System.Uri]$dm.Url).Host )
    $dm.CookieContainer.Add( $cookie )    
}

$newFieldValue = [FileHold.Library.FieldWithValue]::new()
$newFieldValue.FieldId = 1007
$newFieldValue.FieldValue = $true
$fieldsWithValues = @( ,$newFieldValue )

$dm.SetMetadata( 4041, 1002, "EmployeeInfo_filled1-test", $fieldsWithValues, $false, $null )