Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Winforms 将输入变量从PowerShell表单传递到cmdlet参数将导致空白数据_Winforms_Powershell_Active Directory_Powershell 2.0_Powershell 3.0 - Fatal编程技术网

Winforms 将输入变量从PowerShell表单传递到cmdlet参数将导致空白数据

Winforms 将输入变量从PowerShell表单传递到cmdlet参数将导致空白数据,winforms,powershell,active-directory,powershell-2.0,powershell-3.0,Winforms,Powershell,Active Directory,Powershell 2.0,Powershell 3.0,我试图从PowerShell表单中获取一个输入的文本字符串,如WIN,并查找所有计算机名称的前三个字母都是WIN 为了做到这一点,我构建了一个,只对代码进行了非常轻微的修改,以获取输入的文本字符串并将其保存为变量$global:x。我将该变量($global:x)传递到最后一行代码中的过滤器中,该过滤器输出到一个文件,但输出文件为空。我在谷歌上搜索了一下,将变量从$x更改为$global:x,但这不起作用 文件仍然是空的。我对TechNet代码示例的唯一更改是将变量从$x更改为$global:x

我试图从PowerShell表单中获取一个输入的文本字符串,如
WIN
,并查找所有计算机名称的前三个字母都是
WIN

为了做到这一点,我构建了一个,只对代码进行了非常轻微的修改,以获取输入的文本字符串并将其保存为变量$global:x。我将该变量(
$global:x
)传递到最后一行代码中的过滤器中,该过滤器输出到一个文件,但输出文件为空。我在谷歌上搜索了一下,将变量从
$x
更改为
$global:x
,但这不起作用

文件仍然是空的。我对TechNet代码示例的唯一更改是将变量从
$x
更改为
$global:x
,并在末尾添加过滤器;过滤器是代码中的最后一行,应该通过管道将变量结果传输到输出文件中

整个代码如下

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "FindSimiliarComputers"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$global:x=$objTextBox.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") 
    {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$global:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please enter the partial computer name"
$objForm.Controls.Add($objLabel) 

$objTextBox = New-Object System.Windows.Forms.TextBox 
$objTextBox.Location = New-Object System.Drawing.Size(10,40) 
$objTextBox.Size = New-Object System.Drawing.Size(260,20) 
$objForm.Controls.Add($objTextBox) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Get-ADComputer -filter {name -like "$global:x*" -and Enabled -eq "true" } | Select -expand Name | out-file C:\Users\Admin1\Desktop\computers.txt 
你的

将其更改为:

Get-ADComputer -filter "name -like '$global:x*' -and Enabled -eq 'true'" | Select -expand Name | out-file C:\Users\Admin1\Desktop\computers.txt 
你的剧本也会成功的


你所关注的文章已经过时了-有关于它的问题的讨论,还有2014年在

@JustinLahm上更新的新例子-感谢你的编辑,我在这个论坛上做新用户时就提出了这个问题。已批准并向您发送+1。节日快乐。。。