Obj.SetInfo()错误obj没有方法SetInfo()

时间:2015-12-18 21:08:05

标签: powershell active-directory

我使用Windows PowerShell收到以下错误。有谁知道如何纠正它?

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行)上收到错误。

1 个答案:

答案 0 :(得分:0)

您通过Get-ADObject检索计算机对象:

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

这将返回一个Microsoft.ActiveDirectory.Management.ADObject对象,该对象没有SetInfo()方法。

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

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

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

Set-ADObject -Instance $computerObject

但是,最好的办法是,因为你正在处理计算机对象,所以可能会使用Get-ADComputerSet-ADComputer