Running ProjectWise PowerShell Commands through Visual Studio

I'm trying to create a Program in Visual Studio to run some PowerShell Commands. The Commands work fine when running through the PowerShell ISE, but have issues when trying to run through VS.

The code for VS is below.

            Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
            Dim pipeline As Pipeline = runspace.CreatePipeline
            'pipeline.Commands.Add("Import-Module pwps_dab")
            pipeline.Commands.AddScript("New-PWLogin -BentleyIMS")
            pipeline.Commands.AddScript("$PWUser = Get-PWUsersByMatch -UserName " & sUserToClean)
            pipeline.Commands.AddScript("Remove-PWDocumentCopyOutLocations -InputUsers $PWUser ")
            pipeline.Commands.AddScript("Get-PWDocumentsBySearchExtended -CheckOutUsers $Username | CheckIn-PWDocumentsOrFree -Free ")
            pipeline.Commands.AddScript("Remove-PWUserByMatch -UserNameForItems " & sNewDocumentOwner & " -InputUsers $PWUser")
            Dim results As Collection(Of PSObject) = pipeline.Invoke
            Dim stringbuilder As StringBuilder = New StringBuilder()
            For Each ps As PSObject In results
            Return stringbuilder.ToString
        Catch ex As Exception
            MsgBox("Error in Running Powershell Script" & Environment.NewLine & Environment.NewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

The error message im getting is below.

If i run the Import-Module as another command above the New_PWlogin (you can see its currently commented out) it says this

Im new to running PowerShell commands through VS, but have done lots of VS projects previously and not too bad with PowerShell but im pretty stumped by this one!


  • Robert, 

    I got the following Visual Basic .NET code to run after adding a reference to from the C:\Windows\Microsoft.NET\Framework64\v4.0.30319 folder:

    Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
    Using MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
    MyPipeline.Commands.AddScript("Set-ExecutionPolicy RemoteSigned -force")
    MyPipeline.Commands.AddScript("Import-Module pwps_dab -force")
    MyPipeline.Commands.AddScript("new-pwlogin -BentleyIMS -DatasourceName 'ServerName:DatasourceName'")
    Dim results = MyPipeline.Invoke()
    'Do something with the results
    End Using


    Please note that I am running the Import-Module command with .AddScript method, not the .Add method.  Also note the use of the -DatasourceName parameter with the new-pwlogin cmdlet.  It is important to provide sufficient information so that the login dialog box, or no other dialog boxes for that matter, pop up during the running of your script.

    You may want to verify that each line works with 64 bit PowerShell ISE before using them in your VB .NET application.   

    If you are new to PowerShell, running a script from a .NET application is kind of an advanced place to start.  But I'm sure there are plenty of .NET gurus in this group that can help ya Slight smile

    Mark Weisman |  Bentley Systems

    Answer Verified By: Robert Golding 

Reply Children
No Data