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…

    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