Author Posts

September 30, 2017 at 4:01 pm

I have a PowerShell script that I can call from another script with this and it works.

Cls
D:\SQLBackups\BkUpAllDBwithDate -Instan .\sqlexpress

I now want to call the same script from VS 2017 Win 10 using VB.net. I have tried many iterations of the following to no avail.
I am not well versed in PS so please bear with me.

Dim myProcess As Process = Process.Start("powershell.exe", "-noexit -file D:\BkUpAllDBwithDate.ps1 -Instan .\sqlexpress ")

TIA

October 2, 2017 at 7:10 am

Is the path correct?
In your examples they differ.

October 2, 2017 at 1:24 pm

I stared at that for hours and did not catch that. TY
Now I have something else happening but I will try and chase that down.
Is there a way to trap the PowerShell Error?
Thank you for the help.
TIA

October 2, 2017 at 1:51 pm

you could add this to the code that you want to see the output from
Start-Transcript -path c:\test\transcript.txt -append
# your code here
Stop-Transcript

October 2, 2017 at 4:19 pm

Ok, that worked and said that the parameter is empty meaning I think my vb call is not correct so will try to find what is wrong.
Thank you again for the help.

October 2, 2017 at 4:52 pm

This is the error trapped.

PS>TerminatingError(Get-SqlDatabase): "Cannot validate argument on parameter 'ServerInstance'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
Get-SqlDatabase : Cannot validate argument on parameter 'ServerInstance'. The argument is null or empty. Provide an
argument that is not null or empty, and then try the command again.
At D:\SQLBackUps\BkUpAllDBwithDate.ps1:7 char:33
+ Get-SqlDatabase -ServerInstance $Instan |
+ ~~~~~~~

I guess that means either my VB.Net call is wrong or my Parameter statement in PowerShell is not correct. According to every example I can find they are both correct but any help would be appreciated.

October 5, 2017 at 10:03 pm

Haven't used VB.Net but maybe as a troubleshooting step, test with something simple.
E.g. run notepad instead of powershell and pass in a path to a textfile.
If that works then it's most likely something that doesn't get correctly passed to/from e.g. the argument section.
E.g. test with just the filename and not the arguments.
You could do that by adding default values in your script as an example.
If that works then you know for sure that it's the parameters that is causing the issue or something related to it.

I guess another step would be to set at breakpoint at that line and examine what it looks like at runtime.
Another option would be to go through an intermediate step and use ProcessStartInfo and see if that changes anything.
You've also a lot more options using ProcessStartInfo.

October 5, 2017 at 10:56 pm

I have tried some of what you mentioned. Particularly the breakpoint but was unable to figure out what happened in the PowerShell script.
I tried setting a breakpoint in the script but could not get it to work.
Because I needed to get the project completed I just created stored procedures and made calls to them. Worked.
I will try some of your other ideas!
TY