

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Map Server 訊息區塊 (SMB) 網路磁碟機
<a name="map-smb-network-drives"></a>

您可以使用 SMBs 目標網路下的任何機器。如果您偏好透過工作階段指令碼設定設定，則必須先建立指令碼，該指令碼會在使用者登入時叫用，因為工作階段指令碼是依映像設定。

若要映射伺服器訊息區塊 (SMB) 網路磁碟機，請執行下列步驟。

## 步驟 1：確保服務正在執行
<a name="smb-start"></a>

從開始功能表開啟 **services.msc**，並確保下列服務都執行中：
+ DNS 用戶端
+ 函數探索資源發佈
+ SSDP 探索
+ UPnP 裝置主機

## 步驟 2：建立 SMB 資料夾
<a name="create-smb-server-manager"></a>

您可以使用 File Explorer 建立 SMB。

**使用 File Explorer 設定 SMB 共用資料夾**

1. 在 SMB 資料夾上按一下滑鼠右鍵，然後選擇**屬性**、**共用**。

1. 選擇**進階共用**。

1. 針對**進階共用**，勾選**共用此資料夾**，然後選擇**許可**。

1. 如果您想要為所有使用者提供許可，請將其保留為預設設定。

   如果您想要新增特定使用者，請在**共用許可**下，選擇**每個人**，**移除**。然後選擇**新增**，然後輸入您要存取檔案共享的使用者或群組。

   針對您新增的每個使用者或群組，選擇**允許** 來指派**完整控制**、**變更**或**讀取許可**。

1. 選擇**套用**、**確定**、**確定**、**關閉**。

## 步驟 3：確認 SMB 可在網域中存取
<a name="verify-smb"></a>

從使用相同安全群組並加入相同網域的另一個伺服器開啟檔案總管。導覽至網路路徑資料夾，透過提供的網路路徑存取網路共用。選擇**屬性**、**共用**、**網路路徑**。

## 步驟 4：讓使用者能夠從本機/網域群組政策建立符號連結
<a name="enable-smlink-smb"></a>

為您的使用者啟用從本機/網域群組政策建立符號連結，以確保群組政策中定義的工作階段指令碼或登入指令碼。這可讓您在步驟 5 中建立具有使用者許可的指令碼。

**讓使用者能夠從本機/網域群組政策建立符號連結**

1. 在將用於定義此政策的 GPO 中，選擇**電腦組態**、**Windows 設定**、**安全設定**、**使用者權利指派**、**政策**、**建立符號連結**。然後，更新要包含的使用者許可。如需建立符號連結的詳細資訊，請參閱[建立符號連結](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-10/security/threat-protection/security-policy-settings/create-symbolic-links)。

1. 根據預設，remote-to-remote （例如，對稱連結映射到另一個類似的對稱連結內的網路共享） 和remote-to-local（例如，對稱連結映射到對稱連結映射到網路共享內的本機共享） 存取會停用。如果需要符號連結映射，請執行下列命令：
   + 啟用remote-to-remote存取 - `fsutil behavior set SymlinkEvaluation R2R:1`
   + 啟用remote-to-local存取 - `fsutil behavior set SymlinkEvaluation R2L:1 `

## 步驟 5：建立在使用者登入時調用的指令碼
<a name="create-script-smb"></a>

使用 WorkSpaces 應用程式工作階段指令碼或 GPO 登入指令碼，建立在使用者登入時調用的指令碼。如果您選擇使用 WorkSpaces 應用程式工作階段指令碼，工作階段指令碼只會套用至該特定的 WorkSpaces 應用程式映像。如果您使用 GPO 登入指令碼，則 GPOs會套用至網域/OU，您可以將其設定為您的機群。如此一來，您就不需要為您擁有的每個映像設定指令碼。

### 選項 1：使用工作階段指令碼在我的檔案下掛載 SMB 共用資料夾 （使用 Powershell)
<a name="powershell-smb"></a>

**使用工作階段指令碼在我的檔案下掛載 SMB 共用資料夾 （使用 Powershell)**

