2015-12-18 25 views
0

我使用Windows PowerShell获取下面的错误。有谁知道如何改正它?Obj.SetInfo()错误obj没有方法SetInfo()

ERROR: Method invocation failed because [Microsoft.ActiveDirectory.Management.ADObject] 
doesn't contain a method named 'SetInfo'. 
No.psf (97, 25): ERROR: At Line: 97 char: 25 

ERROR: + $computerObject.SetInfo <<<<() 
ERROR: + CategoryInfo   : InvalidOperation: (SetInfo:String) [], RuntimeException 
ERROR: + FullyQualifiedErrorId : MethodNotFound 

我很抱歉没有发布代码。我仍然有这个错误的问题。

$No_Load = { 
    # Load the ActiveDirectory module if it's available 
    # Check if the ActiveDirectory module is installed 
    if ((Get-Module -ListAvailable | where { $_.Name -eq 'ActiveDirectory' }) -eq $null) { 
    $labelDialogRedNewNo.Text += "You need to install the ActiveDirectory module!`n" 
    } else { 
    # Check if the ActiveDirectory module is allready Imported 
    if ((Get-Module ActiveDirectory) -eq $null) { 
     Import-Module ActiveDirectory -ErrorAction 'SilentlyContinue' 
     $labelDialogGreenNewNo.Text += "ActiveDirectory module imported`n" 
    } else { 
     $labelDialogGreenNewNo.Text += "ActiveDirectory already imported`n" 
    } 
    } 

    #Initialize variables 
    $dateTime = Get-Date -Format G 
    $computerName = (Get-WmiObject -Class Win32_ComputerSystem -Property Name).Name 
    $userName = (Get-WmiObject -Class Win32_ComputerSystem -Property UserName).UserName 

    #These Varables for display 
    $companyComputer = (Get-ADOrganizationalUnit -Filter 'Name -like "*Computers*"' | select name -ExpandProperty name) 
    $distinguishedName = (Get-ADComputer ($computerName) -Property DistinguishedName).DistinguishedName 
    $computerObject = (Get-ADObject ($distinguishedName)) 
    $location = (Get-ADObject ($distinguishedName) -Properties Location).Location 
    $departmentNumber = (Get-ADObject ($distinguishedName) -Properties DepartmentNumber).DepartmentNumber 
    $company = (Get-ADObject ($distinguishedName) -Properties Company).Company 
    $accountType = (Get-ADObject ($distinguishedName) -Properties ExtensionAttribute15).ExtensionAttribute15 

    #Initialize Form Controls 
    $txtBillingCodeNewNo.Text = $departmentNumber 
    $lblComputerNameNewNo.Text = $computerName 
    $lblUserNameNewNo.Text = $userName 
    $lblPhysicalLocationNewNo.Text = $location 
    $comboboxATNewNo.Text = $accountType 

    $comboboxOUNewNo.Text = $company 
    #$comboboxATNewNo.Text = $companyComputer 
    Load-ComboBox -ComboBox $comboboxOUNewNo (Get-ADOrganizationalUnit -SearchScope OneLevel -SearchBase 'OU=Agencies,DC=state,DC=in,DC=us' -Filter * -Properties Name | select name -ExpandProperty name) 
    #Load-ComboBox -ComboBox $comboboxOUNewNo (Get-ADOrganizationalUnit -Filter 'Name -like "*Computers*"' | select name -ExpandProperty name) 
} 

#region Control Helper Functions 
function Load-ComboBox { 
    Param (
    [ValidateNotNull()] 
    [Parameter(Mandatory=$true)] 
    [System.Windows.Forms.ComboBox]$ComboBox, 
    [ValidateNotNull()] 
    [Parameter(Mandatory=$true)] 
    $Items, 
    [Parameter(Mandatory=$false)] 
    [string]$DisplayMember, 
    [switch]$Append 
) 

    if (-not $Append) { 
    $ComboBox.Items.Clear() 
    } 

    if ($Items -is [Object[]]) { 
    $ComboBox.Items.AddRange($Items) 
    } elseif ($Items -is [Array]) { 
    $ComboBox.BeginUpdate() 
    foreach ($obj in $Items) { 
     $ComboBox.Items.Add($obj) 
    } 
    $ComboBox.EndUpdate() 
    } else { 
    $ComboBox.Items.Add($Items) 
    } 

    $ComboBox.DisplayMember = $DisplayMember 
} 

$btnSubmitNewNo_Click = { 
    $computerObject.name = 'IOT' 
    $computerObject.company = 'IOT' 
    $computerObject.departmentNumber = $departmentNumber 
    $computerObject.extensionAttribute15 = $accountType 
    $computerObject.SetInfo() 
} 

当我设置一个断点,我让所有的相关的值,最重要的是computerObject。但是,当我尝试SetInfo()时出现错误。有人能帮忙吗?再次,所有的computerObject.properties都有值,但我得到SetInfo()(第97行)的错误。

+0

对不起,但我的问题是没有人知道如何纠正这个错误 – Irwin

+5

向我们展示您正在运行的命令 –

+0

我已将它放入代码标记中,因为它使错误消息更清晰。我试过报价标签,但看起来和以前一样糟糕。我将OP的评论转移到帖子中:他们正在寻找任何可能解决此错误的想法。 – TwoStraws

回答

0

你可以通过Get-ADObject检索计算机对象:

$computerObject = (Get-ADObject ($distinguishedName)) 

这会返回一个Microsoft.ActiveDirectory.Management.ADObject对象不具有SetInfo()方法。

如果你想通过SetInfo()设置属性通过ADSI检索计算机对象:

$computerObject = [adsi]"LDAP://$distinguishedName" 

如果你想通过Set-ADObject检索设置属性通过Get-ADObject计算机对象:

Set-ADObject -Instance $computerObject 

然而,最好的方法,因为你正在处理计算机对象,可能会使用Get-ADComputerSet-ADComputer