使用 Desired State Configuration 与 Azure Scale Sets 进行驱动器映射
Drive Mapping with Azure Scale Sets using Desired State Configuration
我运行正在研究一个有趣的问题。也许你们这些好人可以帮助我了解这里发生的事情。如果有更好的方法,我洗耳恭听
我正在 运行在 Azure 上配置 DSC,并想映射一个驱动器。我读过这确实不是 DSC 的用途,但我不知道在使用 Azure Scalesets 的 DSC 之外有任何其他方法可以做到这一点。这是我 运行 遇到问题的脚本部分:
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:
}
}
我也在 SetScript 部分尝试过这段代码:
(New-Object -ComObject WScript.Network).MapNetworkDrive('W:','\azurestorage.file.core.windows.net\storage',$true,'username','passwordhere')
我还尝试了一个简单的 net use 命令来映射驱动器,而不是花哨的 New-Object 或 New-PSDrive cmdlet。相同的行为。
如果我运行这些命令(New-Object/NetUse/New-PSDrive)手动,如果我运行,机器将映射驱动器] 它带有一个单独的驱动器号。不知何故,驱动器正在尝试映射但未映射。
我已完成的故障排除:
- 我的环境中没有域。我只是尝试创建一个规模集和 运行 DSC 以使用在创建存储帐户时授予的存储帐户凭据配置计算机。
- 我正在使用存储帐户用户 ID 和访问密钥(随机生成的密钥,通常使用存储帐户名称作为用户)提供给我的用户名和密码。
- Azure 在 运行 启用 DSC 模块时没有抛出任何错误(事件日志中没有错误,仅供参考 - 资源执行序列正确地列出了我在 DSC 文件中的所有序列。)
- 当我登录到机器并检查驱动器是否被映射时,我 运行 进入了我想要的驱动器盘符 (W:) 上断开的网络驱动器。
- 如果我打开 Powershell,我收到一个错误:"Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed."
- 如果我运行 "Get-PSDrive" W:驱动器没有出现。
- 如果我 运行 在 Powershell 控制台中手动设置 SetScript 代码,映射的驱动器在不同的驱动器号下工作正常。
- 如果我尝试断开 W: 驱动器,我会收到 "This network connection does not exist."
- 我想也许 DSC 在映射之前需要一些时间并添加了一个睡眠定时器,但这没有用。相同的行为。
我想象的是这里发生的事情:
DSC 在 NT AUTHORITY\SYSTEM
帐户下运行,除非已设置凭据属性,否则在从网络共享中提取文件时会使用 Computer account
。但是看看 Azure 文件的操作方式,权限应该不是问题,但是 运行 NT AUTHORITY\SYSTEM
下的整个过程可能。我建议您尝试 run DSC as a user 您的虚拟机,看看是否可行。
ps。您还可以尝试对具有网络共享的 VM 执行相同的操作,您确信 share\ntfs 权限是正确的。您可能需要启用 anonymous user to access your share 才能正常工作。
我之前遇到过类似的问题,虽然它不涉及 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 来测试它,但这在 Server 2016 hyper-v vm
上运行良好
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
}
在我的简短测试中,驱动器只会在服务器重新启动后出现。
我运行正在研究一个有趣的问题。也许你们这些好人可以帮助我了解这里发生的事情。如果有更好的方法,我洗耳恭听
我正在 运行在 Azure 上配置 DSC,并想映射一个驱动器。我读过这确实不是 DSC 的用途,但我不知道在使用 Azure Scalesets 的 DSC 之外有任何其他方法可以做到这一点。这是我 运行 遇到问题的脚本部分:
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:
}
}
我也在 SetScript 部分尝试过这段代码:
(New-Object -ComObject WScript.Network).MapNetworkDrive('W:','\azurestorage.file.core.windows.net\storage',$true,'username','passwordhere')
我还尝试了一个简单的 net use 命令来映射驱动器,而不是花哨的 New-Object 或 New-PSDrive cmdlet。相同的行为。
如果我运行这些命令(New-Object/NetUse/New-PSDrive)手动,如果我运行,机器将映射驱动器] 它带有一个单独的驱动器号。不知何故,驱动器正在尝试映射但未映射。
我已完成的故障排除:
- 我的环境中没有域。我只是尝试创建一个规模集和 运行 DSC 以使用在创建存储帐户时授予的存储帐户凭据配置计算机。
- 我正在使用存储帐户用户 ID 和访问密钥(随机生成的密钥,通常使用存储帐户名称作为用户)提供给我的用户名和密码。
- Azure 在 运行 启用 DSC 模块时没有抛出任何错误(事件日志中没有错误,仅供参考 - 资源执行序列正确地列出了我在 DSC 文件中的所有序列。)
- 当我登录到机器并检查驱动器是否被映射时,我 运行 进入了我想要的驱动器盘符 (W:) 上断开的网络驱动器。
- 如果我打开 Powershell,我收到一个错误:"Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed."
- 如果我运行 "Get-PSDrive" W:驱动器没有出现。
- 如果我 运行 在 Powershell 控制台中手动设置 SetScript 代码,映射的驱动器在不同的驱动器号下工作正常。
- 如果我尝试断开 W: 驱动器,我会收到 "This network connection does not exist."
- 我想也许 DSC 在映射之前需要一些时间并添加了一个睡眠定时器,但这没有用。相同的行为。
我想象的是这里发生的事情:
DSC 在 NT AUTHORITY\SYSTEM
帐户下运行,除非已设置凭据属性,否则在从网络共享中提取文件时会使用 Computer account
。但是看看 Azure 文件的操作方式,权限应该不是问题,但是 运行 NT AUTHORITY\SYSTEM
下的整个过程可能。我建议您尝试 run DSC as a user 您的虚拟机,看看是否可行。
ps。您还可以尝试对具有网络共享的 VM 执行相同的操作,您确信 share\ntfs 权限是正确的。您可能需要启用 anonymous user to access your share 才能正常工作。
我之前遇到过类似的问题,虽然它不涉及 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 来测试它,但这在 Server 2016 hyper-v vm
上运行良好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
}
在我的简短测试中,驱动器只会在服务器重新启动后出现。