2017-06-27 22 views
0

私はPowerShellアプリケーションで作業しています。このアプリケーションでは、テキストボックスからユーザーIDを入力し、ActiveDirectoryを検索して3つのフィールドを返す必要があります。しかし、私は次のエラー受け取る、私はそれを使用しようとするたび:PowerShell TextBoxからの入力の取得

Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again. 
At C:\Path\cc-lookup-gui.ps1:40 char:21 
+  $y = Get-ADUser $script:x -Properties cC 
+      ~~~~~~~~~ 
    + CategoryInfo   : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException 
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser 

は、ここに私のGUIや検索機能のためのコードです:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")|Out-Null 
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")|Out-Null 
$net = New-Object -ComObject Wscript.Network 


$form = New-Object System.Windows.Forms.Form 
$form.Width = 525 
$form.Height = 350 
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D 
$form.Text = "CC Lookup" 
$form.MaximumSize = New-Object System.Drawing.Size(525,350) 
$form.StartPosition = "centerscreen" 
$form.KeyPreview = $true 
$form.Add_KeyDown({if($_.KeyCode -eq "Enter"){$script:x=$input.Text;Search}}) 
$form.Add_KeyDown({if($_.KeyCode -eq "Escape"){$form.Close()}}) 

$input = new-object System.Windows.Forms.TextBox 
$input.maxLength = 6 
$input.Location = New-Object System.Drawing.Size(200,75) 


add-type -assemblyName System.Windows.Forms 
$label1 = New-Object System.Windows.Forms.Label 
$label1.Location = New-Object System.Drawing.Size(200,25) 
$label1.AutoSize = $true 
$label1.Text = "Enter User ID:" 

$Button1 = new-object System.Windows.Forms.Button 
$Button1.Location = New-Object System.Drawing.Size(100,132) 
$Button1.Size = New-Object System.Drawing.Size(80,20) 
$Button1.Text = "Search" 
$Button1.Add_Click({$script:x=$input.Text;Search}) 

$button2 = New-Object System.Windows.Forms.Button 
$button2.Location = New-Object System.Drawing.Size(300,132) 
$button2.Text = "Clear" 
$button2.Add_Click({Clears}) 

function Search{ 
    $y = Get-ADUser $script:x -Properties cC 
    $output = $y.samAccountName + '|' + $y.CN + '|' + $y.cC 
    Add-Type -AssemblyName System.Windows.Forms 
    $label = New-Object System.Windows.Forms.Label 
    $label.Text = $Output 
    $label.AutoSize = $true 
    $label.Location = New-Object System.Drawing.Size(200,100) 
    $form.controls.add($label) 
} 

function Clears{ 
    $label.Text = $null 
    $input.Text = $null 
} 

$form.Controls.Add($label1) 
$form.Controls.Add($button2) 
$form.Controls.Add($input) 
$form.Controls.Add($Button1) 
$form.Add_Shown({$Form.Activate()}) 
$Form.ShowDialog() 
$x = $input.Text 

私はすべて同様の問題の解決策を確認してきた私は、スタック上で無駄に見つけることができます。私は変数$ xをグローバルに宣言しようとしました。検索関数に$ input.textを直接呼び出し、$ xを文字列に変換しました。これらはすべてこの同じエラーを返します。 PowerShellバージョン5を実行しています。いつものように、どんな助けも大歓迎です。 help about_Automatic_Variablesを参照してください - - とあなたがあなたの{}スクリプトブロックで使用するとき、それは(この場合には何も)入力をスクリプトブロックしないように参照します期待しないだろう、代わりにあなたのInputBox関数の

+0

いずれのボタンも失敗します。私はそれから始めるだろう。 – TheIncorrigible1

+0

@TessellatingHeckler、それが問題でした。あなたが回答として投稿した場合、私は喜んでそれを受け入れる:) – Cameron

答えて

関連する問題