2016-12-08 46 views
1

我遇到了一個有趣的問題。也許你們好人可以幫助我理解這裏發生的事情。如果有更好的方法,我全都是耳朵。使用期望的狀態配置使用Azure Scale集進行驅動器映射

我正在Azure上運行DSC配置,並希望映射驅動器。我已經讀過這真的不是什麼DSC,但我不知道有任何其他方式使用Azure Scalesets在DSC之外進行此操作。下面是我遇到的問題腳本的一部分:我也試圖將此代碼SetScript部分

Script MappedDrive 
    { 
     SetScript = 
     { 
     $pass = "passwordhere" | ConvertTo-SecureString -AsPlainText -force 
     $user = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "username",$pass 
     New-PSDrive -Name W -PSProvider FileSystem -root \\azurestorage.file.core.windows.net\storage -Credential $user -Persist 
     } 
     TestScript = 
     { 
      Test-Path -path "W:" 
     } 
     GetScript = 
     { 
     $hashresults = @{} 
     $hashresults['Exists'] = test-path W: 
     } 
    } 

(New-Object -ComObject WScript.Network).MapNetworkDrive('W:','\\azurestorage.file.core.windows.net\storage',$true,'username','passwordhere') 

我也試過一個簡單的net use命令映射驅動器而不是花哨的,New-Object或New-PSDrive cmdlet。同樣的行爲。

如果我運行這些命令(新對象/ Net Use/New-PSDrive)手動,如果我使用單獨的驅動器號運行驅動器,機器將映射驅動器。不知何故,驅動器試圖被映射,但沒有映射。

疑難解答我做:

  • 沒有在我的環境沒有域。我只是試圖創建一個比例集並運行DSC來使用創建存儲帳戶時授予的存儲帳戶憑據來配置機器。
  • 我正在使用由存儲帳戶用戶ID和訪問密鑰(隨機生成的密鑰,通常以存儲帳戶的名稱作爲用戶)給予我的用戶名和密碼。
  • Azure在運行DSC模塊時沒有錯誤(事件日誌中沒有錯誤,僅供參考 - 資源執行序列正確列出了DSC文件中的所有序列。)
  • 當我登錄到機器並檢查如果驅動器被映射,我會遇到我想要的驅動器號(W :)上斷開的網絡驅動器。
  • 如果我打開Powershell,我收到一個錯誤:「嘗試對'FileSystem'提供程序執行InitializeDefaultDrives操作失敗。」
  • 如果我運行「Get-PSDrive」,W:驅動器不會出現。
  • 如果我在Powershell控制檯內手動運行SetScript代碼,映射的驅動器在另一個驅動器號下工作正常。
  • 如果我嘗試斷開W:驅動器,我收到「此網絡連接不存在」。
  • 我想也許DSC需要一些時間在映射之前添加一個睡眠定時器,但這並不奏效。同樣的行爲。

回答

0

我想像這是怎麼發生的事情:
DSC的NT AUTHORITY\SYSTEM帳戶下運行,並且,除非Credential屬性已經設置,從網絡共享拉文件時Computer account使用。但是看看Azure文件如何操作,權限不應該成爲問題,但在NT AUTHORITY\SYSTEM下運行整個過程可能會發生。我建議你嘗試使用虛擬機的run DSC as a user並查看是否有效。

ps。您也可以嘗試對具有網絡共享的虛擬機執行相同的操作,您可以確信共享\ ntfs權限是正確的。您可能需要啓用anonymous user to access your share才能正常工作。

+0

請記住,這不是一個標準份額。這個共享是使用Azure共享文件存儲構建的。共享是使用Azure門戶和/或使用Powershell cmdlet創建的。您不會像Windows服務器上的普通共享那樣修改用戶權限。 – 227Passive

+0

我從來沒有在我的答案的任何地方說過修改任何權限,請在評論之前閱讀。 @ 227Passive – 4c74356b41

2

我以前有類似的問題,雖然它沒有涉及DSC,但在服務器重新啓動之前安裝Azure文件共享應該沒問題,那麼它將顯示爲斷開連接的驅動器。如果我使用New-Object/Net Use/New-PSDrive和persist選項,會發生這種情況。

這個問題的答案的問題,我在updated docs

Persist your storage account credentials for the virtual machine

Before mounting to the file share, first persist your storage account credentials on the virtual machine. This step allows Windows to automatically reconnect to the file share when the virtual machine reboots. To persist your account credentials, run the cmdkey command from the PowerShell window on the virtual machine. Replace with the name of your storage account, and with your storage account key.

發現

cmdkey /add:<storage-account-name>.file.core.windows.net /user:<storage-account-name> /pass:<storage-account-key>

Windows will now reconnect to your file share when the virtual machine reboots. You can verify that the share has been reconnected by running the net use command from a PowerShell window.

Note that credentials are persisted only in the context in which cmdkey runs. If you are developing an application that runs as a service, you will need to persist your credentials in that context as well.

Mount the file share using the persisted credentials

Once you have a remote connection to the virtual machine, you can run the net use command to mount the file share, using the following syntax. Replace with the name of your storage account, and with the name of your File storage share.

net use <drive-letter>: \\<storage-account-name>.file.core.windows.net\<share-name> example : net use z: \\samples.file.core.windows.net\logs

Since you persisted your storage account credentials in the previous step, you do not need to provide them with the net use command. If you have not already persisted your credentials, then include them as a parameter passed to the net use command, as shown in the following example.

編輯: 我沒有一個Azure的VM免費測試它,但這工作正常n a Server 2016 hyper-v虛擬機

Script MapAzureShare 
    { 
     GetScript = 
     { 

     } 
     TestScript = 
     { 
      Test-Path W: 
     } 
     SetScript = 
     { 
      Invoke-Expression -Command "cmdkey /add:somestorage.file.core.windows.net /user:somestorage /pass:somekey" 
      Invoke-Expression -Command "net use W: \\somestorage.file.core.windows.net\someshare" 
     } 
     PsDscRunAsCredential = $credential 
    } 

在我簡短的測試中,驅動器只會在服務器重新啓動後出現。

+0

這實際上最終會爲我拋出一個錯誤。 [[Script] MappedDrive]對 目標「使用用戶提供的憑證執行SetScript」執行「Set-TargetResource」操作。 VERBOSE:[2016-12-09T14:51:06] [錯誤]系統錯誤1312發生。 VERBOSE:[2016-12-09T14:51:06] [錯誤] 指定的登錄會話不存在。它可能已經被終止。 – 227Passive

+0

你使用了'PsDscRunAsCredential'嗎?沒有它,'cmdkey'將不起作用。 – TravisEz13

+0

'PsDscRunAsCredential'無效。如果我使用Credtial,而cmdkey永遠不會獲得用戶,我會傳遞結果。恐怕這是不可能的,因爲在https://powershell.org/forums/topic/remotely-setting-network-credentials-using-cmdkey/中 – guillem