Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum Running ProjectWise PowerShell Commands through Visual Studio
    • Sign In

    • State Verified Answer
    • Replies 4 replies
    • Subscribers 65 subscribers
    • Views 1276 views
    • Users 0 members are here
    • Visual Studio
    • powershell

    Running ProjectWise PowerShell Commands through Visual Studio

    Robert Golding
    Offline Robert Golding over 1 year ago

    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.

    Try
                Dim runspace As Runspace = RunspaceFactory.CreateRunspace()
                runspace.Open()
                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
                runspace.Close()
                Dim stringbuilder As StringBuilder = New StringBuilder()
                For Each ps As PSObject In results
                    stringbuilder.AppendLine(ps.ToString())
                Next
                Return stringbuilder.ToString
            Catch ex As Exception
                MsgBox("Error in Running Powershell Script" & Environment.NewLine & Environment.NewLine & ex.Message, MsgBoxStyle.Critical)
                End
            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!

    Thanks!

    • Sign in to reply
    • Cancel

    Top Replies

    • MWBSI
      MWBSI Mon, May 2 2022 4:54 PM +1 verified
      Robert, I got the following Visual Basic .NET code to run after adding a reference to system.windows.forms from the C:\Windows\Microsoft.NET\Framework64\v4.0.30319 folder: Using MyRunSpace As Runspace…
    • MWBSI
      +1 MWBSI Mon, May 2 2022 4:54 PM

      Robert, 

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

      Using MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
      MyRunSpace.Open()
      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

      MyRunSpace.Close()

      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 

      • Cancel
      • Vote Up +1 Vote Down
      • Sign in to reply
      • Reject Answer
      • Cancel
    • Robert Golding
      0 Offline Robert Golding Wed, May 4 2022 9:47 PM in reply to MWBSI

      Thanks Mark, works perfectly!

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Robert Golding
      0 Offline Robert Golding Mon, Jun 13 2022 2:59 AM

      Am having an issue which is semi related to this thread so I thought I would ask here rather than create a new post.

      When running the below, I'm getting an error stating i'm not logged into the Datasource.

      The code to run the script is the exact same that works on the original program listed in the post above, so I don't believe the error is correct, i believe its a problem with the script as if i comment out the line 

      MyPipeline.Commands.AddScript("$pwFolders = Get-PWFolders -FolderID 871907")

      I get a different error related to a SQL error which i think i can fix.

      MyPipeline.Commands.AddScript("Set-ExecutionPolicy RemoteSigned -force")
      MyPipeline.Commands.AddScript("Import-Module pwps_dab -force")
      MyPipeline.Commands.AddScript("New-pwlogin -BentleyIMS -DatasourceName 'perpw1.com.au:Per_Pwise_new'")
      MyPipeline.Commands.AddScript("$PwFolders = Get-PWFolders -FolderID 871907")
      MyPipeline.Commands.AddScript("$ProjectIDs = $pwFolders.ProjectID -join ','")
      MyPipeline.Commands.AddScript("$SQLStr = SELECT o_docguid FROM dms_doc WHERE o_dmsstatus = 'O' AND o_projectno IN ($ProjectIDs)")
      MyPipeline.Commands.AddScript("$SQLReturned = Select-PWSQL -SQLSelectStatement $SQLStr")
      MyPipeline.Commands.Add("Out-String")
      
      Dim results As Collection(Of PSObject) = MyPipeline.Invoke()

      I have run the line in question in the PowerShell ISE and it works so i'm confused as to what could be wrong with the script? Am i missing something? 

      Thanks in advance!

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Sean Duphily
      0 Offline Sean Duphily Wed, Jun 22 2022 5:02 PM in reply to Robert Golding

      I'm actually getting the same type of error.  It seems as if the following line is not recognizing the current Connection Client user:

      MyPipeline.Commands.AddScript("new-pwlogin -BentleyIMS -DatasourceName 'deldot-pw.bentley.com:deldot-pw-01'")

      When I run that line, the response I get when running inside of Visual Studio is 'False', however when run inside of PowerShell, it comes back as 'True'.  Maybe a guru here can explain what may be missing or setup incorrectly with the project to recognize the current user logged into the CC.

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies