2023-01-31

How to move a device in AD to a different OU in a GUI

We have all of our Autopilot deployed devices in 1 OU and the techs have to move them to their own site's OU I have written a GUI to do this. they enter the device name and the location name. The location name is tha final ou the device will reside in. My GUI gets the OUs for the site and lists them in a Out-Gridview you click on the ou you want and click ok. it sends that to a textbox. then you click move. thats where I ger the error that the device cannot be found. I am sure I have some silly syntax wrong. Thanks in advance.

Add-Type -Name Window -Namespace Console -MemberDefinition '
[DllImport("Kernel32.dll")]
public static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow);'

[Console.Window]::ShowWindow([Console.Window]::GetConsoleWindow(), 0)
<# 
.NAME
    AP Device Move
#>

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()

$Form                            = New-Object system.Windows.Forms.Form
$Form.ClientSize                 = New-Object System.Drawing.Point(400,400)
$Form.text                       = "Form"
$Form.TopMost                    = $false

$LBL_APDEVICE                    = New-Object system.Windows.Forms.Label
$LBL_APDEVICE.text               = "Computer Name"
$LBL_APDEVICE.AutoSize           = $true
$LBL_APDEVICE.width              = 25
$LBL_APDEVICE.height             = 10
$LBL_APDEVICE.location           = New-Object System.Drawing.Point(0,2)
$LBL_APDEVICE.Font               = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TBX_APDEVICE                    = New-Object system.Windows.Forms.TextBox
$TBX_APDEVICE.Text               = ""
$TBX_APDEVICE.multiline          = $false
$TBX_APDEVICE.width              = 100
$TBX_APDEVICE.height             = 20
$TBX_APDEVICE.location           = New-Object System.Drawing.Point(6,37)
$TBX_APDEVICE.Font               = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$LBL_SITE                        = New-Object system.Windows.Forms.Label
$LBL_SITE.text                   = "Site Name"
$LBL_SITE.AutoSize               = $true
$LBL_SITE.width                  = 25
$LBL_SITE.height                 = 10
$LBL_SITE.location               = New-Object System.Drawing.Point(4,71)
$LBL_SITE.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$LOCATION                        = New-Object system.Windows.Forms.TextBox
$LOCATION.multiline              = $false
$LOCATION.width                  = 100
$LOCATION.height                 = 20
$LOCATION.location               = New-Object System.Drawing.Point(3,101)
$LOCATION.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$DESTINATION_OU                  = New-Object system.Windows.Forms.TextBox
$DESTINATION_OU.text             = ""
$DESTINATION_OU.multiline        = $false
$DESTINATION_OU.width            = 100
$DESTINATION_OU.height           = 20
$DESTINATION_OU.location         = New-Object System.Drawing.Point(14,194)
$DESTINATION_OU.Font             = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$TARGET_OU                       = New-Object system.Windows.Forms.Label
$TARGET_OU.text                  = "Target OU"
$TARGET_OU.AutoSize              = $true
$TARGET_OU.width                 = 25
$TARGET_OU.height                = 10
$TARGET_OU.location              = New-Object System.Drawing.Point(12,139)
$TARGET_OU.Font                  = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Get_OU                          = New-Object system.Windows.Forms.Button
$Get_OU.text                     = "Get OUs for Site"
$Get_OU.width                    = 104
$Get_OU.height                   = 30
$Get_OU.location                 = New-Object System.Drawing.Point(133,54)
$Get_OU.Font                     = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$BTN_MOVE                        = New-Object system.Windows.Forms.Button
$BTN_MOVE.text                   = "Move Device"
$BTN_MOVE.width                  = 91
$BTN_MOVE.height                 = 30
$BTN_MOVE.location               = New-Object System.Drawing.Point(34,246)
$BTN_MOVE.Font                   = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$Form.controls.AddRange(@($LBL_APDEVICE,$TBX_APDEVICE,$LBL_SITE,$LOCATION,$DESTINATION_OU,$TARGET_OU,$Get_OU,$BTN_MOVE))

$Get_OU.Add_Click({ GetSiteOUs })
$BTN_MOVE.Add_Click({ doit })

#region Logic 
function GetSiteOUs  {
  $DESTINATION_OU.Text = Get-ADOrganizationalUnit -Filter "Name -Like '*$($LOCATION.Text.Trim())*'" |
        Select-Object -ExpandProperty 'Distinguishedname' |
        Out-GridView -PassThru -Title "Select the OU"
        
         }
         

function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = "OU=$DESTINATION_OU.text,OU=Computers,OU=World,OU=Disney,OU=Goofy,OU=Duck,OU=Donald,DC=Mickey,DC=Mouse,"

Move-ADObject –Identity "CN=$Device,OU=Autopilot,OU=Lucy,OU=linus,OU=Brown,OU=charlie,DC=Mickey,DC=Mouse," -TargetPath $DestOU
}
#endregion

[void]$Form.ShowDialog()


No comments:

Post a Comment