Runspace opening multiple remote powershell connections and not closing them

Hello,
I hope this has not already been asked as I have been searching for good amount of time.
I have an asp.net site that runs some remote PowerShell scripts. Below is the code I Pass the script to. Everything works fine except it does not close all connections.
When I do a netstat -ao I see that each time this is called it leaves 2 sessions opened to the remote server. I go on the remote server and do a Get-PSSession and do not see any of the sessions opened. When stepping through the code, "pRunspace.Open()"
creates 2 connections to the destination server, Then the "psresult = pShell.Invoke" creates another open connection. The Dispose() only closes one of the 3 connections. Upping the amount of connections or changing the idle time on the receiving
server is not an option as I have already increased/lowered them and it still gets maxed connections. . iisreset clears the open connections. I have tried .disconnect() but the destination does not have PowerShell 3.0. Am I missing something stupid here? Is
there a better way to go about this. 
Any help is appreciated.
Thank you,
Mike
Public Shared Function RunRemotePSScript(ByVal script As PSCommand, ByVal ServerURL As String, ByVal shellurl As String) As String
Dim pCredential As PSCredential
Dim pConnectionInfo As WSManConnectionInfo
Dim pRunspace As Runspace
Dim pShell As PowerShell
Dim securepass As New System.Security.SecureString
Dim serverUri As New Uri(ServerURL)
Dim psresult As New System.Collections.ObjectModel.Collection(Of PSObject)
For Each c In "Password"
securepass.AppendChar(c)
Next
'-- set credentials
pCredential = New PSCredential("Domain\username", securepass)
'-- set connection info
pConnectionInfo = New WSManConnectionInfo(serverUri, shellurl, pCredential)
'-- create remote runspace
pRunspace = RunspaceFactory.CreateRunspace(pConnectionInfo)
pRunspace.Open()
'-- create powershell
pShell = PowerShell.Create
pShell.Runspace = pRunspace
'-- add command to powershell
pShell.Commands = script
Try
'-- invoke the powershell
psresult = pShell.Invoke
pShell.Dispose()
pRunspace.Close()
pRunspace.Dispose()
Dim sb As String = ""
For Each result As PSObject In psresult
sb &= result.ToString
Next
Return sb
pShell.Dispose()
pRunspace.Dispose()
pRunspace.Close()
pRunspace = Nothing
Catch ex As Exception
If Not IsNothing(pShell) Then
pShell.Dispose()
End If
If Not IsNothing(pRunspace) Then
pRunspace.Dispose()
pRunspace.Close()
End If
Return Err.Description
End Try
End Function
That is the netstat output I get after running through the function one time.
c:\Windows\system32>netstat -ao |findstr Server1
  TCP    MycomptuerIP:49307    RemoteServer1:http           ESTABLISHED     7632
  TCP    MycomptuerIP:49312    RemoteServer:http           ESTABLISHED     7632
Here is an example of how i call the function. 
Public Shared Function Set_UM_PinNumber(ByVal UserSamAccout As String, ByVal PinNumber As String) As String
Try
Dim output As String = ""
Dim psh As New PSCommand
psh.AddCommand("Set-UMMailboxPIN")
psh.AddParameter("Identity", UserSamAccout)
psh.AddParameter("Pin", (PinNumber))
psh.AddParameter("PinExpired", 0)
output = AD.ADWrapper.RunRemotePSScript(psh, "http://Server.domain.com/powershell?serializationLevel=Full", "http://schemas.microsoft.com/powershell/Microsoft.Exchange")
If output = "" Then
Return "Pin Updated"
Else
Return output
End If
Catch ex As Exception
Return Err.Description
End Try
End Function

Fred, 
Thank you for taking time to reply. From what I got and tested "The EndInvoke method
is used to retrieve the results from the completed runspace" Then you clean up after that.. Close is supposed to close all the connections. But I could be very wrong. I am self taught and
truly could be going at this all wrong. This was working in the past ( could be that more people are using it now that I am seeing the connections full). I really hope someone can help me or direct me in the right direction as this is causing a lot of problem
for me.
Mike 

