这是我的代码示例:
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上一切正常,代码运行没有任何问题。但是当我在控制台上运行它时,当我更新CSV文件时会发生任何事情。我发现了问题,它在$ConWebSrv = New-WebServiceProxy -Uri $uri
上。如果我注释掉代码,它在控制台上运行没有问题。
注意:如果您在控制台上复制并粘贴,则可以使用.ps1文件。
答案 0 :(得分:0)
解决了这个问题。脚本块$ changeaction的变量需要在该scriptblock内。