使用PowerShell 自动创建DFS命名空间服务器

DFS命名空间概述

DFS命名空间 PowerShell脚本命令

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

# 创建DFS命名空间服务器,创建的共享文件夹目录必须存在于dfsserver上
# eg.
# C:\dfsn.ps1 -Domain 'test.to' -ShareFolderPhysicalPath 'C:\dfsn' -ShareFolderNetworkPath 'DFSN01' -DFSServerName 'WIN01'

[CmdletBinding()]
param
(
    # 域名
    [Parameter(Mandatory=$true,Position=0)][string]$Domain,
    # 共享文件夹物理路径
    [Parameter(Mandatory=$true,Position=1)][string]$ShareFolderPhysicalPath,
    # 共享文件夹网络路径
    [Parameter(Mandatory=$true,Position=2)][string]$ShareFolderNetworkPath,
    # DFS命名空间服务器名称
    [Parameter(Mandatory=$true,Position=3)][string]$DFSServerName
)

# 设置共享文件夹
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{
        $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
}

扫描二维码关注公众号,回复: 6987992 查看本文章

猜你喜欢

转载自www.cnblogs.com/GoCircle/p/11250415.html