Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added credentials to get-url
  • Loading branch information
chaliy committed Nov 25, 2011
1 parent c6d551f commit f2c36f2
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions PsUrl/PsUrl.psm1
Expand Up @@ -7,12 +7,21 @@ function Get-Url {
Param(
[Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Mandatory=$true, Position=0)]
[String]$Url,
[String]$ToFile
[String]$ToFile,
[Management.Automation.PSCredential]$Credential
)
$client = (New-Object Net.WebClient)
if ($Credential){
$ntwCred = $Credential.GetNetworkCredential()
$client.Credentials = $ntwCred
$auth = "Basic " + [Convert]::ToBase64String([Text.Encoding]::Default.GetBytes($ntwCred.UserName + ":" + $ntwCred.Password))
$client.Headers.Add("Authorization", $auth)
}

if ($ToFile -ne ""){
(New-Object System.Net.WebClient).DownloadFile($Url, $ToFile)
$client.DownloadFile($Url, $ToFile)
} else {
(New-Object System.Net.WebClient).DownloadString($Url)
$client.DownloadString($Url)
}
<#
.Synopsis
Expand Down Expand Up @@ -40,7 +49,8 @@ Param(
[HashTable]$Data,
[String]$Content,
[TimeSpan]$Timeout = [TimeSpan]::FromMinutes(1),
[Management.Automation.PSCredential]$Credential
[Management.Automation.PSCredential]$Credential,
[String]$ContentType
)


Expand All @@ -55,6 +65,10 @@ Param(
$req.Credentials = $ntwCred
$req.PreAuthenticate = $true
}

if ($ContentType -ne ""){
$req.ContentType = $ContentType
}

if ($Content -ne ""){
$reqStream = $req.GetRequestStream()
Expand Down Expand Up @@ -108,6 +122,8 @@ Param(
Hashtable of the data to post.
.Parameter Timeout
Optional timeout value, by default timeout is 1 minute.
.Parameter ContentType
Adds Content-Type header to request
.Example
Write-Url http://chaliy.name -Data @{"Foo" = "Bar" }
Expand Down

0 comments on commit f2c36f2

Please sign in to comment.