【问题标题】:Moving computer to another OU in OSD with PowerShell使用 PowerShell 将计算机移动到 OSD 中的另一个 OU
【发布时间】:2017-11-03 02:46:29
【问题描述】:

我在让这段代码在 TS 环境中工作时遇到了很多麻烦。

在 Windows 环境下,它运行良好。通过谷歌搜索此代码,您会发现几乎所有变体在 Windows 中都可以正常工作。但是,在任务序列中,这些变体只会产生不同的错误消息,说明计算机无法移动的原因。

  1. 未指定的错误
  2. 指定的域不存在或无法联系
  3. 对象的实例未设置为对象

当我们调用psbase.MoveTo() 方法时,我感觉发生了一些事情。到那时,它可以打印出看起来像一个合理对象的东西。换句话说,它们不是 null 之类的。

然后psbase.MoveTo() 说不。

示例代码。

$logFile = "MoveComputerLog.txt"

# Domain Credentials 
$account = "domain\osdaccount"
$password = "thepassword"

function logMessage {
    param ([string]$logstring)

    Write-Host $logstring

    Add-content $logFile -value $logstring
}


$computerName = "COMPUTERNAME"


logMessage "computerName: $computerName"

$root = "LDAP://sweet.domain.com"
$domain = New-Object System.DirectoryServices.DirectoryEntry($root, $account, $password)
$search = New-Object System.DirectoryServices.DirectorySearcher($domain)

$search.filter = "(&(objectClass=computer)(name=$computerName))"

$result = $search.findall() 


$computerDN = $result.Properties.Item("DistinguishedName") 

logMessage "DN: $computerDn"


$computer = New-Object System.DirectoryServices.DirectoryEntry("LDAP://$computerDN", $account, $password)

logMessage "Computer: $computer"


$destination = "LDAP://ou=here,ou=goes,ou=it,dc=sweet,dc=domain,dc=com"
$ou = New-Object System.DirectoryServices.DirectoryEntry($destination, $account, $password) 


try {
    # "The specified domain couldn't be connected or doesn't exist."
    $computer.psbase.MoveTo($ou.Path)

} catch {
    Write-Host "Encountered error while moving $computerName"

    logMessage $error[0]
}

【问题讨论】:

    标签: powershell move ou


    【解决方案1】:

    这是定义,ADSI 很重要:

    PS C:\> $computer.psbase.MoveTo
    
    OverloadDefinitions
    -------------------
    void MoveTo(adsi newParent)
    void MoveTo(adsi newParent, string newName)  
    

    你可以试试这个,在 $result = $search.findall() 之后:

    $computer = [ADSI]$result.path
    $computer.psbase.Moveto( [ADSI]LDAP://ou=here,ou=goes,ou=it,dc=sweet,dc=domain,dc=com )
    

    【讨论】:

    • 谢谢,但遇到同样的错误。我在我的问题中发布的代码适用于 Windows。但在 PE 环境中,情况有所不同。
    猜你喜欢
    • 1970-01-01
    • 2010-09-09
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多