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