none
New confusion on running powershell scripts RRS feed

  • Question

  • So I got one script working perfectly as I want it too. However my next power script doesn't work

    Below is the code that works.

     Dim objProcess As New Process()
            objProcess.StartInfo.Verb = "runas"
            objProcess.StartInfo.FileName = "powershell.exe"
            objProcess.StartInfo.Arguments = "E:\Install\3rdParty\Additional_Support_files\Windows_Firewall.ps1"
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.RedirectStandardError = True
            objProcess.StartInfo.UseShellExecute = False
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.Start()
    
            Dim output As String = objProcess.StandardOutput.ReadToEnd()
            Dim errors As String = objProcess.StandardError.ReadToEnd()
    
            txt_log.Text += "Output:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += output + Environment.NewLine
            txt_log.Text += Environment.NewLine
            txt_log.Text += "Errors:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += errors + Environment.NewLine

    Here is the code for the new script

     Dim objProcess As New Process()
            objProcess.StartInfo.Verb = "runas"
            objProcess.StartInfo.FileName = "powershell.exe"
            objProcess.StartInfo.Arguments = """E:\Install\BlackBoard\TS\BbTS 3.13.0.0 R2\Script\SetSecurityPolicies.ps1"""
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.RedirectStandardError = True
            objProcess.StartInfo.UseShellExecute = False
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.Start()
    
            Dim output As String = objProcess.StandardOutput.ReadToEnd()
            Dim errors As String = objProcess.StandardError.ReadToEnd()
    
            txt_log.Text += "Output:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += output + Environment.NewLine
            txt_log.Text += Environment.NewLine
            txt_log.Text += "Errors:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += errors + Environment.NewLine

    Here is the output:

    Errors:
    -------
    E:\Install\BlackBoard\TS\BbTS : The term 'E:\Install\BlackBoard\TS\BbTS' is not recognized as the name of a cmdlet, 
    function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 
    path is correct and try again.At line:1 char:1

    + E:\Install\BlackBoard\TS\BbTS 3.13.0.0 R2\Script\SetSecurityPolicies.ps1
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (E:\Install\BlackBoard\TS\BbTS:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    I know the folder and script exist. I can run it through powershell manually

    Any help or advice is appreciated.

    It looks like powershell isn't reading the folder string. There are spaces in the folder which is why I added the """Folder Name"""

    Thursday, April 11, 2019 5:15 PM

Answers

  • I go this fixed. What I did was create a text box. In that text box placed a simple bat file in it.

    It is a hidden box: 

    E:
    cd install
    cd Softwarename
    cd SoftwareVersion
    cd Script
    PowerShell.exe ./SetSecurityPolicies.ps1

    Then I ran this code:

    My.Computer.FileSystem.WriteAllText("E:\Install\Softwarename\SoftwareVersion\Script\Config.bat", txt_Invisible.Text, False, System.Text.Encoding.ASCII)
            Threading.Thread.Sleep(200)
    
            btn_SecurityPolicy.BackColor = Color.Red
            btn_SecurityPolicy.ForeColor = Color.Yellow
    
            Dim procStartInfo As New ProcessStartInfo
            Dim procExecuting As New Process
    
            With procStartInfo
                .UseShellExecute = True
                .FileName = "E:\Install\Softwarename\SoftwareVersion\Script\Config.bat"
                .WindowStyle = ProcessWindowStyle.Normal
                .Verb = "runas" 'add this to prompt for elevation
            End With
    
            procExecuting = Process.Start(procStartInfo)
    		
    		'I do not want the bat file to stay.
    		My.Computer.FileSystem.DeleteFile("E:\Install\BlackBoard\TS\Config.bat")

    And I get the expected results. When I tried running the ps1 script it either errored out or froze. Yet I confirmed directories were correct by using same command but opening the log file. 

    Anyways this is my workaround and I hope it helps anyone banging their head against the wall.

    Thank you to all who gave me advice.

    • Marked as answer by Broggy69 Thursday, April 18, 2019 9:09 PM
    Thursday, April 18, 2019 9:09 PM

All replies

  • Also when I try:

     objProcess.StartInfo.Arguments = "E:\Install\BlackBoard\TS\" & """BbTS 3.13.0.0 R2""" & "\Script\SetSecurityPolicies.ps1"

    I get:

    Errors:
    -------
    The string is missing the terminator: ".
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

    Thursday, April 11, 2019 5:18 PM
  • I just tried:

    Dim sq As String = "'"
    Dim path As String = sq & "E:\Install\BlackBoard\TS\BbTS 3.13.0.0 R2\Script\SetSecurityPolicies.ps1" & sq

    Below is the result I receive:

    So the path looks correct but it doesn't run it.

    Output:
    -------
    E:\Install\BlackBoard\TS\BbTS 3.13.0.0 R2\Script\SetSecurityPolicies.ps1


    Errors:
    -------

    Thursday, April 11, 2019 9:01 PM
  • I got it to run, but the result isn't the same as running it through powershell.

    Here is the new working code. The single codes needed to be just on the folder with the spaces.

      Dim objProcess As New Process()
            objProcess.StartInfo.Verb = "runas"
            objProcess.StartInfo.FileName = "powershell.exe"
            objProcess.StartInfo.Arguments = "E:\Install\BlackBoard\TS\" & "'" & "BbTS 3.13.0.0 R2" & "'" & "\Script\SetSecurityPolicies.ps1"
            objProcess.StartInfo.RedirectStandardOutput = True
            objProcess.StartInfo.RedirectStandardError = True
            objProcess.StartInfo.UseShellExecute = False
            objProcess.StartInfo.CreateNoWindow = True
            objProcess.Start()
    
            Dim output As String = objProcess.StandardOutput.ReadToEnd()
            Dim errors As String = objProcess.StandardError.ReadToEnd()
    
            txt_log.Text += "Output:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += output + Environment.NewLine
            txt_log.Text += Environment.NewLine
            txt_log.Text += "Errors:" + Environment.NewLine
            txt_log.Text += "-------" + Environment.NewLine
            txt_log.Text += errors + Environment.NewLine

    Here is the output:

    -------
    Configuring Local Audit Policies
    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    The command was successfully executed.

    Configured Local Audit Policies

    Configuring Security Event Log Property 'Maximum Log Size'
    Maximum Log Size already 1 GB, or greater.  Value was not changed

    Disabling SSL 2.0

    Disabled SSL 2.0

    Disabling SSL 3.0

    Disabled SSL 3.0

    Disabling TLS 1.0

    Disabled TLS 1.0

    Disabling TLS 1.1

    Disabled TLS 1.1

    Disabling RC4 Ciphers

    RC4 Ciphers Disabled


    Disabling Windows Error Reporting


    Errors:
    -------
    serverWerOptin : The term 'serverWerOptin' is not recognized as the name of a cmdlet, function, script file, or 
    operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try 
    again.
    At E:\Install\BlackBoard\TS\BbTS 3.13.0.0 R2\Script\SetSecurityPolicies.ps1:163 char:3
    +   serverWerOptin /disable
    +   ~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (serverWerOptin:String) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : CommandNotFoundException

    However when running this script through powershell outside of VB here is the results:

    PS C:\Users\GBowlsby> E:\Install\BlackBoard\TS'\BbTS 3.13.0.0 R2'\Script\SetSecurityPolicies.ps1
    Configuring Local Audit Policies
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    The command was successfully executed.
    Configured Local Audit Policies

    Configuring Security Event Log Property 'Maximum Log Size'
    Maximum Log Size already 1 GB, or greater.  Value was not changed

    Disabling SSL 2.0

    Disabled SSL 2.0

    Disabling SSL 3.0

    Disabled SSL 3.0

    Disabling TLS 1.0

    Disabled TLS 1.0

    Disabling TLS 1.1

    Disabled TLS 1.1

    Disabling RC4 Ciphers

    RC4 Ciphers Disabled


    Disabling Windows Error Reporting

    You have chosen to disable Windows Error Reporting

    For more information, visit
    http://go.microsoft.com/fwlink/?LinkId=158331
    For Windows privacy statement, please visit
    http://go.microsoft.com/fwlink/?LinkId=280262

    Script completed. Press Enter to continue.

    Anyone have an idea why?

    Thursday, April 11, 2019 9:43 PM
  • Hi

    see the following link,see if it has the same issue as you.

    https://stackoverflow.com/questions/47445749/import-module-not-working-from-within-winforms-application

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 12, 2019 7:16 AM
  • Hi

    see the following link,see if it has the same issue as you.

    https://stackoverflow.com/questions/47445749/import-module-not-working-from-within-winforms-application

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Sorry I have not been able to test this yet. My VM at work was deleted. At least I made a backup. Waiting for my VM to get rebuilt
    Monday, April 15, 2019 5:49 PM
  • I go this fixed. What I did was create a text box. In that text box placed a simple bat file in it.

    It is a hidden box: 

    E:
    cd install
    cd Softwarename
    cd SoftwareVersion
    cd Script
    PowerShell.exe ./SetSecurityPolicies.ps1

    Then I ran this code:

    My.Computer.FileSystem.WriteAllText("E:\Install\Softwarename\SoftwareVersion\Script\Config.bat", txt_Invisible.Text, False, System.Text.Encoding.ASCII)
            Threading.Thread.Sleep(200)
    
            btn_SecurityPolicy.BackColor = Color.Red
            btn_SecurityPolicy.ForeColor = Color.Yellow
    
            Dim procStartInfo As New ProcessStartInfo
            Dim procExecuting As New Process
    
            With procStartInfo
                .UseShellExecute = True
                .FileName = "E:\Install\Softwarename\SoftwareVersion\Script\Config.bat"
                .WindowStyle = ProcessWindowStyle.Normal
                .Verb = "runas" 'add this to prompt for elevation
            End With
    
            procExecuting = Process.Start(procStartInfo)
    		
    		'I do not want the bat file to stay.
    		My.Computer.FileSystem.DeleteFile("E:\Install\BlackBoard\TS\Config.bat")

    And I get the expected results. When I tried running the ps1 script it either errored out or froze. Yet I confirmed directories were correct by using same command but opening the log file. 

    Anyways this is my workaround and I hope it helps anyone banging their head against the wall.

    Thank you to all who gave me advice.

    • Marked as answer by Broggy69 Thursday, April 18, 2019 9:09 PM
    Thursday, April 18, 2019 9:09 PM