1. 成功定義使用者許可後，請使用使用者內容或系統內容設定下列範例指令碼。

   以下是使用使用者內容的 config.json 指令碼範例。

   ```
   "SessionStart": {
       "executables": [
       {
           "context": "system",
           "filename": "",
           "arguments": "",
           "s3LogEnabled": true
       },
       {
           "context": "user",
           "filename": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
           "arguments": "-File \"C:\\AppStream\\SessionScripts\\userStart.ps1\"",
           "s3LogEnabled": true
       }
    ],
   "waitingTime": 30
   ```

   以下是使用系統內容的範例指令碼。

   ```
   "SessionStart": {
       "executables": [
       {
           "context": "system",
           "filename": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
           "arguments": "-File \"C:\\AppStream\\SessionScripts\\systemStart.ps1\"",
           "s3LogEnabled": true
       },
       {
           "context": "user",
           "filename": "",
           "arguments": "",
           "s3LogEnabled": true
       }
    ],
   "waitingTime": 30
   ```

1. 如果您使用的是多工作階段機群，您可以使用系統環境變數`$env:AppStream_Session_UserName`來導覽至使用者的 My Files 資料夾。這允許在使用系統內容 時映射到 `Admin`而非使用者名稱`$env:USERNAME`。

   ```
   # Define the target application path
   $targetPathes = "<SMB-PATH>"
   
   # Define the shortcut location
   $symlinkLocation = "C:\Users\$Env:AppStream_Session_UserName\My Files\Custom Folder"
   
   # Create the junction for Custom Home Folder under MyFiles
   New-Item -ItemType SymbolicLink -Path $symlinkLocation -Target $targetPaths
   ```

### 選項 2：使用 GPO Logon 指令碼將 SMB 共用資料夾掛載到我的檔案下
<a name="powershell-gpo-logon"></a>

1. 透過建立符號 fink 到檔案或資料夾來掛載 SMB 共用資料夾。如需詳細資訊，請參閱[範例 7：建立檔案或資料夾的符號連結](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.                                         management/new-item?view=powershell-7.4#example-7-create-a-symbolic-link-to-a-file-or-folder)

1. [指派使用者登入指令碼。](https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-                                 2012-r2-and-2012/dn789196(v=ws.11)#how-to-assign-user-logon-scripts)

1. 新增下列指令碼，在我的檔案下建立自訂主資料夾的連接。

   ```
   # Define the target application path
   $targetPathes = "<SMB-PATH>"
   
   # Define the shortcut location
   $symlinkLocation = "C:\Users\$env:Username\My Files\Custom Folder"
   
   # Create the junction for Custom Home Folder under MyFiles
   New-Item -ItemType SymbolicLink -Path $symlinkLocation -Target $targetPaths
   ```

   如果您使用的是 Windows Server 2022 映像，在成功完成登入指令碼之前，可能會發生無法建立 My Files 資料夾的問題。當您的 SMB 掛載操作透過 Logon Script 完成時，這可能會導致逾時。若要解決此問題，同時掛載 SMB，請執行下列動作，使用 Logon 指令碼觸發獨立程序 (`Start-Process`)：

   1. 建立登入指令碼。

      ```
      # Define the log file path
      $logFilePath = "<This-is-where-your-log-files-are-saved>"
      
      # Function to write log messages
      function Write-Log {
          param (
              [string]$message
          )
          $timestamp = get-date -format "yyyy-MM-dd HH:mm:ss"
          $logMessage = "$timestamp - $message"
          $logMessage | Out-File -FilePath $logFilePath -Append -Encoding UTF8
      }
      
      try {
          Write-Log "Setting execution policy..."
          Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
          Write-Log "Unblocking logon script file..."
          $filePath = "<This-is-where-your-actual-logon-script-is-linked>"
          Unblock-File -Path $filePath
          Write-Log "Running actual logon script..."
          Start-Process -FilePath 'Powershell.exe' -ArgumentList "-File `"$filePath`""
      } catch {
          Write-Log "An error occurred: $_" "ERROR"
      }
      ```

   1. 如有需要，請使用群組政策更新此登入指令碼延遲組態。如需詳細資訊，請參閱[設定登入指令碼延遲](https://admx.help/?Category=Windows_8.1_2012R2&Policy=Microsoft.Policies.GroupPolicy::LogonScriptDelay)。登入指令碼延遲將是觸發非同步登入指令碼之前將延遲的時間量。預設延遲為 5 分鐘。

   1. 重新啟動機群以套用登入指令碼延遲。