Similar Messages

  • How do I configure Firefox to open multiple pages on startup and not set my homepage to open mutliple tabs?

    So I know how to set my homepage to open multiple tabs, however it has the undesired effect of making it so those tabs are opened every time I hit the homepage button. Is there a way to set Firefox to open multiple pages on start-up, while not actually setting the homepage to open multiple tabs?

    You can edit a copy of the Firefox desktop shortcut and add the links to the target field to make Firefox open those pages when you use this shortcut.
    See:
    *https://developer.mozilla.org/Command_Line_Options
    If you run out of space (i.e. exceed the maximum length) then use a CMD file with the start command.
    *start "" "<path_to_firefox\firefox.exe>" URL1 URL2
    *https://technet.microsoft.com/en-us/library/bb491005.aspx

  • Multiple remote debugger connections from JDev

    Hello,
    I'm running JDev Studio Edition Version 11.1.2.3.0
    I need to debug an application running on a 2 node WLS Cluster. I have the two servers running in debug mode listening on different hosts/ports.
    In JDev I click the debug button twice and open a connection to each server in turn, however I suspect that the last connection made may be the only one that's active?
    Can somebody please clarify whether it's possible to open multiple remote debugger connections in JDev?
    Many thanks.

    Hello,
    I'm running JDev Studio Edition Version 11.1.2.3.0
    I need to debug an application running on a 2 node WLS Cluster. I have the two servers running in debug mode listening on different hosts/ports.
    In JDev I click the debug button twice and open a connection to each server in turn, however I suspect that the last connection made may be the only one that's active?
    Can somebody please clarify whether it's possible to open multiple remote debugger connections in JDev?
    Many thanks.

  • Can I open directly to first page, and not the title/TOC?

    Can I open directly to first page, and not the title/TOC? We're using HTML5 content for multiple platforms. It works fine, but takes three steps to get to it. We'd like for the book to start immediately with our imported widget. The current process is clunky for the user. Thank you.

    Coincidentally i use Google also ..but i wouldn't like to change it...thanks.. your answer it's being helpful. I solved the problem by changing my Firefox preferences : 1.-PRIVACY 2.- Remember history mode.
    Anyway i still think that shouldn't work this way when you are in "Never remember history " mode.

  • How do I get bookmarks on the toolbar to open in the current tab, and not open in a new tab?

    how do I get bookmarks on the toolbar to open in the current tab, and not open in a new tab?

    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

  • I want to delete a few contacts, apps, pics etc from my iphone, but when I delete and connect to my laptop they sync and come back again. How can I delete those items and not get them back when I connect my iphone to my laptop?

    I want to delete a few contacts, apps, pics etc from my iphone, but when I delete and connect to my laptop they sync and come back again. How can I delete those items and not get them back when I connect my iphone to my laptop?
    Also where exactly on the laptop is teh data stored.
    Thank you!!

    go to 'Edit' in the menu bar. Then choose 'Preferences'.
    you'll see a pop-up and there's a tab called 'Devices' there. If you click that, you will be able to check a box that says 'prevent automatic syncing for iPods, iPads and iPhones' or something like that.
    Hope this helps
    NB: if you don't even see the menu bar that has options like 'File, Edit, View, etc', first click the black/white square button on the top left of your screen, and then click 'show menu bar'. It should pop right up.
    Good luck

  • Accidentally  abandoned and not closed connections

    Tome cat datasource can close abandoned connections. can weblogic data source implement
    this? If not how can webligic connection pool deal with these accidentally abandoned
    and not closed connections?

    zorro wrote:
    connection is retrived by this function.
    public Connection createConnection()throws Exception{
    try{
    InitialContext ictx=new InitialContext();
    DataSource ds=(DataSource)ictx.lookup("jdbc/Oracle");
    return ds.getConnection();
    catch(NamingException e){
    logger.error("failed to get connection:"+e);
    return null;
    And most of us use it in the way as follow. But I am not sure that there is no
    connection leak in the code of the other programmers. If connection leak steadily
    and seldom occur, what will happan when the weblogic server works for a long time?
    I know that tomcat datasource handal the connection leak very well.The code below is correct. As soon as GC finds an abandoned connection, it will
    be closed and returned to the pool. You can also turn on leak checking, which will
    print out where the leaked connection was reserved, to help you find the code tp fix.
    Joe
    >
    >
    Connection conn=null;
    try{
    conn=createConnection();
    catch(Exception e ){
    finally{
    try{
    if(conn!=null)conn.close();
    catch(Exception e ){}

  • How do I get a numbers spread sheet to open where I left it and not start from the beginning every time I open it?

    How do I get a numbers spread sheet to open where I left it and not start from the beginning every time I open it?

    There is no built-in setting for redirecting a bookmark to a new tab.
    Is it possible that the computer which does that has an add-on such as the Tab Mix Plus extension installed? TMP has a setting for that (Events > Tab Opening).
    https://addons.mozilla.org/firefox/addon/tab-mix-plus/

  • Is it possible to download iOS  5 from one iMac and not get them totally wiped of apps and stuff. We have an iPad each and a communal email address and an iCloud address each. One iPad now is totally screwed up.

    Is it possible to download iOS  5 from one iMac on two ipads and not get them totally wiped of apps and stuff. We have an iPad each and a communal email address and an iCloud address each. One iPad now is totally screwed up.

    Thank you for your reply.
    I am somewhat new to iOS and OS having grown up with DOS and Windows. I am not familiar with IMAP and iCloud and I am not even sure which types of servers my various emails use.
    The problem I would anticipate with iCloud is that (I assume) it would limit my access to data (including mail) on the iCloud to when I happen to be logged onto the internet. I also assume(?) that large files (photos etc.) would require some time to download whenever I wish to access them from whichever device I happen to be using (I hope I am wrong about this).
    I have kept my devices to 500gb - 1TB for OS and 32gb - 128gb for iOS but since I have pretty slow WiFi (shared with DirecTV Whole Home and Netflix streaming) it is my impression that iCloud might be quite limited due to data transfer times.
    Perhaps a factor would be that I have 2 DSL lines and my main DSL line is connected to two wireless routers (to also provide WiFi in a metal building that blocks WiFi and Cellular signals)
    I may be totally wrong in my assumptions regarding iCloud. My mail is not so urgent that I need access to it instantly and I could wait until I had internet access or I can get it on my iPhone. My main concern with iCloud would be my data files (Word, Pages, Excel, Numbers etc.).
    On the other hand, if I wanted to keep my 10,000 emails that would take up a lot of iCloud memory and I don't know how to measure how much storage it would take or how to move Hotmail or even iCloud mail to iCloud.
    It may take me some time to figure this out.

  • HT1284 How can I backup word documents and not have them crowd my screen? Thanks.

    Hi. How can I backup word documents and not have them crowd my screen? I know, you guessed it. I'm a novice, but not ashamed!

    Please explain what you mean when you say backup Word documents crowds your screen.
    I don't see any connection between backup and your screen.
    Allan

  • How do I upload pictures from my iphone to a new computer and not lose them

    My laptop crashed so I bought a new computer and iTunes says if I sync my pictures they'll be replaced with what's on the new computer. There are no pictures on the new computer.  So how do I get them off my phone and not lose them too?

    Click here and follow the instructions. Once the photos are on your computer, put a copy of them onto an external drive.
    (118114)

  • Can you delete a text messages from your mac Book OSX 10.9.2 and not delete them from your iphone 5 running version 7.0.4

    c
    an you delete a text messages from your mac Book OSX 10.9.2 and not delete them from your iphone 5 running version 7.0.4

    Have you tried restarting or resetting your iPhone?
    Restart: Press On/Off button until the Slide to Power Off slider appears, select Slide to Power Off and, after It shuts down, press the On/Off button until the Apple logo appears.
    Reset: Press the Home and On/Off buttons at the same time and hold them until the Apple logo appears (about 10 seconds).
    Also consider deleting and reinstalling the Mail Account in question.

  • I want to have different headers in Pages. I want to be able to set them and not have them be the same. For example I want ii then iii and so on...How do I do that?

    I want to have different headers in Pages. I want to be able to set them and not have them be the same. For example I want ii then iii and so on...How do I do that?

    Hi BelleDesign,
    "Headers" is short for "Continuing Headers."
    Use a Header when you want recurring information on a series of pages in your document.
    If ii, iii, etc. are page numbers, you can place them in Headers using Insert > Page Number, then formatting the page number to lower case Roman numerals.
    For more on Headers and Footers, see Chapter 3, Working with document parts in the Pages '09 User Guide, available for download via the Help menu in Pages. Discussion of Headers and Footers starts on Page 54.
    Regards,
    Barry

  • My laptop was stolen and i have a new one, is there any way to move all the picture folders to the new computer and not lose them?

    My laptop was stolen and i have a new one, is there any way to move all the picture folders to the new computer and not lose them? They stole my external harddrive too so unfortunately if i lose these, i have no way to get them back?

    What randers4 said. HOWEVER, if the photos were taken with a higher resolution camera than the iPhone what you recover will not be as high a resolution as the originals.

  • Remote Desktop Connection and printing issues

    Hi, 
    I am using Windows 7 on my desktop and also connected to Windows Server 2008 R2 to run applications in Windows Server. However, I noted there is problem with the printer issues. Sometimes, when I tried to print some documents using applications
    in Windows Server with my local printer in my room connected to desktop, it won't work.
    Moreover, I need to go to Windows Server, Component Service to restart printer spooler so it will work again. This is a pain. 
    I also noticed there is about 30 seconds wait to process any document to print.
    Unlike if I print documents directly from my local desktop computer running Windows 7 and connected to local printer, it only takes seconds to print.
    My desktop is connected to Windows Server 2008 via local area network. I should not expect such a significant delay in printing. 
    I also wonder why printer spooler needs to be restarted when printer is not working.
    Can anyone advise me about this? Can Microsoft looks into this problem and release updates to fix the bug to improve the system?
    Thanks
    Regards,
    Clement

    Hi,
    Please check if you turn on this settings:
    You need to set this up just before you connect. The setting will be saved so that you won't need to change it the next time you connect.
    1. Open Remote Desktop Connection by clicking the Start button, clicking
    All Programs, clicking Accessories, and then clicking
    Remote Desktop Connection.
    2.Click Options, and then click the Local Resources
    tab.
    3.Under Local devices and resources, select the Printers check box.
    4.Click Connect to start the session.
    Make sure that you select your printer in the Print dialog box each time you print.
    Karen Hu
    TechNet Community Support

Maybe you are looking for

  • How do I open a page in new window using af:commandLink?

    Hi, I have a requirement where I need to open the page in a new window on clicking the af:commandLink. I could use af:goLink, but I cannot as I have to invoke the URL using http POST method. If I use af:goLink, all the parameters will be displayed in

  • Exporting list of tables to text files?

    Anyone have solution for exporting list of SQL tables to text files? My goal is to have a flexible/dynamic way to export sql tables to text files that is executed via a sql job. I plan on having a configuration table that has sqltablename, fieldstoex

  • Overdue report - how to

    Hello, I have date A and date B. In routine I calculated number of days (A-B). In Query Designer I want to create an overdue report. Menas: If result of calculated days = 1 -> One column should be named as "Overdue 1". How to?

  • FM to simulate posting of a FI document

    Hi Experts ,                                I want to know the function module using which we can simulate a finance doucment (To check if the document has all the data required to be posted ) in transaction FV60.                  I will be very grea

  • ORA-1653: nable to extend table

    Hi guys. Had this error in the alert.log file. Mon Jun 26 09:10:37 2006 ORA-1653: unable to extend table APPLSYS.WF_LOCAL_ROLES_STAGE by 4849950 in tablespace APPLSYSD The tablespace have three datafiles of which its both datafiles are only 40% full.