Runnning Powershell Code in XAML

Hello everyone. I saw a video on Youtube by Rob Holt (great video btw) called “Using the PowerShell API from .NET”, and I needed some help with running my PowerShell code in my XAML GUI. I was able to run simple commands, but not the commands in my script. I previously built my app entirely in powershell ISE, but I wanted to gain experience in Visual Studio using XAML for the GUI. If needed, I can post any code that is needed. Thanks for any help that anyone can provide :slight_smile:

Hi Angelman, welcome to the forum.

I don’t see how we could help you without knowing what help you seek and/or seeing some code/errors that you’re needing help with. Please format any code or errors as code using the preformatted option </>

Hi, thanks for your reply. I wrote a PowerShell program for my Helpdesk that can find AD users and computers and then perform some functions such as reset password, unlock account, and others. Here is my code for the Find Computer function in my PS script. I would like to perform this code in the BtnFindComputer_Click in my XAML GUI, and then display the results in the appropriate textboxes.

    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox2tab2.Clear()
    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox3tab2.Clear()
    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox4tab2.Clear()
    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox5tab2.Clear()
    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox6tab2.Clear()
    Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $textbox7tab2.Clear()
  

    $ComputerText = $textbox1tab2.Text
    $ComputerInfo = Get-ADComputer -Identity $ComputerText -Properties *
    $Ping = Test-Connection $ComputerText -Count 1
    $IPV4= $Ping | Select-Object "IPV4Address"
    $Memberships = Get-ADComputer -identity $ComputerText -properties Memberof | %{$_.memberof} | %{get-adgroup $_ | Select -ExpandProperty name} | Out-String 
    $QueryComp = Query user /server:$ComputerText | ForEach-Object { $PSItem | ConvertFrom-String} |Select -ExpandProperty P2 | Out-String

          
    if ($Null -eq $ComputerInfo.Name) {$statusbartab2.Text = 'Computer Not Found!'} else {$statusbartab2.Text = 'Computer Found!'}  
    if ($Null -eq $Ping.IPV4Address) {$textbox2tab2.Text = "Failed"} else {$textbox2tab2.Text = $textbox2tab2.Text + ($Ping.IPV4Address.ToString()) -replace "(CN=)(.*?),.*",'$2'}if ($Null -eq $IPV4) {$textbox3tab2.Text = "Offline"} else {$textbox3tab2.Text = "Online"} 
    if ($Null -eq $ComputerInfo.OperatingSystem) {$textbox4tab2.Text = "N\A"} else {$textbox4tab2.Text = $textbox4tab2.Text + ($ComputerInfo.OperatingSystem.ToString()),($ComputerInfo.OperatingSystemVersion.ToString()) -replace "(CN=)(.*?),.*",'$2'}
    if ($Null -eq $QueryComp) {$textbox6tab2.Text = "Don't Know"} else {$textbox6tab2.Text = $textbox6tab2.Text + ($QueryComp) -replace "USERNAME"}
    if ($Null -eq $ComputerInfo.Description) {$textbox5tab2.Text = "N\A"} else {$textbox5tab2.Text = $textbox5tab2.Text + ($ComputerInfo.Description.ToString()) -replace "(CN=)(.*?),.*",'$2'}
    if ($Null -eq $Memberships) {$textbox7tab2.Text = "N\A"} else {$textbox7tab2.Text = $textbox7tab2.Text + ($Memberships)} 
    {

Let me know if you need anything else. Thanks again everyone!