Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
powershell v2:WebClient/UploadString从不连接_Powershell_Powershell 2.0_Webclient_Uploadstring - Fatal编程技术网

powershell v2:WebClient/UploadString从不连接

powershell v2:WebClient/UploadString从不连接,powershell,powershell-2.0,webclient,uploadstring,Powershell,Powershell 2.0,Webclient,Uploadstring,使用powershell v2和,我尝试在文件被修改时发送推送通知 $folder = 'c:\path\to\file' $filter = '*.*' $user = "pushbullet_token" $url = "https://api.pushbullet.com/v2/pushes" $fsw = New-Object IO.FileSystemWatcher $folder, $filter $fsw.IncludeSubdirectories = $true $fsw.No

使用powershell v2和,我尝试在文件被修改时发送推送通知

$folder = 'c:\path\to\file'
$filter = '*.*'
$user = "pushbullet_token"
$url = "https://api.pushbullet.com/v2/pushes"

$fsw = New-Object IO.FileSystemWatcher $folder, $filter
$fsw.IncludeSubdirectories = $true
$fsw.NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

    $name = $Event.SourceEventArgs.Name
    $path = $Event.SourceEventArgs.FullPath

Out-File -FilePath c:\path\to\file\outlog.txt -Append -InputObject "$path"

    $title = $path
    Add-Type -AssemblyName System.Web
    $title = [System.Web.HttpUtility]::UrlEncode($title)
    $data =  "type=note&title=" + $title + "&body=body"

    $webclient = new-object System.Net.WebClient
    $webclient.Credentials = new-object System.Net.NetworkCredential($user, "")

Out-File -FilePath c:\path\to\file\outlog.txt -Append -InputObject "$data"

    $result = $webclient.UploadString($url, "POST", $data)

Out-File -FilePath c:\path\to\file\outlog.txt -Append -InputObject "$result"

}
#Unregister-Event FileCreated
检查脚本时,会写入一个
outlog.txt
文件,但只会写入前两条消息,并且不会提交通知

当我手动启动
uploadstring

$user = "pushbullet_token"
$url = "https://api.pushbullet.com/v2/pushes"
$data = "type=note&title=title&body=body"
$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($user, "")
$result = $webclient.UploadString($url, "POST", $data)

工作正常。

全局变量
$url
在事件处理程序脚本块中不可用。按如下方式更改Register ObjectEvent:

$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -MessageData $url -Action {
    $name = $Event.SourceEventArgs.Name
    $path = $Event.SourceEventArgs.FullPath
    $url  = $Event.MessageData
    ...
}

在事件处理程序中对$webclient.UploadString(…)的调用引发了一个异常,该异常终止了它所运行的EventJob上下文。您可以通过键入以下内容进行验证:

Get-Job 
寻找失败的工作。然后,您可以对失败的作业执行Receive作业以获取错误消息。这可能是身份验证错误。通过放置有效的身份验证令牌,我可以使您的代码正常工作

如果您想让事件处理程序在发生错误的情况下继续运行,则必须使用try/catch,例如:

$result = try { $webclient.UploadString($url, "POST", $data) } 
          catch { $_.Exception.InnerException.Response }