简体   繁体   中英

powershell script running good from ISE but having problem from cli

I have written a ps script which do the the job when i execute it from ISE but when i save it and run it from CMD i have an error, basicly i have made gui with list of OU and list of GPO and i wanted to make a link and unlink button (eg OU named blabla and GPO named GPOblabla so if i check them both and press link the code would link and unlink would unlink) the problem is (with my understanding) with the button2 (unlink) function which is defined at the beginning of the code, and in that function im using command that use parameters later in the code, i guess ISE save it in memory? whats the best thing to do here? here is the code:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$button2_click = {
try {Remove-GPLink -Name $GPO -target $OU}
catch {
Write-Warning $Error[0]
} $form.close() }

$form = New-Object System.Windows.Forms.Form
$form.Text = 'GPO Connector V1.0'
$form.Size = New-Object System.Drawing.Size(600,200)
$form.StartPosition = 'CenterScreen'

$button1 = New-Object System.Windows.Forms.Button
$button1.Location = New-Object System.Drawing.Point(10,120)
$button1.Size = New-Object System.Drawing.Size(75,23)
$button1.Text = 'Link'
$button1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $button1

$button2 = New-Object System.Windows.Forms.Button
$button2.Location = New-Object System.Drawing.Point(90,120)
$button2.Size = New-Object System.Drawing.Size(75,23)
$button2.Text = 'UnLink'
$Button2.Add_Click($Button2_Click)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(80,20)
$label.Size = New-Object System.Drawing.Size(480,20)
$label.Text = 'SELECT GPO And Corresponding OU (ONLY WORKSTATION OU)'

$form.Controls.Add($label)
$form.Controls.Add($button1)
$form.Controls.Add($button2)

$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80

$listBox2 = New-Object System.Windows.Forms.ListBox
$listBox2.Location = New-Object System.Drawing.Point(300,40)
$listBox2.Size = New-Object System.Drawing.Size(260,20)
$listBox2.Height = 80

#gpolist.txt holding the gpo's and oulist.txt would hold the ou's
Get-Content .\gpolist.txt | ?{$_ -notmatch "^#"}  | Where-Object { $_.Trim() -ne '' } | ForEach-Object {
[void] $listBox.Items.Add("$_")}

Get-Content .\oulist.txt | ?{$_ -notmatch "^#"} | Where-Object { $_.Trim() -ne '' } | ForEach-Object {
[void] $listBox2.Items.Add("$_")}

$form.Controls.Add($listBox)
$form.Controls.Add($listBox2)
$form.Topmost = $true
$result = $form.ShowDialog()


if ($result -eq [System.Windows.Forms.DialogResult]::Cancel)
{   break   }


if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$GPO = $listBox.SelectedItem
$OU = $listBox2.SelectedItem    }

New-GPLink $GPO -target $OU | out-null }

You're not setting variables $GPO and $OU until after showing the dialog, so these variables won't be defined in your $button2_click script block, so the Remove-GPLink call won't work as expected.

One way to solve the problem is to refer to both $listBox.SelectedItem and $listBox2.SelectedItem directly:

$button2_click = {
  try { 
    Remove-GPLink -Name $listBox.SelectedItem -target $listBox2.SelectedItem 
  }
  catch {
    Write-Warning $Error[0]
  }
  $form.close()
}

Note that if you were to define variables $GPO and $OU inside that script block , you'd have to define them as $script:GPO =... and script:$OU =... if you wanted to also access them after closing the dialog.


As for why things worked in the ISE :

The ISE dot-sources your scripts when it runs them, and with repeated invocations variables may linger and affect subsequent runs.

The implication is that you had run at least once with the code path $GPO = $listBox.SelectedItem and $OU = $listBox2.SelectedItem getting hit, which would have made clicking on button 2 in subsequent runs "work".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM