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!
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
Mark Weisman | Bentley Systems
Answer Verified By: Robert Golding
Thanks Mark, works perfectly!
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!
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.