Need help

Hi community,

i try to create handy tools for HD team, tools should update computer description and then move computer to selected computer OU. 1st part working ok, but when i try to move computer to OU i get error

Code:

$reader=(New-Object System.Xml.XmlNodeReader $xaml) 
try{$Form=[Windows.Markup.XamlReader]::Load( $reader )}
catch{Write-Host "Unable to load Windows.Markup.XamlReader."; break}
$xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name)}

### Computers ###
#$computers = (Get-ADComputer -Filter ' Enabled -eq "True" ') | Sort-Object Name
$computers = (Get-ADComputer -Filter * -SearchBase "OU=Test_COMPUTERS,DC=enterprise,DC=corp") | Sort-Object Name
$cmbComputers.ItemsSource = $computers.Name
#################

### AD OU ###
$TEST_OU = "OU=TEST,DC=enterprise,DC=corp"
$TEST2_OU = "OU=TEST2,DC=enterprise,DC=corp"

$TEST_OU = (Get-ADOrganizationalUnit -SearchScope Subtree -Filter * -SearchBase "$TEST_OU")| Sort-Object Name
$TEST2_OU = (Get-ADOrganizationalUnit -SearchScope Subtree -Filter * -SearchBase "$TEST2_OU")| Sort-Object Name
$OU = $TEST_OU.Name + $TEST2_OU.Name
$cmbOU.ItemsSource = $OU
#############

$cmbComputers.Add_SelectionChanged({
    $script:selectedComputer = $computers.Where({$_.Name -eq $cmbComputers.SelectedItem.ToString()})
    $cmbComputers.SelectedItem.ToString()
    $selectedComputer = $cmbComputers.SelectedItem.ToString()
   
})

$cmbOU.Add_SelectionChanged({
    $script:selectedOU = $OU.Where({$_.Name -eq $cmbOU.SelectedItem.ToString()})
    $SelectedOU = $cmbOU.SelectedItem.ToString()   
   
})

$btnUpdateDescription.Add_Click({
    $description = $txtDescription.Text
    Set-ADComputer $cmbComputers.SelectedItem.ToString() -Description "$description"
    $txtDescription.Clear()
})

$btnMoveToOU.Add_Click({
    
    $targetOU = "OU=$cmbOU.SelectedItem.ToString(),DC=enterprise,DC=corp"
       
    Move-ADObject $selectedComputer -TargetPath $targetOU
    ### Refresh OU ###
      #???
    $txtDescription.Clear()
})

$Form.ShowDialog() | Out-Null

It’ll be great if you can update the post with the error.

Error:
Move-ADObject : Cannot convert ‘System.Collections.ObjectModel.Collection`1[System.Management.Automation.PSObject]’ to the type ‘Microsoft.ActiveDirectory.Management.ADObject’ required by
parameter ‘Identity’. Specified method is not supported.
At C:\Users\tester\test.ps1:102 char:19

  • Move-ADObject $selectedComputer -TargetPath $targetOU
    
  •               ~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : InvalidArgument: (:slight_smile: [Move-ADObject], ParameterBindingException
    • FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.Management.Commands.MoveADObject

Thanks

As the error states, please make sure on the type of input argument for -identity parameter. You can try printing the value and type of the input argument.

$selectedComputer

$selectedComputer.gettype()

I believe, -Identity can accept string, but it should be a valid identity.

Move-ADObject is expecting an ADObject for parameter -Identity. You are providing a string.

 

$selectedComputer = $cmbComputers.SelectedItem.ToString()


Move-ADObject $selectedComputer

 

 

Instead, try

$selectedComputer = $cmbComputers.SelectedItem.ToString()


$computer = Get-ADComputer -Identity $selectedComputer 

Move-ADObject -Identity $computer -TargetPath $targetOU

 

Thank you to both of you
i ended with this:

$targetOU = $cmbOU.SelectedItem.ToString()
    $selectedComputer = $cmbComputers.SelectedItem.ToString()
    $computer = Get-ADComputer -Identity $selectedComputer
    $MoveToOU = "OU=$targetOU,DC=enterprise,DC=corp"
    Move-ADObject -Identity $computer -TargetPath $MoveToOU

now all working, Thanks
One more question - how i can refresh OU after i moved computer, because computer not appear until i manually refresh OU
Thanks