Results 1 to 6 of 6

Thread: [RESOLVED] VB.net execute powershell script

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Resolved [RESOLVED] VB.net execute powershell script

    Hello can someone help me how i can make from my .net winform to execute the powershell script as admin

    here is what i try, but also when open the window of powershell to display results what the code does.
    Code:
    Dim FileNamePath As String = My.Application.Info.DirectoryPath & "\Script.ps1"
        Dim processStartInfo As New ProcessStartInfo("powershell.exe") With {
            .Arguments = "-NoProfile -noexit -ExecutionPolicy Bypass -File " & FileNamePath,
            .UseShellExecute = False,
            .CreateNoWindow = False,
            .RedirectStandardError = True,
            .RedirectStandardOutput = True,
            .Verb = "runas",
            .WindowStyle = ProcessWindowStyle.Normal
        }
    
        Using p As New Process()
            p.StartInfo = processStartInfo
            p.Start()
            p.WaitForExit()
        End Using

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: VB.net execute powershell script

    Could you rephrase your question? Also, use Path.Combine to append a filename to a path. Also, if you must concatenate strings, you should use string interpolation.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: VB.net execute powershell script

    yeah i will clear more my question.
    So basically i have a file at some folder.
    So i need to execute the PowerShell to read that file and do his things what is inside the file .ps1.
    But i want to show in PowerShell window (Visible - Normal) and to display the results that are doing from the code.
    To run the PowerShell as administrator privilege's.
    As i read topics i understand that arguments should have "-NoProfile -noexit -ExecutionPolicy Bypass" to not have issue with running the command. (but the script will be run on administrator account on windows server 2019, so maybe i will not need them?? )
    that's it.

    even if i try directly to put path only in powershell start give me errors like:
    Install-WindowsFeature : The term 'Install-WindowsFeature' is not recognized as the name of a
    cmdlet, function, script file, or operable program.
    Last edited by luckydead; Oct 26th, 2022 at 07:43 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: VB.net execute powershell script

    found a small solution to my issue:
    Code:
    Dim combined As String = Path.Combine(My.Application.Info.DirectoryPath, Path.GetFileName("\Script.ps1"))
            Dim powerShellPath = "C:\Windows\SysNative\WindowsPowerShell\v1.0\powershell.exe"
            Process.Start(powerShellPath, "-noexit -file " & "" & combined & "" & " -Verb RunAs""")

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: VB.net execute powershell script

    You should try to avoid hardcoding the path for Windows. While it will usually be "C:\Windows", that doesn't have to be case. To be sure you use the correct path use https://learn.microsoft.com/en-us/do...e?view=net-7.0 to retrieve the "windir" environment variable.

    Also:
    "Dim powerShellPath" should be "Dim powerShellPath As String". Unless you really need it, I would turn "Option Infer" off.
    -"Process.Start(powerShellPath, $"-noexit -file ""{combined}"" -Verb RunAs""")" - See the difference? That's string interpolation.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: VB.net execute powershell script

    great hint advice for the start

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width