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
  • 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!

Reply
  • 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!

Children