Powershell 管理域之搜索OU并移动
2016-01-04 21:14
489 查看
在前段时间,由于项目的需要,需要在windows server域管理器中将所有Hyper—V VM搜索出来并将其移入统一的组下面。所使用的是windows server自带的powershell命令。下面是其具体的实现:
import-module ActiveDirectory Write-Host "This script is to avoid repeating configuration action.`n" Write-Host "Before you config the GPRegistryValue,do you want to backup?Please input y to backup or n do nothing." $backup = Read-Host #Test the path(it's a folder),and backup all the gpo. if($backup -eq "y"){ if(!(Test-Path C:\GpoBackups)){ New-Item -ItemType Directory -Force -Path C:\GpoBackups } Backup-GPO -All -Path C:\GpoBackups } #Check the OU(organization Unit),and create it if not exist. Write-Host "Please input a target OU name.eg:GPWRM.`n" $ldap = '(&(cn=Microsoft Hyper-V)(objectCategory=serviceConnectionPoint))' $ouName = Read-Host $targetOU = "OU=" + $ouName +",DC=hpv,DC=local" if(![adsi]::Exists("LDAP://$targetOU")){ New-ADOrganizationalUnit -Name $ouName -path "DC=hpv,DC=local" } #Find target and move to OU. $searcher = [adsisearcher]$ldap $searcher.FindAll()| ForEach-Object{ $obj = $_.GetDirectoryEntry() $path = $obj.distinguishedName.Value.Replace("CN=Microsoft Hyper-V,","") Move-ADObject -Identity "$path" -TargetPath "$targetOU" } Write-Host "Please input a new GPO name.Eg:Configure firewall rules for remote gpupdate12.`n" $gpoName = Read-Host Write-Host "Please input a starter GPO Name.eg:Group Policy Remote Update Firewall Ports.`n" $starterGpoName = Read-Host New-GPO –Name $gpoName –StarterGpoName $starterGpoName | New-GPLink –target $targetOU –LinkEnabled yes Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!AllowAutoConfig" -ValueName "(Default)" -Value "true" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!IPv4Filter" -ValueName "(Default)" -Value "*" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!IPv6Filter" -ValueName "(Default)" -Value "*" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!AllowBasic" -ValueName "(Default)" -Value "false" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!AllowUnencryptedTraffic" -ValueName "(Default)" -Value "true" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!AllowNegotiate" -ValueName "(Default)" -Value "false" -Type String Set-GPRegistryValue -Name $gpoName -Key "HKLM\Software\Policies\Microsoft\Windows\WinRM\Service!AllowKerberos" -ValueName "(Default)" -Value "false" -Type String Get-ADComputer -filter * -Searchbase $targetOU | foreach{ Invoke-GPUpdate -computer $_.name -RandomDelayInMinutes 0 -force}
相关文章推荐
- 【DevOps】为什么我们永远疲于奔命?
- 网络管理之IP地址篇
- 文件的读出 编辑 管理
- PowerShell打开或关闭光驱
- PowerShell移动目录中指定文件的方法(非全部文件)
- Powershell小技巧之创建短网址
- PowerShell实现查询打开某个文件的默认应用程序
- PowerShell设置文件只读、隐藏属性的方法
- Windows Powershell扩展类型系统
- PowerShell脚本源码输出到文件的最佳写法
- Windows Powershell 通过函数扩展别名
- PowerShell脚本实现创建桌面快捷方式的方法
- PowerShell重启服务命令Restart-Service详细介绍
- PowerShell中执行Javascript的方法示例
- 探索PowerShell(十三) WMI对象介绍
- SQL Server 2008 R2 应用及多服务器管理
- Powershell小技巧之使用Copy-Item添加程序到开机启动
- Windows Powershell创建对象
- Windows Powershell 访问数组
- Powershell小技巧之使用WS-Man来调用PowerShell命令