【问题标题】:Exchange with Powershell - Weird Error与 Powershell 交换 - 奇怪的错误
【发布时间】:2015-07-03 19:17:56
【问题描述】:

我输入了我的代码,但它抛出了一个错误。代码运行良好,在我尝试添加保管箱功能之前,我能够让它运行。我似乎无法弄清楚这有什么问题......

Method invocation failed because [System.Reflection.RuntimeAssembly] does not contain a method named 'op_A
At C:\scripts\tmail1.ps1:127 char:1
+ $return2 = ($return[0] + " " + $return[1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.Reflection.RuntimeAssembly] does not contain a method named 'op_A
At C:\scripts\tmail1.ps1:128 char:1
+ $return3 = ($return[0] + "." + $return[1])
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Method invocation failed because [System.Reflection.RuntimeAssembly] does not contain a method named 'op_A
At C:\scripts\tmail1.ps1:129 char:1
+ $return4 = $return[0] + "." + $return[1] + $return[2]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Cannot validate argument on parameter 'Alias'. The argument is null or empty. Provide an argument that is
empty, and then try the command again.
    + CategoryInfo          : InvalidData: (:) [New-Mailbox], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,New-Mailbox
    + PSComputerName        : pqhiexch01.xxxxxxx.com

Cannot bind argument to parameter 'Identity' because it is null.
    + CategoryInfo          : InvalidData: (:) [Set-User], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Set-User

我的代码如下:

function button ($title,$mailbx, $WF, $TF) {

###################Load Assembly for creating form & button######

[void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
[void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)

#####Define the form size & placement

$form = New-Object “System.Windows.Forms.Form”;
$form.Width = 750;
$form.Height = 500;
$form.Text = $title;
$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;

##############Define text label1
$textLabel1 = New-Object “System.Windows.Forms.Label”;
$textLabel1.Left = 25;
$textLabel1.Top = 15;

$textLabel1.Text = $mailbx;

##############Define text label2

$textLabel2 = New-Object “System.Windows.Forms.Label”;
$textLabel2.Left = 25;
$textLabel2.Top = 50;

$textLabel2.Text = $WF;

##############Define text label3

########################

# Edit This item to change the DropDown Values

[array]$DropDownArray = "@yahoo.com", "@gmail.com", "@msn.com"

$textLabel3 = New-Object “System.Windows.Forms.Label”;
$textLabel3.Left = 25;
$textLabel3.Top = 85;

$textLabel3.Text = $TF;

############Define text box1 for input
$textBox1 = New-Object “System.Windows.Forms.TextBox”;
$textBox1.Left = 150;
$textBox1.Top = 10;
$textBox1.width = 200;

############Define text box2 for input

$textBox2 = New-Object “System.Windows.Forms.TextBox”;
$textBox2.Left = 150;
$textBox2.Top = 50;
$textBox2.width = 200;

############Define text box3 for input

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {

 $Choice = $DropDown.SelectedItem.ToString()
 }

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




$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(150,90)
$DropDown.Size = new-object System.Drawing.Size(200,30)

ForEach ($Item in $DropDownArray) {
 $DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(150,90) 
$DropDownLabel.size = new-object System.Drawing.Size(100,20) 
$DropDownLabel.Text = "Items"
#############Define default values for the input boxes
$defaultValue = “”
$textBox1.Text = $defaultValue;
$textBox2.Text = $defaultValue;


#############define OK button
$button = New-Object “System.Windows.Forms.Button”;
$button.Left = 360;
$button.Top = 85;
$button.Width = 100;
$button.Text = “Ok”;

############# This is when you have to close the form after getting values
$eventHandler = [System.EventHandler]{
$textBox1.Text;
$textBox2.Text;
$dropdown.Items;
$form.Close();};

$button.Add_Click($eventHandler) ;

#############Add controls to all the above objects defined
$form.Controls.Add($button);
$form.Controls.Add($textLabel1);
$form.Controls.Add($textLabel2);
$form.Controls.Add($textLabel3);
$form.Controls.Add($textBox1);
$form.Controls.Add($textBox2);
$Form.Controls.Add($DropDownLabel)
$Button.Add_Click({Return-DropDown});
$ret = $form.ShowDialog();

#################return values

return $textBox1.Text, $textBox2.Text, $textBox3.Text
}

$return= button “Enter Info” “First Name” “Last Name” “Email Address”
$return2 = ($return[0] + " " + $return[1])
$return3 = ($return[0] + "." + $return[1])
$return4 = $return[0] + "." + $return[1] + $return[2]


New-Mailbox -Alias $return3 -Name $return2 -FirstName $return[0] -LastName $return[1] -UserPrincipalName $return4  -Password (ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
Set-User -Identity $return3 -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'

【问题讨论】:

    标签: powershell exchange-server


    【解决方案1】:

    这可能有点误导,但错误是说它不知道如何将 + 运算符与 $return[0] 对象一起使用。例如,字符串和整数可以与 + 运算符一起使用。

    您错过了复制整个错误。考虑以下代码。我将尝试错误地向文件夹名称对象添加空格。

    $folder = Get-Item "C:\temp"
    $folder + " "
    
    Method invocation failed because [System.IO.DirectoryInfo] doesn't contain a method named 'op_Addition'.
    At line:2 char:1
    + $folder + " "
    + ~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound
    

    op_Addition 是您帖子中被截断的文本。

    您的实际问题

    即使您有一个 return 语句,一个函数也会将 all 输出返回到输出流。您需要将额外的输出通过管道传输到 null。铸造到[void] 或管道到Out-Null 是两种常见的方法。

    这些行是实际问题

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

    需要通过管道使输出为空/无效。 (我从编辑中看到您已经知道了这一点。)

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

    【讨论】:

    • 当我以这种方式输入时,它会显示无法将参数“名称”绑定到目标。异常设置“名称”:“属性长度过长。最大长度为 64,提供的值长度为 169。” + CategoryInfo : WriteError: (:) [New-Mailbox], ParameterBindingException + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.Exchange.Management.RecipientTasks.NewMailbox
    • @TylerS $return2 3,4 如果您在控制台上打印它们会是什么样子?
    • System.Windows.Forms,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 System.Drawing,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms ,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089.System.Drawing,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d5 0a3a System.Windows.Forms,版本=4.0.0.0,文化=中性,PublicKeyToken =b77a5c561934e089.System.Drawing,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d5 0a3a0
    • @TylerS 是的。你的问题我错了。您的函数返回的结果超出了您的预期。您可能已经在其他答案中解决了这个问题。您需要先将该输出过滤为 null。
    【解决方案2】:

    如果我运行此代码,它可以正常工作。有没有人可以在表单中输入下拉框?

    function button ($title,$mailbx, $WF, $TF) {
    
    ###################Load Assembly for creating form & button######
    
    [void][System.Reflection.Assembly]::LoadWithPartialName( “System.Windows.Forms”)
    [void][System.Reflection.Assembly]::LoadWithPartialName( “Microsoft.VisualBasic”)
    
    #####Define the form size & placement
    
    $form = New-Object “System.Windows.Forms.Form”;
    $form.Width = 750;
    $form.Height = 500;
    $form.Text = $title;
    $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen;
    
    ##############Define text label1
    $textLabel1 = New-Object “System.Windows.Forms.Label”;
    $textLabel1.Left = 25;
    $textLabel1.Top = 15;
    
    $textLabel1.Text = $mailbx;
    
    ##############Define text label2
    
    $textLabel2 = New-Object “System.Windows.Forms.Label”;
    $textLabel2.Left = 25;
    $textLabel2.Top = 50;
    
    $textLabel2.Text = $WF;
    
    ##############Define text label3
    
    $textLabel3 = New-Object “System.Windows.Forms.Label”;
    $textLabel3.Left = 25;
    $textLabel3.Top = 85;
    
    $textLabel3.Text = $TF;
    
    ############Define text box1 for input
    $textBox1 = New-Object “System.Windows.Forms.TextBox”;
    $textBox1.Left = 150;
    $textBox1.Top = 10;
    $textBox1.width = 200;
    
    ############Define text box2 for input
    
    $textBox2 = New-Object “System.Windows.Forms.TextBox”;
    $textBox2.Left = 150;
    $textBox2.Top = 50;
    $textBox2.width = 200;
    
    ############Define text box3 for input
    
    $textBox3 = New-Object “System.Windows.Forms.TextBox”;
    $textBox3.Left = 150;
    $textBox3.Top = 90;
    $textBox3.width = 200;
    
    #############Define default values for the input boxes
    $defaultValue = “”
    $textBox1.Text = $defaultValue;
    $textBox2.Text = $defaultValue;
    $textBox3.Text = $defaultValue;
    
    #############define OK button
    $button = New-Object “System.Windows.Forms.Button”;
    $button.Left = 360;
    $button.Top = 85;
    $button.Width = 100;
    $button.Text = “Ok”;
    
    ############# This is when you have to close the form after getting values
    $eventHandler = [System.EventHandler]{
    $textBox1.Text;
    $textBox2.Text;
    $textBox3.Text;
    $form.Close();};
    
    $button.Add_Click($eventHandler) ;
    
    #############Add controls to all the above objects defined
    $form.Controls.Add($button);
    $form.Controls.Add($textLabel1);
    $form.Controls.Add($textLabel2);
    $form.Controls.Add($textLabel3);
    $form.Controls.Add($textBox1);
    $form.Controls.Add($textBox2);
    $form.Controls.Add($textBox3);
    $ret = $form.ShowDialog();
    
    #################return values
    
    return $textBox1.Text, $textBox2.Text, $textBox3.Text
    }
    
    $return= button “Enter Info” “First Name” “Last Name” “Email Address”
    $return2 = ($return[0] + " " + $return[1])
    $return3 = ($return[0] + "." + $return[1])
    $return4 = $return[0] + "." + $return[1] + $return[4]
    
    
    New-Mailbox -Alias $return3 -Name $return2 -FirstName $return[0] -LastName $return[1] -UserPrincipalName $return4  -Password (ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
    Set-User -Identity $return3 -StreetAddress '1600 Pennsylvania Ave NW' -City 'Washington' -StateOrProvince 'D.C.' -PostalCode '20500' -Phone '202-456-1111' -Fax '202-456-2461'
    

    【讨论】:

    • 似乎我们都发现了您对这个问题的问题。由于您现在遇到了另一个问题,请考虑提出新问题,因为该问题已得到解答。
    【解决方案3】:

    我也有同样的错误信息,但这是因为代码中的错误:

    $NUFirstname =  $mTextBox1
    $NULastname  =  $mTextBox2
    

    应该是:

    $NUFirstname =  $mTextBox1.text
    $NULastname  =  $mTextBox2.text
    

    干杯, 拉德贡达

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多