locked
Setting HttpContext.Current using Powershell RRS feed

  • Question

  • I am trying to create a fake HttpContext using Powershell and I am hitting a snag.  When I try to set the HttpContext.Current static property, nothing happens.  I don't get an error message, but the property remains null.

    I'm no poweshell expert and so I'm thinking I may be doing something fundamentally wrong.  Any ideas are welcome.  Here is the code I am trying:

     

     

     

    $sw = New-Object System.IO.StringWriter
    
    $resp = New-Object System.Web.HttpResponse $sw
    
    $req = New-Object System.Web.HttpRequest "", http://intranet.cbi, ""
    
    $htc = New-Object System.Web.HttpContext $req, $resp
    
    [System.Web.HttpContext]::Current = $htc
    

     

    However the Current property stays NULL as shown below:

     

    PS C:\Users\Administrator>  [System.Web.HttpContext]::Current -eq $NULL
    
    True
    

    By the way, this does work in C#


     

     


    http://jcapka.blogspot.com
    Friday, October 29, 2010 3:55 PM

Answers

  • Hi,

    I get the same problem when running your code line by line in powershell.exe console. But it works fine in PowerShell ISE console. It also works fine in a script, I think you can use it safely in a script.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Joe Capka Thursday, November 4, 2010 7:43 PM
    Tuesday, November 2, 2010 9:08 AM

All replies

  • Hi,

    I get the same problem when running your code line by line in powershell.exe console. But it works fine in PowerShell ISE console. It also works fine in a script, I think you can use it safely in a script.

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Joe Capka Thursday, November 4, 2010 7:43 PM
    Tuesday, November 2, 2010 9:08 AM
  • Thanks, I'll give that a shot.  Any idea why it won't work in the console? 
    http://jcapka.blogspot.com
    Tuesday, November 2, 2010 9:17 AM

  • I’m not sure but I guess it may be caused by different scope. BTW, what’s your test result?

    Thanks.


    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, November 4, 2010 9:06 AM
  • I have the same results as you, it works in PowerShell ISE.  Thanks!
    http://jcapka.blogspot.com
    Thursday, November 4, 2010 7:44 PM