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

Code: Select all

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:

Code: Select all

Import-Module ActiveDirectory
$location = $args[0]

$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.

User avatar
jvierra
Posts: 13802
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.

User avatar
jvierra
Posts: 13802
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:

Code: Select all

PowerShell.exe -ExecutionPolicy Bypass -File "\\MYDOMAIN.com\SYSVOL\MYDOMAIN.com\scripts\ContextMenus\TestSPN.ps1" "%*"
I changed your form to look like:

Code: Select all

$form1_Load = {
Import-Module ActiveDirectory
$Input = ([environment]::GetCommandLineArgs()[5] -split ',*..=')[1]

$spn = Get-ADComputer $Input -Properties servicePrincipalName | select -ExpandProperty servicePrincipalName
$textbox1.Lines = $spn | Out-String
$listbox1.Items.AddRange($spn)
}
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.

User avatar
jvierra
Posts: 13802
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.

Code: Select all

Import-Module ActiveDirectory
$location = $args[0]

$Input = ($location -split ',*..=')[1]

$Computer = Get-ADComputer $Input -Properties cn | select -ExpandProperty cn
$DateCreated = Get-ADComputer $Input -Properties customDateCreated | select -ExpandProperty customDateCreated
$Justify = Get-ADComputer $Input -Properties customJustify | select -ExpandProperty customJustify
$Validated = Get-ADComputer $Input -Properties customlastValidated | select -ExpandProperty customlastValidated
$ProjectCode = Get-ADComputer $Input -Properties customProjectCode | select -ExpandProperty customProjectCode
$Purpose = Get-ADComputer $Input -Properties customPurpose | select -ExpandProperty customPurpose
$Ticket = Get-ADComputer $Input -Properties customTicket | select -ExpandProperty customTicket
$POC = Get-ADComputer $Input -Properties customPOCmail | select -ExpandProperty customPOCmail

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#Window Options
$form = New-Object System.Windows.Forms.Form
$form.BackColor = "DimGray"
$form.Text = 'ComputerEdit for ' + $Computer
$form.FormBorderStyle = 'Fixed3D'
$form.Size = New-Object System.Drawing.Size(940,380)
$form.StartPosition = 'CenterScreen'

User avatar
jvierra
Posts: 13802
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:

Code: Select all

$location = $args[0]
$Input = ($location -split ',*..=')[1]
Then I changed any reference to $Input inside the function to $Script:Input.

Thanks!!

User avatar
jvierra
Posts: 13802
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

Post Reply