#Define Default Path
$DefaultPath = "CN=Computers,DC=datacenter-flo,DC=deb" #Default Computer Path
#Define Hostgroup
$HVGroupName = "DL-CG-Hyper-V-Hosts" #Hyper-V Group Name
$HVGroupPath = "OU=Hyper-V,OU=Computer,OU=Groups,OU=DC-Berlin,DC=datacenter-flo,DC=deb" #Grouppath of Hyper-V Hosts
$HVGroupScope ="DomainLocal" #Groupscope could be DomainLocal , Global or Universal
$HVGroupCategory ="Security" #Groupcategory could be Security or Distribution (only use Security)
#Define Hosts
$HVHostPath = "OU=Hyper-V,OU=Server,OU=DC-Berlin,DC=datacenter-flo,DC=deb" #Path of Hyper-V Hosts
$HVHostW = "*SVR-HV*" #Hyper-V Search Value
#Move Hosts from Computers to OU
$HVHosts = Get-ADComputer -LDAPFilter "(name=$HVHostW)" -SearchBase "$DefaultPath"
#Result Host Search
foreach ($Srv in $HVHosts)
{
$outprint = $Srv.SamAccountName
Write-Host "$outprint was found in $DefaultPath" -ForegroundColor Green
}
if(!$HVHosts) {
write-host "No Computers are found in default container" -ForegroundColor Yellow
return
}
foreach ($Svr in $HVHosts) {
$outprint = $Srv.SamAccountName
Write-Host " " -ForegroundColor Green
if(!(Move-ADObject $Svr -TargetPath $HVHostPath)) {
$Status = "SUCCESS"
} else {
$Status = "FAILED"
}
$OutputObj = New-Object -TypeName PSobject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Svr.Name.tostring()
$OutputObj | Add-Member -MemberType NoteProperty -Name SourcePath -Value $DefaultPath
$OutputObj | Add-Member -MemberType NoteProperty -Name DestinationPath -Value $HVHostPath
$OutputObj | Add-Member -MemberType NoteProperty -Name Status -Value $Status
$OutputObj
}
Write-Host " " -ForegroundColor Green
#Define Identities
$HVGroups = Get-ADGroup -LDAPFilter "(name=$HVGroupName)" -SearchBase "$HVGroupPath"
$HVHosts = Get-ADComputer -LDAPFilter "(name=$HVHostW)" -SearchBase "$HVHostPath"
#Result Host Search
foreach ($Srv in $HVHosts)
{
$outprint = $Srv.SamAccountName
Write-Host "$outprint was found in $HVHostPath" -ForegroundColor Green
}
Write-Host " " -ForegroundColor Green
#Create Hostgroup
New-ADGroup –name $HVGroupName –groupscope DomainLocal -GroupCategory Security –path $HVGroupPath
Write-Host "$HVGroupName was created in $HVGroupPath" -ForegroundColor Green
Write-Host " " -ForegroundColor Green
#Add Hosts to Groups
foreach ($Srv in $HVHosts)
{
Add-ADGroupMember -Identity $HVGroupName -Members $HVHosts.SamAccountName
$outprint = $Srv.SamAccountName
Write-Host "$outprint was add to $HVGroupName" -ForegroundColor Green
}