使用PowerShell 自动创建DFS复制组

 运行环境:Windows Server 2012 R2

 

DFS复制组 PowerShell脚本命令

 
需要注意的是DFS依赖域,若此服务器未存在于域控上,或未存在域内,则此脚本会报错

此脚本可在域内任意服务器执行

脚本创建的是交错型拓扑结构,使用的默认计划

检测是否存在复制组时,有一种情况会检查错误,就是查看复制组时报错误。

 

# 创建DFS复制
# author:lttr <www.cnblogs.com/GoCircle>
# date:2019-08-09
# eg. # C:\dfsr.ps1 -Domain "test.to" -DfsN "DFSN01" -GroupName "RG01" -FolderName "dfs" -ShareFolderPhysicalPath "C:\DFShare\dfs" -SourceComputerName "WIN01" -servers "WIN02","WIN03","WIN04" [CmdletBinding()] param ( # 域名 [Parameter(Mandatory=$true,Position=0)][string]$Domain, # DFS命名空间名称 [Parameter(Mandatory=$true,Position=1)][string]$DfsN, # 复制组名称 [Parameter(Mandatory=$true,Position=2)][string]$GroupName, # 复制组文件夹名称 [Parameter(Mandatory=$true,Position=3)][string]$FolderName, # 共享文件夹物理路径 [Parameter(Mandatory=$true,Position=4)][string]$ShareFolderPhysicalPath, # DFS成员列表 [Parameter(Mandatory=$true,Position=6)][String[]]$servers ) # 域内远程设置共享文件夹 function SET-Share($DriveLetter,$ShareFolderPhysicalPath,$ShareFolderNetworkPath,$Server){ if(!(Test-Path \\$Server\$DriveLetter$\$ShareFolderPhysicalPath)){ $null = New-Item -Path \\$Server\$DriveLetter$\$ShareFolderPhysicalPath -type directory -Force } $Shares = [WMICLASS]"\\$Server\root\cimv2:WIN32_Share" $null = $Shares.Create($DriveLetter+":\"+$ShareFolderPhysicalPath,$ShareFolderNetworkPath,0) } try { #检测是否发布到命名空间 if(Get-DfsnFolderTarget -Path "\\$Domain\$DfsN\$FolderName" -ErrorAction SilentlyContinue) { Write-Host "DFS复制组[$GroupName]已存在!" -ForegroundColor Red }else{ # 设置复制组 $null = New-DfsReplicationGroup -GroupName $GroupName | New-DfsReplicatedFolder -GroupName $GroupName -FolderName $FolderName -DfsnPath "\\$Domain\$DfsN\$FolderName" -WarningAction silentlyContinue | Add-DfsrMember -ComputerName $servers # 设置连接和成员身份 for($i=0;$i -le $servers.Length-1;$i++){ for($j=$i+1;$j -le $servers.Length-1;$j++){ $null = Add-DfsrConnection -GroupName $GroupName -SourceComputerName $servers[$i] -DestinationComputerName $servers[$j] } } foreach ($server in $servers) { # 为命名空间新建文件夹目标 $null = New-DfsnFolderTarget -Path "\\$Domain\$DfsN\$FolderName" -TargetPath "\\$server.$Domain\$FolderName" -State "Online" -ReferralPriorityClass "sitecostnormal" # 设置共享文件夹 SET-Share ($ShareFolderPhysicalPath -split ":")[0] ($ShareFolderPhysicalPath -split ":\\")[-1] $FolderName $server # 设置复制组成员的本地路径 $null = Set-DfsrMembership -GroupName $GroupName -FolderName $FolderName -ComputerName $server -PrimaryMember $True -ContentPath $ShareFolderPhysicalPath -Force } # 更新本地DFSR配置信息 Update-DfsrConfigurationFromAD -ComputerName $servers #检测是否发布到命名空间 if(Get-DfsnFolderTarget -Path "\\$Domain\$DfsN\$FolderName" -ErrorAction SilentlyContinue) { Write-Host "创建DFS复制组[$GroupName]成功!" -ForegroundColor Green }else{ Write-Host "创建DFS复制组[$GroupName]失败!" -ForegroundColor Red } } } catch { Write-Host "创建DFS复制组[$GroupName]失败!" -ForegroundColor Red }

 

posted @ 2019-08-13 23:22  听雨的人  阅读(631)  评论(0编辑  收藏  举报