Help with my PowerShell Form

I “created” a PowerShell form with several buttons that perform actions, I start out the script with “get-credential” and store it in a variable called $cred. The issue I am having is how to use the stored variable on the button. I would be glad if someone could provide assistance with this. Also the script below does not render when ran with PowerShell.exe it only runs when I run it from PowerShell_ISE.exe… Thank you in advance.

$cred = Get-Credential
   $Job = Start-Job -ScriptBlock {
   Add-Type -AssemblyName System.Windows.Forms
   Add-Type -AssemblyName System.Drawing
   $form = New-Object System.Windows.Forms.Form
   $form.Text = 'CNR Tools’
   $form.Size = New-Object System.Drawing.Size(900,600)
   $form.StartPosition = 'CenterScreen'
   $form.AutoSize = $true
   $form.MaximizeBox = $false
   $form.FormBorderStyle = 'FixedSingle'


   $img = [System.Drawing.Image]::Fromfile("c:\users\$env:username\Pictures\logo.png")
   $pictureBox = New-Object Windows.Forms.PictureBox
   $pictureBox.Width = $img.Size.Width
   $pictureBox.Height = $img.Size.Height
   $pictureBox.Location = New-Object System.Drawing.Size(600,465)
   $pictureBox.Image = $img
   $form.controls.add($pictureBox) 

   $ADUCButton = New-Object System.Windows.Forms.Button
   $ADUCButton.Location = New-Object System.Drawing.Point(10,25)
   $ADUCButton.Size = New-Object System.Drawing.Size(300,100)
   $ADUCButton.Font = New-Object System.Drawing.Font(“Times New Roman”,14, [System.Drawing.Fontstyle]::Bold) 
   $ADUCButton.Text = ' Active Directory Users and Computers '
   $ADUCButton.Add_Click({Start-Process -filepath 'c:\windows\system32\cmd.exe' -WindowStyle maximized})
   $ADUCButton.FlatAppearance.BorderColor = [System.Drawing.Color]::DarkBlue
   $ADUCButton.BackColor = [System.Drawing.Color]::CornflowerBlue
   $form.Controls.Add($ADUCButton)

   $label = New-Object System.Windows.Forms.Label
   $label.location = New-Object System.Drawing.Point(100,500)
   $label.Size = New-Object System.Drawing.Size(280,70)
   $label.Font = New-Object System.Drawing.Font("Lucida Console",8, 
 [System.Drawing.FontStyle]::Italic)
   $label.Text = 'Created by a PowerShell Novice'
   $form.Controls.Add($label)

   $CancelButton = New-Object System.Windows.Forms.Button
   $CancelButton.Location = New-Object System.Drawing.Point(850,300)
   $CancelButton.Size = New-Object System.Drawing.Size(75,23)
   $CancelButton.Text = 'Close'
   $CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
   $form.CancelButton = $CancelButton
   $form.Controls.Add($CancelButton)



   $result = $form.ShowDialog()

   if ($result -eq [System.Windows.Forms.DialogResult]::OK)
   {
   $x = $listBox.SelectedItems
   $x
   }

   } -Credential $cred
   Recieve-Job $job

 

Hello Ogheneotsuko,

First, you don’t want to run a form in a background job. The UI is the main thread intended for user interaction. This is likely why you are not seeing it in regular powershell. Powershell_ise.exe does some magic so always be sure to test in the real console.

Second, if you are intending to use the credential on the start-process command, you’ll simply add the creds there. I’ve updated your script to remove the background job, create a small powershell script in $env:temp, and set Start-Process to run powershell.exe with the created script as an argument.

Put in different valid credentials than the ones calling so you can see the output show you the credentials are passed along and being used.

@'
whoami
pause
'@ | Out-File $env:temp\hello.ps1 -Encoding ascii -Force

$cred = Get-Credential

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'CNR Tools’
$form.Size = New-Object System.Drawing.Size(900,600)
$form.StartPosition = 'CenterScreen'
$form.AutoSize = $true
$form.MaximizeBox = $false
$form.FormBorderStyle = 'FixedSingle'

