Powershell脚本启动过程无效

时间:2018-01-05 22:59:29

标签: powershell cmdlets start-process

我的脚本出现问题,但找不到解决方法。总之,当我尝试运行这个脚本时,它说:

Cmdlet Start-Process an der Befehlspipelineposition 1   Geben SieWertefürdiefolgenden参数an:   文件路径:

这是什么意思? 我知道我在Powershell中不是很好,去年夏天开始写剧本。 所以它可能看起来有点难看......

这是我的剧本,希望有人可以帮助我:

function Task_S{

    #starting 'task_s.ps1'
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "..\Start_and_furthers\task_s.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = .$utilityScript
}

function Task_D{

    #starting 'task_d.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_d.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Task_I{

    #starting 'task_i.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_i.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Task_T{

    #starting 'task_t.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_t.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Choicebuttons{

    function Window {

        # Add Window
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "Exercises"
        $Form.Size = New-Object System.Drawing.Size(200,200)
        $Form.Topmost = $True
    }

    function Buttons {

        # Add Button for S
        $Button_S = New-Object System.Windows.Forms.Button
        $Button_S.Location = New-Object System.Drawing.Size(40,30)
        $Button_S.Size = New-Object System.Drawing.Size(120,30)
        $Button_S.Text = "S: "

        # Add Button for D
        $Button_D = New-Object System.Windows.Forms.Button
        $Button_D.Location = New-Object System.Drawing.Size(40,100)
        $Button_D.Size = New-Object System.Drawing.Size(120,30)
        $Button_D.Text = "D:"

        # Add Button for I
        $Button_I = New-Object System.Windows.Forms.Button
        $Button_I.Location = New-Object System.Drawing.Size(40,100)
        $Button_I.Size = New-Object System.Drawing.Size(120,30)
        $Button_I.Text = "I:"

        # Add Button for T
        $Button_T = New-Object System.Windows.Forms.Button
        $Button_T.Location = New-Object System.Drawing.Size(40,100)
        $Button_T.Size = New-Object System.Drawing.Size(120,30)
        $Button_T.Text = "T:"
    }

    function Labels {

        # Add Label for S
        $Label_S = New-Object System.Windows.Forms.Label
        $Label_S.Location = New-Object System.Drawing.Size(40,30)
        $Label_S.Text = "Strukturen suchen und dokumentieren mit Struktogramm"

        # Add Label for D
        $Label_D = New-Object System.Windows.Forms.Label
        $Label_D.Location = New-Object System.Drawing.Size(40,30)
        $Label_D.Text = "Datentypen anwenden und dokumentieren"

        # Add Label for I
        $Label_I = New-Object System.Windows.Forms.Label
        $Label_I.Location = New-Object System.Drawing.Size(40,30)
        $Label_I.Text = "Ablaufstruktur umsetzen & Quellcode ausarbeiten"

        # Add Label for T
        $Label_T = New-Object System.Windows.Forms.Label
        $Label_T.Location = New-Object System.Drawing.Size(40,30)
        $Label_T.Text = "Debuggen & Testen"
    }

    function AddForms {

        # Add Buttons and Labels to Window
        $Form.Controls.Add($Button_S)
        $Form.Controls.Add($Button_D)
        $Form.Controls.Add($Button_I)
        $Form.Controls.Add($Button_T)
        $Form.Controls.Add($Label_S)
        $Form.Controls.Add($Label_D)
        $Form.Controls.Add($Label_I)
        $Form.Controls.Add($Label_T)
    }

    function ButtonEvent {

        #Add Button event 
        $Button_S.Add_Click({Task_D})
        $Button_D.Add_Click({Task_D})
        $Button_I.Add_Click({Task_I})
        $Button_T.Add_Click({Task_T})
    }

    # Starting for Window
    Add-Type -AssemblyName System.Windows.Forms    
    Add-Type -AssemblyName System.Drawing

    # Loading Functions & Inserts
    Window
    Buttons
    Labels
    Addforms
    Buttonevent

    #Show the Form 
    $form.ShowDialog()| Out-Null
 }

Choicebuttons

2 个答案:

答案 0 :(得分:0)

您可以试试这个吗?

$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = $utilityScript
$ps.StartInfo.RedirectStandardOutput = $True
$ps.StartInfo.UseShellExecute = $false
$ps.start()
$ps.WaitForExit()
$result = $ps.StandardOutput.ReadToEnd();

我建议你把它变成一个功能,所以你可以多次调用它。像这样:

function RunProcessWithOutput($fileName)
{
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = $fileName
$ps.StartInfo.RedirectStandardOutput = $True
$ps.StartInfo.UseShellExecute = $false
$ps.start()
$ps.WaitForExit()
return $ps.StandardOutput.ReadToEnd();
}

