powershell 如何将AD中的设备移动到GUI中的其他OU

rdlzhqv9  于 2023-02-16  发布在  Shell
关注(0)|答案(1)|浏览(153)

我们在1个OU中部署了所有Autopilot设备,技术人员必须将它们移动到自己站点的OU中。我编写了一个GUI来执行此操作。他们输入设备名称和位置名称。位置名称是设备将驻留的最终OU。我的GUI获取站点的OU,并在Out-Gridview中列出它们,您单击所需的OU,然后单击“确定”。它会将其发送到文本框中。然后你点击移动。这就是我格尔的错误,该设备无法找到。我肯定我有一些愚蠢的语法错误。提前感谢。

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()
cqoc49vn

cqoc49vn1#

答案是调整doit函数

function doit{
$DEVICE = $TBX_APDEVICE.TEXT
$DestOU = $DESTINATION_OU.text

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

相关问题