Issue in moving some workstations to a different OU RRS feed

  • Question

  • Hi Everyone

    I have been assigned a task to look for all workstations(which has OU=Expired Workstations and OU=Domain Computers) and move them to their original OU

    My DistinguishedName of Get-ADComputer looks like this 

    DistinguishedName : CN=CAWLK30446,OU=Workstations,OU=PR-Canada,DC=pr-americas,DC=pernod-ricard,DC=group

    Full script is below:

    # Identify and select all enabled workstations in "Domain Computers" and "Expired Workstations" OUs, and all workstations with a valid OS
    $computers = get-adcomputer -Filter 'ObjectClass -eq "Computer"' -properties name, OperatingSystem

    $OU = $computers| ? {(($_.DistinguishedName -like "*OU=Domain Computers*") -or ($_.DistinguishedName -like "*OU=Expired Workstations*")) -and ($_.Enabled -like "True")`
    -and (($_.OperatingSystem -like "Windows 10*") -or ($_.OperatingSystem -like "Windows 7*") -or ($_.OperatingSystem -like "Windows 8.1*"))}

    $SiteCode = "P71" # Site code 
    $ProviderMachineName = "CAWLKAP268.pr-americas.pernod-ricard.group" # SMS Provider machine name
    $Import = "\\pracmsources\root$\PowerShellCmdlets\bin\ConfigurationManager.psd1"

    # Customizations
    $initParams = @{}
    # Import the ConfigurationManager.psd1 module 
    if((Get-Module ConfigurationManager) -eq $null) {
        Import-Module Microsoft.PowerShell.Core\FileSystem::$Import @initParams 
    # Connect to the site's drive if it is not already present
    if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
        New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
    Set-Location P71:
    #Clear Previous Data
    $Output = $null
    $Info = $null
    $DeviceName = $null
    $ComputerOU = $null
    $UserArray = @{}
    $Stampf = (Get-Date).toString("yyyyMMdd_hhmmsstt")
    $Logfile1 = "Microsoft.PowerShell.Core\FileSystem::\\cawlkcst4\manage$\Logs\MoveWorkstations\Success\$stampf users_success.log"
    $Logfile2 = "Microsoft.PowerShell.Core\FileSystem::\\cawlkcst4\manage$\Logs\MoveWorkstations\Failed\$stampf users_failed.log"
    #Write-Host $logfile

    Function LogWrite1
       Param ([string]$logstring)
       $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
       $Line = "$Stamp $logstring"
       Add-Content $Logfile1 -value $Line
    Function LogWrite2
       Param ([string]$logstring)
       $Stamp = (Get-Date).toString("yyyy/MM/dd HH:mm:ss")
       $Line = "$Stamp $logstring"
       Add-Content $Logfile2 -value $Line

    # Retrieve information and move workstations to corresponding OUs
    ForEach($computer in $OU){
        $DeviceName = $computer.Name
        $OriginalOU1 = $computer.DistinguishedName
     if($OriginalOU1 -like "*OU=Requires No Policy*"){
     $temporary = $OriginalOU1 -split ","
     $OriginalOU = ($temporary[2]).substring(3)
     $temporary = $OriginalOU1 -split ","
     $OriginalOU = ($temporary[1]).substring(3)

        # Define function
        $Info = Get-CMUserDeviceAffinity -DeviceName $DeviceName
        $ComputerOU = Get-ADComputer $DeviceName | ?{($_.DistinguishedName -notlike "*OU=Requires No Policy*")}
        If ($Info -eq $null){ 
            LogWrite2 -ForegroundColor Red "Unable to find any Primary Users for $DeviceName and the ComputerOU is $OriginalOU"
        elseif ($ComputerOU -eq $null){
        #LogWrite2 "As the Device($DeviceName) is in ""Requires No Policy OU"", it will not be moved"
        Else {
            $Output = $info.UniqueUserName
            If($Output.length -le 5 ){
                If($Output[0] -like "*\local_users"){
                    $Output = $Output[1]
                    $Output = $Output[0]
                If($Output -like "*\local_users"){
                    LogWrite2 -ForegroundColor Red "Unable to find any Primary Users for $DeviceName and the computerOU is $OriginalOU"
                If($Output -like "pr-americas\*"){
                    $Output = $Output.substring(12)
                    $erroroccured = $false
                    $ErrorActionPreference = 'Stop'
                    $temp = Get-ADUser $Output
                    LogWrite2 "user - $Output does not exist in AD"
                    $erroroccured = $true
                    $split = ($temp.DistinguishedName) -split ","
                    $DeviceOU = ($split[2]).substring(3)
                    If($computer.DistinguishedName -like "*OU=Domain Computers*"){
                        $b = $computer.DistinguishedName
                        $origou="Domain Computers"
                        $b = $b -replace "OU=Domain Computers", "OU=$DeviceOU"
                        $b = $computer.DistinguishedName
                        $origou="Expired Workstations"
                        $b = $b -replace "OU=Expired Workstations", "OU=$DeviceOU"
                    Get-ADComputer $DeviceName | Move-ADObject -TargetPath $b             
                   LogWrite1 -ForegroundColor Green "Moved the following machine $DeviceName ($Output) from $origou to $DeviceOU"


    # Set location back to HOME
    Set-Location $HOME

    And am getting below error:

    Move-ADObject : The operation could not be performed because the object's parent is either uninstantiated or deleted

    At E:\Automic\Automation.Platform\Agents\windows\TEMP\JAGFYSRA.TXT.ps1:138 char:46

    + ...             Get-ADComputer $DeviceName | Move-ADObject -TargetPath $b

    +                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        + CategoryInfo          : NotSpecified: (CN=CAWLK31281,O...ricard,DC=group:ADComputer) [Move-ADObject], ADExceptio


        + FullyQualifiedErrorId : ActiveDirectoryServer:8329,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

    Can anyone help me out on this error?

    surya teja yarlagadda

    Monday, April 23, 2018 2:16 PM

All replies

  • The original OU no longer exists. 


    Monday, April 23, 2018 3:08 PM
  • I did not get your point. What do you mean by original OU no longer exists.

    surya teja yarlagadda

    Monday, April 23, 2018 4:07 PM
  • Read the error message completely: "The operation could not be performed because the object's parent is either uninstantiated or deleted"

    The "original parent" is the container  that the object was deleted from.


    Monday, April 23, 2018 5:39 PM