用户PowerShell脚本发布到URL? [英] User powershell script to post to URL?

查看:67
本文介绍了用户PowerShell脚本发布到URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了以下 Python 脚本:

I have inherited the following Python script:

import urllib2
a = urllib2.urlopen('http://mysite/mypage.aspx?action=dosomething')
a.read()
a.close()

我想用powershell脚本替换它.我用谷歌搜索了一下,但我找到的所有东西都会启动一个浏览器窗口.

and I would like to replace it with a powershell script. I have googled a little but everything I find launches a browser window.

这个脚本将被安排,所以如果可能的话,我只想发布并忘记"?

This script is going to be scheduled, so I'd like to just "post and forget" if possible?

非常感谢收到任何帮助:)

Any help very gratefully received :)

推荐答案

这是我不久前写的一个脚本,它展示了如何使用地理标签发布推文.这使用 WebClient.

Here is a script I wrote a while back that shows how to post tweets with Geotags. This uses WebClient.

http://www.ravichaganti.com/blog/?p=979

在此粘贴代码以供参考.

Pasting the code here for easy reference.

Function ByPass-Proxy {
    param ([string]$url)
    $webClient.Proxy.IsBypassed($url)
}

Function Get-GeoCoordinates {
    param ([String]$location)
    $baseURL = "http://maps.google.com/maps/geo?q="
    $apiKey = "Your API Key"
    $url = $baseURL + $location + "&output=xml&sensor=false&key=" + $apiKey
    $locCoords = (([xml]($WebClient.DownloadString($url))).kml.Response.Placemark.Point.coordinates)
    return $locCoords
}

Function Send-Tweet {
    param ([string]$Tweet,[string]$location)
    $geoCoord = Get-GeoCoordinates $location
    $long = $geoCoord.Split(",")[0]
    $lat = $geoCoord.Split(",")[1]
    $TwitURL = "http://twitter.com/statuses/update.xml"
    $WebClient.Credentials = $TwitCredentials
    #$str = [System.Text.Encoding]::UTF8.GetBytes( "status="  + $Tweet + "&lat=" + $lat + "&long=" + $long )
    $str = "status="  + $Tweet + "&lat=" + $lat + "&long=" + $long
    [System.Net.ServicePointManager]::Expect100Continue = $false
    $response = $WebClient.UploadString($TwitURL,$str)
    $response
}

function Get-Credential { 
## Grabbed this from http://poshcode.org/1480

[CmdletBinding(DefaultParameterSetName="Better")]
PARAM(
   [Parameter(Position=1,Mandatory=$false)]
   [Alias("Credential")]
   [PSObject]$UserName=$null,
   [Parameter(Position=2,Mandatory=$false)]
   [string]$Title=$null,
   [Parameter(Position=3,Mandatory=$false)]
   [string]$Message=$null,
   [Parameter(Position=4,Mandatory=$false)]
   [string]$Domain=$null,
   [Parameter(Mandatory=$false)]
   [switch]$GenericCredentials,
   [Parameter(Mandatory=$false)]
   [switch]$Inline
)

PROCESS {
   if( $UserName -is [System.Management.Automation.PSCredential]) {
      return $UserName
   } elseif($UserName -ne $null) {
      $UserName = $UserName.ToString()
   }

   if($Inline) {
      if($Title)    { Write-Host $Title }
      if($Message)  { Write-Host $Message }
      if($Domain) { 
         if($UserName -and $UserName -notmatch "[@\\]") { 
            $UserName = "${Domain}\${UserName}"
         }
      }
      if(!$UserName) {
         $UserName = Read-Host "User"
         if(($Domain -OR !$GenericCredentials) -and $UserName -notmatch "[@\\]") {
            $UserName = "${Domain}\${UserName}"
         }
      }
      return New-Object System.Management.Automation.PSCredential $UserName,$(Read-Host "Password for user $UserName" -AsSecureString)
   }
   if($GenericCredentials) { $Credential = "Generic" } else { $Credential = "Domain" }

   ## Now call the Host.UI method ... if they don't have one, we'll die, yay.
   ## BugBug? PowerShell.exe disregards the last parameter
   $Host.UI.PromptForCredential($Title, $Message, $UserName, $Domain, $Credential,"Default")
}
}

$global:webClient = New-Object System.Net.WebClient
$global:TwitCredentials = Get-Credential -title "Twitter Credentials" -message "Please enter your Twitter username/password"
If (!(ByPass-Proxy "http://www.twitter.com")) {
    $global:Webclient.proxy.Credentials = Get-Credential -title "Proxy Credentials" -message "Please enter username/password for Proxy authentication"
}

这篇关于用户PowerShell脚本发布到URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