0

コンソール上に動作しません:新-たWebServiceProxyは、これが私のコードのサンプルであるだけISE

Add-Type -AssemblyName PresentationFramework 

    Get-Content "$PSScriptRoot\config.ini" | ForEach-Object -Begin {[email protected]{}} -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でokです、コードは問題なく動作します。しかし、私がCSVファイルを更新すると、何かがコンソールで実行されます。問題が見つかりました。それは$ConWebSrv = New-WebServiceProxy -Uri $uriです。私がコードにコメントすると、コンソール上で問題なく実行されます。

注意:コンソールにコピー&ペーストすると動作しますが、.ps1ファイルを試してみてください。

+0

試してみることができます: 'New-WebServiceProxy -Uri $ uri -UseDefaultCredential $ true' – Manu

+0

@Manu、same ...これはISE上で実行していて、試してみるとコンソールのbueでコピー& test.ps1は実行され、エラーは発生しませんが、ファイルを更新するとウィンドウがポップアップしません。 New-WebServiceProxyのアセンブリが必要なのでしょうか?私は検索しましたが、何も見つかりませんでした – emnt

答えて

0

問題を解決しました。スクリプトブロック$ changeactionの変数はそのスクリプトブロック内にある必要があります。

関連する問題