并像这样打电话

$result = RunProcessWithOutput $utilityScript

这是结果

function RunProcessWithOutput($fileName)
{
$ps = new-object System.Diagnostics.Process
$ps.StartInfo.Filename = $fileName
$ps.StartInfo.RedirectStandardOutput = $True
$ps.StartInfo.UseShellExecute = $false
$ps.start()
$ps.WaitForExit()
return $ps.StandardOutput.ReadToEnd();
}

function Task_S{

    #starting 'task_s.ps1'
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "..\Start_and_furthers\task_s.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = RunProcessWithOutput $utilityScript
}

function Task_D{

    #starting 'task_d.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_d.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Task_I{

    #starting 'task_i.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_i.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Task_T{

    #starting 'task_t.ps1
    $thisScriptDirectoryPath = Split-Path -parent 
    $MyInvocation.MyCommand.Definition
    $utilityScriptName = "task_t.ps1"
    $utilityScript = (Join-Path $thisScriptDirectoryPath $utilityScriptName)
    $result = . $utilityScript
}

function Choicebuttons{

    function Window {

        # Add Window
        $Form = New-Object System.Windows.Forms.Form
        $Form.Text = "Exercises"
        $Form.Size = New-Object System.Drawing.Size(200,200)
        $Form.Topmost = $True
    }

    function Buttons {

        # Add Button for S
        $Button_S = New-Object System.Windows.Forms.Button
        $Button_S.Location = New-Object System.Drawing.Size(40,30)
        $Button_S.Size = New-Object System.Drawing.Size(120,30)
        $Button_S.Text = "S: "

        # Add Button for D
        $Button_D = New-Object System.Windows.Forms.Button
        $Button_D.Location = New-Object System.Drawing.Size(40,100)
        $Button_D.Size = New-Object System.Drawing.Size(120,30)
        $Button_D.Text = "D:"

        # Add Button for I
        $Button_I = New-Object System.Windows.Forms.Button
        $Button_I.Location = New-Object System.Drawing.Size(40,100)
        $Button_I.Size = New-Object System.Drawing.Size(120,30)
        $Button_I.Text = "I:"

        # Add Button for T
        $Button_T = New-Object System.Windows.Forms.Button
        $Button_T.Location = New-Object System.Drawing.Size(40,100)
        $Button_T.Size = New-Object System.Drawing.Size(120,30)
        $Button_T.Text = "T:"
    }

    function Labels {

        # Add Label for S
        $Label_S = New-Object System.Windows.Forms.Label
        $Label_S.Location = New-Object System.Drawing.Size(40,30)
        $Label_S.Text = "Strukturen suchen und dokumentieren mit Struktogramm"

        # Add Label for D
        $Label_D = New-Object System.Windows.Forms.Label
        $Label_D.Location = New-Object System.Drawing.Size(40,30)
        $Label_D.Text = "Datentypen anwenden und dokumentieren"

        # Add Label for I
        $Label_I = New-Object System.Windows.Forms.Label
        $Label_I.Location = New-Object System.Drawing.Size(40,30)
        $Label_I.Text = "Ablaufstruktur umsetzen & Quellcode ausarbeiten"

        # Add Label for T
        $Label_T = New-Object System.Windows.Forms.Label
        $Label_T.Location = New-Object System.Drawing.Size(40,30)
        $Label_T.Text = "Debuggen & Testen"
    }

    function AddForms {

        # Add Buttons and Labels to Window
        $Form.Controls.Add($Button_S)
        $Form.Controls.Add($Button_D)
        $Form.Controls.Add($Button_I)
        $Form.Controls.Add($Button_T)
        $Form.Controls.Add($Label_S)
        $Form.Controls.Add($Label_D)
        $Form.Controls.Add($Label_I)
        $Form.Controls.Add($Label_T)
    }

    function ButtonEvent {

        #Add Button event 
        $Button_S.Add_Click({Task_D})
        $Button_D.Add_Click({Task_D})
        $Button_I.Add_Click({Task_I})
        $Button_T.Add_Click({Task_T})
    }

    # Starting for Window
    Add-Type -AssemblyName System.Windows.Forms    
    Add-Type -AssemblyName System.Drawing

    # Loading Functions & Inserts
    Window
    Buttons
    Labels
    Addforms
    Buttonevent

    #Show the Form 
    $form.ShowDialog()| Out-Null
 }

答案 1 :(得分:0)

我认为我喜欢这个解决方案。我刚刚更改了任务的功能,因此需要使用PS的路径。所以现在它看起来像这样:

$utilityScriptName = "..\start2.ps1"
$utilityScript = (Join-Path $PSCommandpath $utilityScriptName)
$result = .$utilityScript

这很有效,但我不知道这是否适用于其他脚本。谢谢你的帮助