Demont
Demont

Reputation: 15

Create Powershell GUI with Visual Studio

I am using Visual Studio to create a GUI environment for all my PowerShell scripts together to create a common platform . I have one problem with the text box . I want to have a text box so all the results appear there . Right now only the last command is appearing there .

Any suggestions ?

 <TextBox  x:Name="Information" HorizontalAlignment="Left" Margin="10,116,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Grid.ColumnSpan="3" Height="255" Width="678"/>

and I want at this texbox to appear the following messages

$WPFMapping.Add_Click({
    {
        $WPFInformation.Text = " We Will try to map the drives"}
    if (((New-Object System.IO.DriveInfo("N:")).DriveType -ne
    "NoRootDirectory"))
    {
        $WPFInformation.Text = " N drive is already mounted and accessible"}
    else {
        net use /persistent:yes N: "this drive"
        Write-Host     "mounting"}
    if (((New-Object System.IO.DriveInfo("V:")).DriveType -ne
    "NoRootDirectory"))
    {
        $WPFInformation.Text = " V drive is already mounted and accessible"}
    else  {
        $WPFInformation.Text = " V drive mapping in progress"}
    net use /persistent:yes V: "this drive"
    $WPFInformation.Text = " V drive mapping has been completed"
})

Upvotes: 0

Views: 297

Answers (1)

Theo
Theo

Reputation: 61013

By setting the text in $WPFInformation.Text for the second drive mapping (V:), you overwrite the text you have set in there earlier for the N: mapping.

To append extra lines to the textbox, use

$WPFInformation.Text += [Environment]::NewLine
$WPFInformation.Text += " V drive is already mounted and accessible"

etc. for every next line you want to add.

You can also use AppendText() to add the new lines in there:

$WPFInformation.AppendText("`r`n V drive is already mounted and accessible")

Where `r`n represents the CRLF newline.

p.s. Of course, make sure the textbox's MultiLine property is set to $WPFInformation.Multiline = $true

Upvotes: 1

Related Questions