Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8841853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:49:19+00:00 2026-06-14T10:49:19+00:00

So I’m going round in circles trying to get this to work, I’ve been

  • 0

So I’m going round in circles trying to get this to work, I’ve been trying for two days and I just can’t figure it out.

I have the following vb function that takes a created powershell script, and should run it in powershell. Everything works fine, until the point at which the command pipeline is invoked. At this point, no commands run.

As you can see, I have tried to add the Microsoft.Exchange.Management.PowerShell.E2010 snapin to the runspace, it didn’t like that at all stating something along the lines of the snapin didnt exist (which it does), and also when I run the code as shown, no commands are recognised as valid. I even added the specific command “Add-PSSnapin” to try and load any Exchange snapins, but it states that “Add-PSSnapin” is not recognised as a valid command.

If I pause the program just before the commands are involked, I can see every command within the pipeline, in the correct format. If I copy and paste the command text in the pipeline directly into a powershell window, it runs fine.

My code is below, any suggestions welcome.

edit: I have also tried adding the line “Add-PSSnapin Ex” (with an asterisk each side of Ex – I cant figure the formatting out on this, sorry)

to try and load the Exchange PS Snapins as the first thing the script would run (opposed to setting this up in the runspace) but no luck

Private Function scriptRunner(ByVal scripttorun As String) As String

    Dim initial As InitialSessionState = InitialSessionState.CreateDefault()
    Dim result As String = ""
    Dim lineFromScript As String = ""
    Dim reader As New StreamReader(tempScript)

    Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
    Dim snapInException As New PSSnapInException

    Dim strUserName As String = "DOMAIN\USER"
    Dim strPassword As String = "PASSWORD"
    Dim SecuredPSWD As New System.Security.SecureString()
    For Each character As Char In strPassword
        SecuredPSWD.AppendChar(character)
    Next

    Dim wsmConnectionInfo As WSManConnectionInfo
    Dim strSystemURI As String = "http://SERVER.DOMAIN/powershell?serializationLevel=Full"
    Dim strShellURI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
    Dim powerShellCredentials As PSCredential = New PSCredential(strUserName, SecuredPSWD)
    wsmConnectionInfo = New WSManConnectionInfo(New Uri(strSystemURI), strShellURI, powerShellCredentials)
    Dim runspace As Runspace = RunspaceFactory.CreateRunspace(wsmConnectionInfo)
    Runspace.Open()
    ' runspace.RunspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)


    Dim pipeLine As Pipeline = runspace.CreatePipeline()

    Dim command As Command = New Command("")
    ' TEST >> pipeLine.Commands.Add("Add-PSSnapin *Ex*")
    Do While reader.Peek() <> -1
        lineFromScript = Nothing
        lineFromScript = reader.ReadLine()
        pipeLine.Commands.Add(lineFromScript)
        'command.Parameters.Add(lineFromScript)
        'pipeLine.Commands.Add(command)

    Loop

    '' Run the contents of the pipeline
    Dim psObjCollection As Collection(Of PSObject) = pipeLine.Invoke()

    runspace.Close()
    runspace.Dispose()

    Return ""
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:49:20+00:00Added an answer on June 14, 2026 at 10:49 am

    I ended up working around the problem rather than fixing it.

    I moved the script code into the vb.net application, and wrote each line to a file, i.e.

            writer.WriteLine("Add-PSSnapin *Ex*")
    

    Then I loaded the script through PowerShell as an application;

                Dim exeStartInfo As System.Diagnostics.ProcessStartInfo
            Dim exeStart As New System.Diagnostics.Process
    
            exeStartInfo = New System.Diagnostics.ProcessStartInfo("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
            exeStartInfo.Arguments = ("-command work\scriptbuilder.ps1")
            exeStartInfo.WorkingDirectory = "C:\ExchangeManager\"
            exeStartInfo.UseShellExecute = False
            exeStart.StartInfo = exeStartInfo
            exeStart.Start()
            exeStart.Close()
    

    Not ideal but it got the job done.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.