1
votes

I have a Problem with my script, and I can't find the solution. Allways when I try to run this script it says:

Cmdlet Start-Process an der Befehlspipelineposition 1 Geben Sie Werte für die folgenden Parameter an: FilePath:

What does this mean? I know I'm not very good in Powershell, just began to script in last summer. So it may look a bit ugly...

here's my script, hope someone could help me:

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
i don't see "start-process" in this script. is it coming from one of the other scripts it's using? - Ricc Babbitt
Thats the Porblem, nothing is in the other scripts expect of Write-Host "hi". - andràs Horber

2 Answers

0
votes

I think I've fond the solution. I just changed the function for the tasks, so it takes the path with PS. So now it looks like this:

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

This works well, but I don't know if this also works for other scripts. Well thanks for your help

0
votes

You can try this instead?

$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();

I'd recommend you make it into a function though so you can call it multiple times. Like so:

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();
}

and call like this

$result = RunProcessWithOutput $utilityScript

and this is the result

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
 }