How Do you Pass a Value From VB.Net

This topic contains 7 replies, has 3 voices, and was last updated by  Terry B 1 week, 4 days ago.

  • Author
    Posts
  • #81011

    Terry B
    Participant

    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

  • #81056

    Fredrik Kacsmarck
    Participant

    Is the path correct?
    In your examples they differ.

    • #81074

      Terry B
      Participant

      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

  • #81079

    Simon B
    Participant

    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

    • #81088

      Terry B
      Participant

      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.

    • #81089

      Terry B
      Participant

      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.

  • #81427

    Fredrik Kacsmarck
    Participant

    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.

    • #81430

      Terry B
      Participant

      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

You must be logged in to reply to this topic.