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.

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!

Parents Reply Children
No Data