Powershell脚本在ISE上运行良好,但在cli上存在问题

我编写了一个ps脚本,当我从ISE执行该脚本时就可以完成该工作,但是当我保存它并从CMD运行它时,我遇到了一个错误, 基本上,我已经用OU列表和GPO列表制作了gui,我想创建一个链接和取消链接按钮(例如,名为blabla的OU和名为GPOblabla的GPO,因此如果我同时选中它们并按链接,代码将链接和取消链接将取消链接) 问题是(据我所知)是在代码开头定义的button2(取消链接)功能,在该功能中,我使用了稍后在代码中使用参数的命令,我想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 }
评论
jalias
jalias

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 $listBox2.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 $scrip:GPO = ... and script:$OU = ... if you wanted to also access them after closing the dialog.

至于为什么事情在ISE工作:

ISE在运行脚本时以点源方式运行它们,并且重复调用变量可能会持续存在并影响后续运行。

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

点赞
评论
99.90%
99.90%

仅存在一些语法问题。

第48行:您无法像这样结束一行,而是继续尝试下一行。因为这行很长,所以更容易阅读的方法是这样的:

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

然后在第52行有一个类似的问题:

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

With both changes, your UI renders. enter image description here

如果这样做有帮助,请记住将其标记为答案并投票:)

点赞
评论