使用PowerShell 自动创建DFS命名空间服务器
运行环境:Windows Server 2012 R2
Writing PowerShell DFS Scripts: Managing DFS Links——包含了常用dfs操作语句
需要注意的是DFS依赖域,若此服务器未存在于域控上,或未存在域内,则此脚本会报错
创建DFS命名空间服务器,DFS命名空间会创建在当前服务器上
在脚本中自动获取本机服务器名称
$server = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object Name).Name
脚本如下
# 创建DFS命名空间服务器,DFS命名空间会创建在当前服务器上
# author:lttr <www.cnblogs.com/GoCircle>
# date:2019-08-09 # eg. # C:\dfsn.ps1 -Domain 'test.to' -ShareFolderPhysicalPath 'C:\dfsn' -ShareFolderNetworkPath 'DFSN01' [CmdletBinding()] param ( # 域名 [Parameter(Mandatory=$true,Position=0)][string]$Domain, # 共享文件夹物理路径 [Parameter(Mandatory=$true,Position=1)][string]$ShareFolderPhysicalPath, # 共享文件夹网络路径 [Parameter(Mandatory=$true,Position=2)][string]$ShareFolderNetworkPath ) # 设置共享文件夹 function SET-Share($ShareFolderPhysicalPath,$ShareFolderNetworkPath){ #文件夹不存在就创建 if(!(Test-Path $ShareFolderPhysicalPath)){ $null = New-Item -Path $ShareFolderPhysicalPath -type directory } #调用WMI对象 WIN32_Share类 $ShareHandle=[WMIClass]"WIN32_Share" #添加为共享 $null = $ShareHandle.Create($ShareFolderPhysicalPath,$ShareFolderNetworkPath,0) } SET-Share $ShareFolderPhysicalPath $ShareFolderNetworkPath pause try { #检测命名空间是否存在 if((Get-DfsnRoot -Path "\\$Domain\$ShareFolderNetworkPath" -ErrorAction SilentlyContinue).State -eq 'Online') { Write-Host "DFS命名空间[\\$Domain\$ShareFolderNetworkPath]已存在!" -ForegroundColor Red }else{ $DFSServerName = (Get-WmiObject -Class Win32_ComputerSystem | Select-Object Name).Name $null = New-DfsnRoot -Path "\\$Domain\$ShareFolderNetworkPath" -TargetPath "\\$DFSServerName\$ShareFolderNetworkPath" -Type DomainV2 if((Get-DfsnRoot -Path "\\$Domain\$ShareFolderNetworkPath" -ErrorAction SilentlyContinue).State -eq 'Online') { Write-Host "创建DFS命名空间[\\$Domain\$ShareFolderNetworkPath]成功!" -ForegroundColor Green } else { Write-Host "创建DFS命名空间[\\$Domain\$ShareFolderNetworkPath]失败!" -ForegroundColor Red } } } catch { Write-Host "DFS命名空间[\\$Domain\$ShareFolderNetworkPath]失败" -ForegroundColor Red }
有错误的请多多指教,共同进步(๑•ᴗ•๑)
By听雨的人
By听雨的人