Pass a variable

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Rabid138
Posts: 10
Joined: Thu Oct 03, 2019 6:22 am

Pass a variable

Post by Rabid138 » Wed Oct 09, 2019 4:44 pm

I have a custom Active Directory context menu. The menu item calls a .bat file (because as far as I know you cannot call a .ps1 file).

.bat
  1. PowerShell.exe -ExecutionPolicy Bypass -WindowStyle Hidden -File "\\mydomain.com\SYSVOL\mydomain.com\scripts\ContextMenus\Computeredit.ps1" "%*"
I currently have a form that I made outside of Powershell Studio and the beginning of that looks like:
  1. Import-Module ActiveDirectory
  2.  
  3. $location = $args[0]
  4.  
  5.  
  6.  
  7. $Input = ($location -split ',*..=')[1]
I have a form made in Powershell Studio that works great if I specify $Input manually. Where exactly do I put those 3 lines in Powershell Studio? I have tried inside of $formBlah_Load and I have tried as the first 3 lines but my form is blank.

jvierra
Posts: 14132
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass a variable

Post by jvierra » Wed Oct 09, 2019 4:56 pm

An AS custom context menu item can call PowerShell. Juist specify PS and add the script as an argument.

Rabid138
Posts: 10
Joined: Thu Oct 03, 2019 6:22 am

Re: Pass a variable

Post by Rabid138 » Wed Oct 09, 2019 5:08 pm

I was under the impression that one had to hack the registry to make a context menu for ADUC to run a .ps1 file. My goal is to be able to right-click a computer in ADUC and for the form to know what computer I just right-clicked.

jvierra
Posts: 14132
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass a variable

Post by jvierra » Wed Oct 09, 2019 5:18 pm

The following will get the command line arguments from anywhere in PowerShell:

[environment]::GetCommandLineArgs()

Rabid138
Posts: 10
Joined: Thu Oct 03, 2019 6:22 am

Re: Pass a variable

Post by Rabid138 » Thu Oct 10, 2019 7:06 am

I really appreciate your help! I am almost there. Granted I might be going about this all wrong. So using your form as an example, everything works if I do the following:
  1. PowerShell.exe -ExecutionPolicy Bypass -File "\\MYDOMAIN.com\SYSVOL\MYDOMAIN.com\scripts\ContextMenus\TestSPN.ps1" "%*"
I changed your form to look like:
  1. $form1_Load = {
  2.  
  3.     Import-Module ActiveDirectory
  4.  
  5.     $Input = ([environment]::GetCommandLineArgs()[5] -split ',*..=')[1]
  6.  
  7.    
  8.  
  9.     $spn = Get-ADComputer $Input -Properties servicePrincipalName | select -ExpandProperty servicePrincipalName
  10.  
  11.     $textbox1.Lines = $spn | Out-String
  12.  
  13.     $listbox1.Items.AddRange($spn)
  14.  
  15. }
This returns exactly what it is supposed to for each right-click of a computer in ADUC. If I add "-WindowStyle Hidden" to the .bat file the form goes blank. I am just trying to hide the console window.

jvierra
Posts: 14132
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass a variable

Post by jvierra » Thu Oct 10, 2019 7:15 am

What you are trying to do cannot be done. Hiding the console Window will hide all child Windows.

Rabid138
Posts: 10
Joined: Thu Oct 03, 2019 6:22 am

Re: Pass a variable

Post by Rabid138 » Thu Oct 10, 2019 8:29 am

First, I want to say thank you for replying to all of my newbie questions. I really appreciate it, since forms are really new to me. Not to sound dense but what do people normally do for a context menu and ADUC? I had a form working with the hidden switch. Below is the first part of the script. I am guessing there is something different between what I am making in Powershell Studio and the script below.

  1. Import-Module ActiveDirectory
  2.  
  3. $location = $args[0]
  4.  
  5.  
  6.  
  7. $Input = ($location -split ',*..=')[1]
  8.  
  9.  
  10.  
  11. $Computer = Get-ADComputer $Input -Properties cn | select -ExpandProperty cn
  12.  
  13. $DateCreated = Get-ADComputer $Input -Properties customDateCreated | select -ExpandProperty customDateCreated
  14.  
  15. $Justify = Get-ADComputer $Input -Properties customJustify | select -ExpandProperty customJustify
  16.  
  17. $Validated = Get-ADComputer $Input -Properties customlastValidated | select -ExpandProperty customlastValidated
  18.  
  19. $ProjectCode = Get-ADComputer $Input -Properties customProjectCode | select -ExpandProperty customProjectCode
  20.  
  21. $Purpose = Get-ADComputer $Input -Properties customPurpose | select -ExpandProperty customPurpose
  22.  
  23. $Ticket = Get-ADComputer $Input -Properties customTicket | select -ExpandProperty customTicket
  24.  
  25. $POC = Get-ADComputer $Input -Properties customPOCmail | select -ExpandProperty customPOCmail
  26.  
  27.  
  28.  
  29. Add-Type -AssemblyName System.Windows.Forms
  30.  
  31. Add-Type -AssemblyName System.Drawing
  32.  
  33. #Window Options
  34.  
  35. $form = New-Object System.Windows.Forms.Form
  36.  
  37. $form.BackColor = "DimGray"
  38.  
  39. $form.Text = 'ComputerEdit for ' + $Computer
  40.  
  41. $form.FormBorderStyle = 'Fixed3D'
  42.  
  43. $form.Size = New-Object System.Drawing.Size(940,380)
  44.  
  45. $form.StartPosition = 'CenterScreen'

jvierra
Posts: 14132
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass a variable

Post by jvierra » Thu Oct 10, 2019 8:38 am

Yes PSS builds forms into a function. Export the PSS form to a file and look at how it is built to see the differences.

Rabid138
Posts: 10
Joined: Thu Oct 03, 2019 6:22 am

Re: Pass a variable

Post by Rabid138 » Thu Oct 10, 2019 9:01 am

Thanks again for pointing me to the right direction! I got this to work with the window hidden. After I exported to PS1 from PSS, I add the following at line 1:
  1. $location = $args[0]
  2.  
  3. $Input = ($location -split ',*..=')[1]
Then I changed any reference to $Input inside the function to $Script:Input.

Thanks!!

jvierra
Posts: 14132
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Pass a variable

Post by jvierra » Thu Oct 10, 2019 9:09 am

Please note that $input is a reserved variable. YOu should not use that name for a variable or it can be altered by PS before you get to use it.

For a partial list see: http://xahlee.info/powershell/automatic_variables.html

Locked