New-WebServiceProxy 仅在 ISE 的控制台上不起作用

New-WebServiceProxy don't work on console only on ISE

这是我的代码示例:

    Add-Type -AssemblyName PresentationFramework

    Get-Content "$PSScriptRoot\config.ini" | ForEach-Object -Begin {$config=@{}} -Process {$store = [regex]::split($_,'='); if(($store[0].CompareTo("") -ne 0) -and ($store[0].StartsWith("[") -ne $True) -and ($store[0].StartsWith("#") -ne $True)) {$config.Add($store[0], $store[1])}}

    $folder = $config.Get_Item("CSVFolder")
    $filter = $config.Get_Item("CSVFile")
    $uri = $config.Get_Item("WebServiceUri")

    Function Register-Watcher {
        $watcher = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ 
            IncludeSubdirectories = $false
            EnableRaisingEvents = $true
        }

        $changeAction = {

            $path = $Event.SourceEventArgs.FullPath
            $name = $Event.SourceEventArgs.Name
            $changeType = $Event.SourceEventArgs.ChangeType
            $timeStamp = $Event.TimeGenerated   

            $ConWebSrv = New-WebServiceProxy -Uri $uri
            $rescode = 0
            $resreason = 0

            $get = Get-Content -Path $path -Last 1
            $artnbr,$barc,$desc,$pu,$qtypu,$meanval,$mittelw,$free1,$free2,$free3,$lenght,$width,$height,$lengthuni,$volume,$voluni,$weight,$weightuni,$timest = $get.split(";")

            $CommWebSrv = $ConWebSrv.setItemDims($artnbr,$width,$lenght,$height,$weight,$weight,"EA","KG","CM","KHT",[ref]$rescode,[ref]$resreason)

            $msgBoxInput = [System.Windows.MessageBox]::Show("Nova leitura detectada. Pretende comunicar?","","YesNo", "Information")
            switch  ($msgBoxInput) {

                   "Yes" {write-host "Yes $artnbr"}
                   "No" {write-host "No"}
            }
        }

        Register-ObjectEvent $Watcher "Changed" -Action $changeAction
    }
Register-Watcher

我结束了脚本,在 ISE 上一切正常,代码 运行s 没有任何问题。但是当我 运行 它在控制台时,当我更新 CSV 文件时,任何事情都会发生。我发现了问题,它在 $ConWebSrv = New-WebServiceProxy -Uri $uri 上。如果我注释掉代码,它 运行 在控制台上没有问题。

注意:如果您在控制台上复制和粘贴它有效,请尝试使用 .ps1 文件。

问题解决了。脚本块 $changeaction 的变量需要在该脚本块内。