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.
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
Rabid138
Posts: 10
Last visit: Fri Nov 03, 2023 5:04 am

Pass a variable

Post by Rabid138 »

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. $location = $args[0]
  3.  
  4. $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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass a variable

Post by jvierra »

An AS custom context menu item can call PowerShell. Juist specify PS and add the script as an argument.
Rabid138
Posts: 10
Last visit: Fri Nov 03, 2023 5:04 am

Re: Pass a variable

Post by Rabid138 »

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass a variable

Post by jvierra »

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

[environment]::GetCommandLineArgs()
Rabid138
Posts: 10
Last visit: Fri Nov 03, 2023 5:04 am

Re: Pass a variable

Post by Rabid138 »

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.     Import-Module ActiveDirectory
  3.     $Input = ([environment]::GetCommandLineArgs()[5] -split ',*..=')[1]
  4.    
  5.     $spn = Get-ADComputer $Input -Properties servicePrincipalName | select -ExpandProperty servicePrincipalName
  6.     $textbox1.Lines = $spn | Out-String
  7.     $listbox1.Items.AddRange($spn)
  8. }
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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass a variable

Post by jvierra »

What you are trying to do cannot be done. Hiding the console Window will hide all child Windows.
Rabid138
Posts: 10
Last visit: Fri Nov 03, 2023 5:04 am

Re: Pass a variable

Post by Rabid138 »

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. $location = $args[0]
  3.  
  4. $Input = ($location -split ',*..=')[1]
  5.  
  6. $Computer = Get-ADComputer $Input -Properties cn | select -ExpandProperty cn
  7. $DateCreated = Get-ADComputer $Input -Properties customDateCreated | select -ExpandProperty customDateCreated
  8. $Justify = Get-ADComputer $Input -Properties customJustify | select -ExpandProperty customJustify
  9. $Validated = Get-ADComputer $Input -Properties customlastValidated | select -ExpandProperty customlastValidated
  10. $ProjectCode = Get-ADComputer $Input -Properties customProjectCode | select -ExpandProperty customProjectCode
  11. $Purpose = Get-ADComputer $Input -Properties customPurpose | select -ExpandProperty customPurpose
  12. $Ticket = Get-ADComputer $Input -Properties customTicket | select -ExpandProperty customTicket
  13. $POC = Get-ADComputer $Input -Properties customPOCmail | select -ExpandProperty customPOCmail
  14.  
  15. Add-Type -AssemblyName System.Windows.Forms
  16. Add-Type -AssemblyName System.Drawing
  17. #Window Options
  18. $form = New-Object System.Windows.Forms.Form
  19. $form.BackColor = "DimGray"
  20. $form.Text = 'ComputerEdit for ' + $Computer
  21. $form.FormBorderStyle = 'Fixed3D'
  22. $form.Size = New-Object System.Drawing.Size(940,380)
  23. $form.StartPosition = 'CenterScreen'
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass a variable

Post by jvierra »

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
Last visit: Fri Nov 03, 2023 5:04 am

Re: Pass a variable

Post by Rabid138 »

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. $Input = ($location -split ',*..=')[1]
Then I changed any reference to $Input inside the function to $Script:Input.

Thanks!!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Pass a variable

Post by jvierra »

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
This topic is 4 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked