Win PowerShell Script - HTTP request - help please
#1
Edit: Figured out how to send the request but now I am getting a 401.

When I put the Same URL string in FireFox I get a result of "OK" So it seems to work fine there

Thoughts?

Code:
function updateXBMC([string]$showName){
    $xbmcAddress = "http://media:[email protected]:8080"
    $sourcePath = "smb://SERVER/TV Shows/TV Shows"

    $command = "/xbmcCmds/xbmcHttp?command=ExecBuiltIn(UpdateLibrary(video,`"$sourcePath/$showName`"))"
    $fullURL = "$xbmcAddress$command"
    $fullURL = $($($fullURL.Replace(" ","%20")).Replace("(","%28")).Replace(")","%29")
    Write-Host $fullURL
    
    $reQUpdateLib = new-object net.webclient
    #Start-Sleep -s 5
    $reSUpdateLib = $reQUpdateLib.DownloadString($fullURL)
    Write-Host "--------Result Start---------------"
    Write-Host $reSUpdateLib
    Write-Host "--------Result End-----------------"
    
    
    $command = "/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(uTorrent:%20Finished%20download,`"$($showName.Replace(" ","%20"))`"))"
    $fullURL = "$xbmcAddress$command"
    $fullURL = $fullURL.Replace(" ","%20")
    Write-Host $fullURL
    $reQNotify = new-object net.webclient

Write-Host "--------Result Start---------------"
    $reSNotify = $reQNotify.DownloadString("$fullURL")
    


}



-------------------------------------------------------------------------------------------------------------------

Note: This seemed the best place to ask


I have writen a script to auto extract my TV shows (yes I know XBMC plays rars). So after getting that working I wanted to add to it and have it update the XBMC Library automatically, and then if there is a way confirm that XBMC received the request. However I need help with the powerShell syntax.

Here is what I have so far. I got the actual URL request string from a script I found while googling.

Code:
function updateXBMC ($showName,$showDirPath){
$xbmcAddress = "http://user:[email protected]:8080"
$command = "/xbmcCmds/xbmcHttp?ExecBuiltIn(UpdateLibrary(video," + $showDirPath + "/))"



WebRequest requestUpdateLib = WebRequest.Create($xbmcAddress $command)
#WebRequest requestNotify = WebRequest.Create($xbmcAddress "/xbmcCmds/xbmcHttp?command=ExecBuiltIn(Notification(uTorrent:%20Finished%20download," + $showName+ "))")
}

How do i send the request? I have been googling for an hour and can't find anything explaining this class fully.


Any help is much appreciated. Smile

Reply
#2
*Bump for ETA*
Reply
#3
Error 401 is authentication error.

Powershell use .net behind and using login:pass@xxx is not really well handled.

You should use things like

Code:
$w = New-Object net.webclient
  if ($user) {
    write-debug "setting credentials, user name:  $user"
    $w.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  }
  $w.DownloadFile($url, $filePath)
Reply
#4
(2012-07-27, 19:46)Tolriq Wrote: Error 401 is authentication error.

Powershell use .net behind and using login:pass@xxx is not really well handled.

You should use things like

Code:
$w = New-Object net.webclient
  if ($user) {
    write-debug "setting credentials, user name:  $user"
    $w.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  }
  $w.DownloadFile($url, $filePath)
Yeah I know it was an authentication error

*bangs head into desk* I tried doing it like that like, 6 different ways just not exactly like that.

It works now, thanks man!!. Smile

Reply
#5
(2012-07-27, 21:36)theColonel26 Wrote:
(2012-07-27, 19:46)Tolriq Wrote: Error 401 is authentication error.

Powershell use .net behind and using login:pass@xxx is not really well handled.

You should use things like

Code:
$w = New-Object net.webclient
  if ($user) {
    write-debug "setting credentials, user name:  $user"
    $w.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  }
  $w.DownloadFile($url, $filePath)
Yeah I know it was an authentication error

*bangs head into desk* I tried doing it like that like, 6 different ways just not exactly like that.

It works now, thanks man!!. Smile
Getting confused a bit by the few posts on this thread, could you share a more contiguous example?
The first part I dont think you'd want to replace the whole command strings () instead of just url encoding the command part?

Not sure how this authentication part plays into to all? Is it just able to read the logged on user's passed thru credentials?
I tried testing this part and couldn't see it doing anything but uncertain if I did a valid test.

I have a similar script but mostly done in command line and I'd like to learn Powershell more and think converting it would be a great learning opportunity, plus could leverage cool Powershell things, and like that PoSH wouldnt require a 3rd party to make the http request like I have to do in batch.
Search first, provide details and keep forums clean. Mark things solved, to close them out and acknowledge helpful volunteers who share. If I have helped, click the plus button.
Reply
#6
(2012-08-23, 08:18)transcender Wrote:
(2012-07-27, 21:36)theColonel26 Wrote:
(2012-07-27, 19:46)Tolriq Wrote: Error 401 is authentication error.

Powershell use .net behind and using login:pass@xxx is not really well handled.

You should use things like

Code:
$w = New-Object net.webclient
  if ($user) {
    write-debug "setting credentials, user name:  $user"
    $w.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  }
  $w.DownloadFile($url, $filePath)
Yeah I know it was an authentication error

*bangs head into desk* I tried doing it like that, like 6 different ways just not exactly like that.

It works now, thanks man!!. Smile
Getting confused a bit by the few posts on this thread, could you share a more contiguous example?
The first part I dont think you'd want to replace the whole command strings () instead of just url encoding the command part?

Not sure how this authentication part plays into to all? Is it just able to read the logged on user's passed thru credentials?
I tried testing this part and couldn't see it doing anything but uncertain if I did a valid test.

I have a similar script but mostly done in command line and I'd like to learn Powershell more and think converting it would be a great learning opportunity, plus could leverage cool Powershell things, and like that PoSH wouldnt require a 3rd party to make the http request like I have to do in batch.

With "authentication" we are referring to the user name and password that you have set for XBMC http access.

I would definitively recommend you convert your script to powershell, and in the process learn PS. I love PS and I am never going back to batch.

remember when you are launch a PS from another program like uTorrent to use this flag "powershell.exe -executionpolicy RemoteSigned"
Reply

Logout Mark Read Team Forum Stats Members Help
PowerShell Script - HTTP request - help please1