当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 编写 DFS PowerShell 脚本:管理 DFS 链接

作者:精品下载站 日期:2024-12-14 20:49:29 浏览:12 分类:玩电脑

编写 DFS PowerShell 脚本:管理 DFS 链接


准备好学习如何使用您在本教程中获得的知识编写更好的 DFS PowerShell 脚本。在这篇文章中,您将了解有关在 PowerShell 中管理 DFS 链接的所有信息!

这篇博文有一个由 TechSnips 贡献者 Scott Hurst 创建的配套视频。请随时佩戴手表,或者,如果您喜欢文字,请继续阅读!

分布式文件系统 (DFS) 链接降低了使用网络文件共享的复杂性。 DFS 链接允许用户和应用程序访问虚拟路径名以连接到共享文件夹。您可以使用 PowerShell 创建 DFS链接

无论您是管理 Active Directory 还是其他 DFS 产品,编写 DFS PowerShell 脚本都将帮助您解决遇到的任何问题。

这个虚拟命名空间使管理员能够显示位于不同服务器上的共享文件夹,甚至更改共享文件夹的位置,这对该文件夹的使用者完全透明。

当文件服务器发生变化时,用户不需要更新书签,并且应用程序也不需要使用新路径进行更新。

DFS 的好处

对于用户来说,对网络共享文件夹的访问被简化为 \FolderName 格式,从而降低了与存储在远程服务器上的文件夹相关的复杂性。

对于应用程序来说,网络上资源的硬编码路径不必由于网络路径的变化而改变。对 DFS 链接进行简单更新,应用程序将继续访问新位置的资源。

DFS 服务器角色的先决条件

  • 活动目录
  • Windows Server 上安装的文件和存储服务角色
  • Windows Server(半年频道)
  • Windows 服务器 2016
  • Windows Server 2012 R2
  • Windows 服务器 2012
  • Windows Server 2008 R2 数据中心/企业

先决条件

  • 具有适当权限的管理员帐户
  • 安装了“文件服务工具 - DFS 管理工具”的 RSAT 工具

设置 DSC PowerShell 脚本

首先,下载并安装 RSAT。接下来,您需要安装所有必需的 Windows 功能。这将安装DFS管理 GUI、Windows PowerShell的DFS命名空间模块(用于使用PowerShell创建DFS链接并管理它们)以及命令行工具,但它不会在服务器上安装任何DFS服务。

Install-WindowsFeature FS-DFS-Namespace, RSAT-DFS-Mgmt-Con

常用DFS命令

您可以使用 DFS 管理 GUI、PowerShell 中的 DFS 命名空间 (DFSN) cmdlet、DfsUtil 命令或调用 WMI 的脚本来管理命名空间。

一些常见的 PowerShell 命令是:

  • Get-DfsnRoot - 发现当前域中的所有 DFS 命名空间 - 通常用于检查当前域中的可用命名空间
  • New-DfsnFolder - 创建新的 DFS 文件夹名称
    - 通常用于在命名空间中创建新的 DFS 文件夹
  • New-DfsnFolderTarget - 将路径分配给 DFS 文件夹名称
    - 通常用于将一个或多个网络文件夹路径分配给 DFS 文件夹
  • Remove-DfsnFolderTarget - 从 DFS 文件夹中删除路径,但不删除 DFS 文件夹。
    - 通常用于从 DFS 文件夹中删除一个或多个网络文件夹路径
  • Remove-DfsnFolder - 删除文件夹及其所有路径
    - 通常用于从命名空间中删除 DFS 文件夹

查找 DFS 命名空间

我们首先使用 Get-DfsnRoot cmdlet 了解当前域中所有在线且可用的命名空间

$Domain = 'tech.io'
(Get-DfsnRoot -Domain $Domain).Where( {$_.State -eq 'Online'} ) | Select-Object -ExpandProperty Path

这表明该域中有两个在线命名空间

查找 DFS 文件夹

使用Get-DfsnFolder命令,我们可以列出每个命名空间中的DFS文件夹

$Domain = 'tech.io'
Get-DfsnFolder -Path "\$Domain\AppRoot\*" | Select-Object -ExpandProperty Path
Get-DfsnFolder -Path "\$Domain\DFSRoot\*" | Select-Object -ExpandProperty Path

从这个输出中,我们可以看到,在AppRoot命名空间中,这不是一个名为PowerShell的DFS文件夹

为 DSC PowerShell 脚本创建 DFS 链接文件夹

在此示例中,我们在三个文件服务器上有一个名为 PowerShell 的复制文件夹; FileServer01、FileServer02数据中心

目标是使用单个路径与我们的管理员共享此复制的文件夹。

为此,我们将使用 AppRoot 命名空间 中创建一个名为 PowerShell 的新 DFS link 文件夹>New-DfsnFolder 并将其指向数据中心服务器的文件共享。我们将 DFS 文件夹 状态 设置为 Online 并设置 TargetPath 状态在线

$Domain = 'tech.io'
try {
    Get-DfsnFolderTarget -Path "\$Domain\AppRoot\PowerShell" -ErrorAction Stop
} catch {
    Write-Host "Path not found. Clear to proceed" -ForegroundColor Green
}

$NewDFSFolder = @{
    Path = "\$Domain\AppRoot\PowerShell"
    State = 'Online'
    TargetPath = '\datacenter\FileShare\PowerShell'
    TargetState = 'Online'
    ReferralPriorityClass = 'globalhigh'
}

New-DfsnFolder @NewDFSFolder

# Check that folder now exists:
Get-DfsnFolderTarget -Path "\$Domain\AppRoot\PowerShell"

