powershell 脚本 运行 来自 ISE,但来自 cli 有问题

powershell script running good from ISE but having problem from cli

我已经编写了一个 ps 脚本,当我从 ISE 执行它时它可以完成工作但是当我保存它并从 CMD 运行 它时我有一个错误, 基本上我已经用 OU 列表和 GPO 列表制作了 gui,我想制作一个 link 和 unlink 按钮(例如,OU 命名为 blabla,GPO 命名为 GPOblabla,所以如果我检查它们并按 link 代码会 link 而 unlink 会 unlink) 问题是(根据我的理解)在代码开头定义的 button2 (unlink) 函数,并且在该函数中我使用在代码后面使用参数的命令,我猜 ISE 保存它在记忆中?在这里最好的事情是什么? 这是代码:

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 }

在显示对话框 之前,您不会设置变量 $GPO$OU,因此这些变量不会在您的 $button2_click 脚本块,因此 Remove-GPLink 调用不会按预期工作。

解决问题的一种方法是直接引用$listBox.SelectedItem$listBox2.SelectedItem

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

请注意,如果您要在该脚本块 中定义变量 $GPO$OU ,则必须将它们定义为 $script:GPO = ...script:$OU = ... 如果您想在关闭对话框后也访问它们。


至于为什么在 ISE 中工作

ISE 对您的脚本进行 运行 点源,并且重复调用变量可能 延迟 并影响后续 运行s.

这意味着您至少有一次 运行 代码路径 $GPO = $listBox.SelectedItem$OU = $listBox2.SelectedItem 被击中,这将导致在随后的 [=47] 中点击按钮 2 =]s "work".