$img = [System.Drawing.Image]::Fromfile("c:\users\$env:username\Pictures\logo.png")
$pictureBox = New-Object Windows.Forms.PictureBox
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Location = New-Object System.Drawing.Size(600,465)
$pictureBox.Image = $img
$form.controls.add($pictureBox) 

$ADUCButton = New-Object System.Windows.Forms.Button
$ADUCButton.Location = New-Object System.Drawing.Point(10,25)
$ADUCButton.Size = New-Object System.Drawing.Size(300,100)
$ADUCButton.Font = New-Object System.Drawing.Font(“Times New Roman”,14, [System.Drawing.Fontstyle]::Bold) 
$ADUCButton.Text = ' Active Directory Users and Computers '
$ADUCButton.Add_Click({Start-Process -filepath "powershell.exe" -WindowStyle maximized -Credential $cred -ArgumentList $env:temp\hello.ps1})
$ADUCButton.FlatAppearance.BorderColor = [System.Drawing.Color]::DarkBlue
$ADUCButton.BackColor = [System.Drawing.Color]::CornflowerBlue
$form.Controls.Add($ADUCButton)

$label = New-Object System.Windows.Forms.Label
$label.location = New-Object System.Drawing.Point(100,500)
$label.Size = New-Object System.Drawing.Size(280,70)
$label.Font = New-Object System.Drawing.Font("Lucida Console",8, 
[System.Drawing.FontStyle]::Italic)
$label.Text = 'Created by a PowerShell Novice'
$form.Controls.Add($label)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(850,300)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Close'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItems
$x
}

Thank you for the information so everything does render but the credentials stored in the $cred variable are not used. I need the entire form to open up with those credentials.

All the individual buttons (8) require elevated privileges to access, so that is why I originally passed the credentials as part of a “job”.

 

I thought of starting the small script in this fashion…

$cred = Get-Credential Start-Process PowerShell.exe $env:temp\myscript.ps1 -Credential $cred
My syntax is obviously incorrect (it fails miserably) but I think it carries the idea of what I am trying to accomplish.

So you’re trying to start the entire script with different credentials and then a third set of creds for the scripts the buttons invoke? May be a bit much for a forum. It seems like you’re on the right track so keep at it and come back if you hit another snag.

Your syntax works fine. You should be able to start this script as you specified and then pass more credentials along. One correction, if it’s saved to $env:temp under the one user then it may not be accessible to the second user. Be sure to move it to a location that the users have access to. My apologies for overlooking that.

No I simply want to start the main script with elevated privileges so two sets of credentials. The one am logged in on and the one to open the script. If you can provide the appropriate syntax I would be forever in your debt…

Unlike most similar games, there is no gold in Path of Exile games. Instead, there are different Orbs, scrolls and POE Items. POE Currency has different purposes, including enhancing equipment, changing item modifiers, and forging new common, magic, rare and unique armor or weapons. Many currencies appear in the form of Orb, such as POE Exalted Orb and POE Chaos Orb. Other forms of currency include scrolls, coins, vials, and sextants.

There are several ways to obtain currency in Path of Exile. Unfortunately, none of them are very fruitful, and it may take a while for them to find the valuables you are looking for, especially when they are rare. So what I recommend most is to buy products directly at IGGM. Their website has more than six years of operating experience and can provide in-game currency without putting your account at risk. At the same time, they provide safe transactions and convenient delivery methods.At https://www.iggm.com/poe-currency

You sir solved my issues, I took your counsel and created a small script with the following line

 

$cred = Get-Credential Start PowerShell.exe '\\myserver\myscript.ps1' -Credential $cred
And that works fine, I do have a follow on question that is purely cosmetic. The solution opens another powershell window with the words "GrowOnly" if there was a way to either hide the window or reduce its size while inserting the message "Do not close me" I would be content. But if this is the solution then it has to be the solution...