# Check that the new DFS Link works using Windows Explorer
Invoke-Expression "explorer '\$Domain\AppRoot\PowerShell\'"

在 DFS 管理 GUI 中,我们可以看到 PowerShell DFS 文件夹不存在。

现在让我们从 PowerShell 控制台中执行 Windows 资源管理器并确认它不存在。

Invoke-Expression "explorer "\$Domain\AppRoot\PowerShell"

如果该文件夹不存在,它将写入输出找不到路径。在终端窗口中以绿色文本清除以继续,如下所示。

从输出中我们看到该文件夹已创建,Referral Priority Class 设置为 Global-HighState在线

GUI 还证实了 PowerShell 告诉我们的内容。

我们的路径现在可以在 Windows 资源管理器中使用。

创建 DFS 文件夹目标

现在我们已在命名空间中成功创建了Powershell DFS 文件夹,向其中添加一个额外的文件夹目标路径并使用 New-DfsnFolderTarget 将该路径设置为“Online”。 DFS PowerShell 脚本允许您添加任意数量的组件。

$Domain = 'tech.io'

## Splat the settings for easy readibility
$NewTPS = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\FileServer01\FileShare\PowerShell'
    State = 'Online'
}

## Add new folder located on the 'FileServer01' server & set Online
New-DfsnFolderTarget @NewTPS

到目前为止,我们已经添加了三个服务器路径中的两个,并且处于在线状态。对于最后一个文件夹路径,我们希望添加路径,但不将其提供给用户。因此,让我们将文件夹目标路径添加到我们的PowerShell DFS 文件夹,这次将DFS旧路径状态设置为脱机,我们将再次使用新建-DfsnFolderTarget

$Domain = 'tech.io'
## Splat the settings for easy readibility
$NewTPS = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\FileServer02\FileShare\PowerShell'
    State = 'Offline'
}

## Add new folder located on the 'FileServer02' server & set to Offline
New-DfsnFolderTarget @NewTPS
## Check that folder now exists:

您可以看到FileServer01Datacenter的路径当前为Online并且FileServer02的状态已已设置为离线

将 DFS 文件夹目标设置为脱机或联机

我们还可以使用 Set-DfsnFolderTarget 更改哪些服务器是在线主机和离线主机,甚至哪些服务器将成为文件路径的主要主机

$Domain = 'tech.io'

## Splatting the settings where the path pointed at the server named FileServer01
$ChangeTPsFS1 = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\FileServer01\FileShare\PowerShell'
    State = 'Offline'
}

## Set folder located on the server path 'FileServer01' to Offline
Set-DfsnFolderTarget @ChangeTPsFS1

## Splatting the settings where the path pointed at the server named FileServer02
$ChangeTPsFS2 = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\FileServer02\FileShare\PowerShell'
    State = 'Online'
    ReferralPriorityClass = 'globalhigh'
}

## Set folder located on the 'FileServer02' server to Online
Set-DfsnFolderTarget @ChangeTPsFS2

## Splatting the settings where the path pointed at the server named Datacenter
$ChangeTPsFS3 = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\datacenter\FileShare\PowerShell'
    ReferralPriorityClass = 'sitecostnormal'
}

## Change Priority of 'Datacenter' server folder path to 'Normal'
Set-DfsnFolderTarget @ChangeTPsFS3

如下所示:

  • FileServer01的路径已更改为Offline
  • 数据中心服务器的ReferralPriorityClass已从global-high切换为sitecost-normal
  • FileServer02的路径已将其状态更改为在线
  • FileServer02 的 ReferralPriorityClass 已切换为 global-high

使用 PowerShell 删除 DFS 文件夹目标路径

我尝试尽可能地为我的代码接种疫苗以预防“胖手指流感”。在这里,我们将尝试在删除其中一个文件夹之前安装一个安全网,方法是在删除文件夹之前确保其处于脱机状态。

# Check Target Path to 'FileServer01' server to Offline & Remove the Folder Target Path
if ((Get-DfsnFolderTarget -Path "\$Domain\AppRoot\PowerShell" -TargetPath '\FileServer01\FileShare\PowerShell').State -eq "Offline") {
    Remove-DfsnFolderTarget -Path "\$Domain\AppRoot\PowerShell" -TargetPath '\FileServer01\FileShare\PowerShell' -Force:$true
}

FileServer01 文件夹太长了!

对于那些愿意放弃安全网选项的人,我们也可以满足您,勇敢的灵魂。

$Domain = 'tech.io'

## Splatting the settings where the path pointed at the server named 'FileServer02'
$DelFTS = @{
    Path = "$Domain\AppRoot\PowerShell"
    TargetPath = '\FileServer02\FileShare\PowerShell'
}

##Delete the DFS FolderTarget
Remove-DfsnFolderTarget @DelFTS -Force:$true

我们已经告别了 FileServer02 文件夹。

删除 DFS 文件夹

这是一条漫长而曲折的道路,但我们的 DFS 链接 的时代已经结束。我们可以使用 Remove-DfsnFolder cmdlet 删除 PowerShell 文件夹和 DFS 链接

$Domain = 'tech.io'

## Delete the DFS Folder
Remove-DfsnFolder -Path "$Domain\AppRoot\PowerShell" -Force:$true

快速仔细检查 DFS 管理 GUI 显示我们的 DFS 链接 已不复存在。

概括

就是这样!您经历了 DFS 和 PowerShell 功夫的曲折之路。到目前为止,我希望您已经获得了一些代码片段来帮助您构建更好的 PowerShell DFS 脚本!

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