在 PowerShell Runbook 中使用 Azure 文件共享
Use Azure file share in PowerShell Runbook
我创建了 az Azure 文件共享,我想在其中存储一些 SharePoint 配置文件、模板和 xml。文件已复制到共享并可供使用。
我知道可用于检索单个文件的 Get-AzureStorageFileContent 命令,但我需要将此共享视为共享,因为这些文件是从 PnP 配置模板中引用的,例如:
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="webparts\head.xml"/>
当我 运行 脚本本地或网络共享时,这工作正常。所以,我的问题是,如何使用 UNC 路径或将其映射为网络驱动器?
根据文档,我应该可以将其用作 smb 共享,但是当我尝试连接时,出现此错误:
New-SmbMapping
: 无法连接到 CIM 服务器。指定的服务不作为已安装的服务存在。
New-PSDrive
在 Azure Powershell 中不存在。我发现这是 Azure 中的一个模块,但是当其中一个需要在 Azure 中更新 .Net 时,我停止安装依赖项。
谁能给我一个端到端的指南,我怎样才能使用 Azure 文件共享实现真正的文件共享功能?
编辑:可接受的解决方法是,如果我可以将所有文件从共享复制到当前临时文件夹,因为从那里我可以使用 $env:TEMP
访问它们。
您收到的错误似乎与 DNS 服务器的权限有关,此 Thread 提供以下解决方案:
在 DNS Server/DC 上添加这两个角色:DnsAdmins 和 WinRMRemoteWMIUsers__。然后 DNS 服务器上的以下内容:
*1。打开计算机管理控制台。右键单击 WMI 控件(在服务和应用程序下)并单击 属性。
2. 在新打开的 Window 中,单击“安全”选项卡。
- 展开根树,然后单击节点 CIMV2,然后单击按钮安全
- 在新打开的Window中,点击高级按钮。
- 在新打开的Window中,点击权限选项卡下的添加按钮。
- 在新打开的Window中,点击“select一个校长”,然后搜索并添加组WinRMRemoteWMIUsers__作为校长,然后点击确定。
- 在应用于中,选择“此命名空间和子命名空间”。
- 对于权限,检查“执行方法”、“启用帐户”和“远程启用”
- 点击确定三下。
- 然后导航到节点 Root – Microsoft – Windows – DNS。做同样的事情,为 WinRMRemoteWMIUsers 添加权限。
- 重新启动服务“Windows Management Instrumentation。
- 检查问题是否已解决。*
这是完整的 MS official documentation,其中还提供了在本地连接驱动器的步骤
更新:递归复制所有文件和文件夹到$env:temp
powershell runbook 中的代码:
function Get-AzureFiles
{
param([string]$filesharename = 'testfolder', #replace with your own fileshare name
[string]$username = 'your account name',
[string]$password = 'your account key',
[string]$destination=$env:TEMP+"\", #note there is a slash at the end
[string]$path="")
$temp = $env:TEMP
# get the context
$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
# get all files and directories
if($path -eq "")
{
$content = Get-AzureStorageFile -ShareName $filesharename -Context $context
}
else
{
$content = Get-AzureStorageFile -ShareName $filesharename -Context $context -Path $path | Get-AzureStorageFile
}
if(!(test-path $destination))
{
mkdir $destination
}
foreach($c in $content)
{
$p = $c.uri.LocalPath -replace "$($c.share.name)/" ,''
#write-host "the value p is: $p"
#if it's a directory in fileshare
if($c.gettype().name -eq "CloudFileDirectory")
{
# Write-Host "$($c.share.name) is a directory"
$destination =$temp + $c.uri.PathAndQuery -replace "/","\"
#create the folder locally
if(!(test-path $destination))
{
mkdir $destination
#write-host "the new directory $destination is created locally."
}
#define the folder path in fileshare
$path = ($c.uri.localpath -replace "/$filesharename/" , "") -replace "/","\"
Get-AzureFiles -destination $destination -path $path
}
#if it's a file
elseif($c.gettype().name -eq "CloudFile")
{
$s = $temp + $c.uri.PathAndQuery -replace "/","\"
#Write-Output "downloading --- $s"
$d1 = $c.uri.PathAndQuery -replace "/","\"
$d1 = $d1.remove($d1.LastIndexOf("\")+1)
$destination =$temp + $d1
#create the folder locally
if(!(test-path $destination))
{
mkdir $destination
#write-host "the new directory $destination is created locally."
}
$path_temp = $c.uri.PathAndQuery -replace "/$filesharename/",""
Get-AzureStorageFileContent -ShareName $filesharename -Path $path_temp -Destination $destination -Context $context
}
}
}
function do-test
{
get-AzureFiles
# you can operate the files copied to $env:temp
dir $env:TEMP\testfolder
dir $env:TEMP\testfolder\t1
dir $env:TEMP\testfolder\t1\t1sub
}
do-test
测试结果:
我在 Azure 门户中的文件共享结构:
我创建了 az Azure 文件共享,我想在其中存储一些 SharePoint 配置文件、模板和 xml。文件已复制到共享并可供使用。
我知道可用于检索单个文件的 Get-AzureStorageFileContent 命令,但我需要将此共享视为共享,因为这些文件是从 PnP 配置模板中引用的,例如:
<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="webparts\head.xml"/>
当我 运行 脚本本地或网络共享时,这工作正常。所以,我的问题是,如何使用 UNC 路径或将其映射为网络驱动器?
根据文档,我应该可以将其用作 smb 共享,但是当我尝试连接时,出现此错误:
New-SmbMapping
: 无法连接到 CIM 服务器。指定的服务不作为已安装的服务存在。
New-PSDrive
在 Azure Powershell 中不存在。我发现这是 Azure 中的一个模块,但是当其中一个需要在 Azure 中更新 .Net 时,我停止安装依赖项。
谁能给我一个端到端的指南,我怎样才能使用 Azure 文件共享实现真正的文件共享功能?
编辑:可接受的解决方法是,如果我可以将所有文件从共享复制到当前临时文件夹,因为从那里我可以使用 $env:TEMP
访问它们。
您收到的错误似乎与 DNS 服务器的权限有关,此 Thread 提供以下解决方案:
在 DNS Server/DC 上添加这两个角色:DnsAdmins 和 WinRMRemoteWMIUsers__。然后 DNS 服务器上的以下内容:
*1。打开计算机管理控制台。右键单击 WMI 控件(在服务和应用程序下)并单击 属性。 2. 在新打开的 Window 中,单击“安全”选项卡。
- 展开根树,然后单击节点 CIMV2,然后单击按钮安全
- 在新打开的Window中,点击高级按钮。
- 在新打开的Window中,点击权限选项卡下的添加按钮。
- 在新打开的Window中,点击“select一个校长”,然后搜索并添加组WinRMRemoteWMIUsers__作为校长,然后点击确定。
- 在应用于中,选择“此命名空间和子命名空间”。
- 对于权限,检查“执行方法”、“启用帐户”和“远程启用”
- 点击确定三下。
- 然后导航到节点 Root – Microsoft – Windows – DNS。做同样的事情,为 WinRMRemoteWMIUsers 添加权限。
- 重新启动服务“Windows Management Instrumentation。
- 检查问题是否已解决。*
这是完整的 MS official documentation,其中还提供了在本地连接驱动器的步骤
更新:递归复制所有文件和文件夹到$env:temp
powershell runbook 中的代码:
function Get-AzureFiles
{
param([string]$filesharename = 'testfolder', #replace with your own fileshare name
[string]$username = 'your account name',
[string]$password = 'your account key',
[string]$destination=$env:TEMP+"\", #note there is a slash at the end
[string]$path="")
$temp = $env:TEMP
# get the context
$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
# get all files and directories
if($path -eq "")
{
$content = Get-AzureStorageFile -ShareName $filesharename -Context $context
}
else
{
$content = Get-AzureStorageFile -ShareName $filesharename -Context $context -Path $path | Get-AzureStorageFile
}
if(!(test-path $destination))
{
mkdir $destination
}
foreach($c in $content)
{
$p = $c.uri.LocalPath -replace "$($c.share.name)/" ,''
#write-host "the value p is: $p"
#if it's a directory in fileshare
if($c.gettype().name -eq "CloudFileDirectory")
{
# Write-Host "$($c.share.name) is a directory"
$destination =$temp + $c.uri.PathAndQuery -replace "/","\"
#create the folder locally
if(!(test-path $destination))
{
mkdir $destination
#write-host "the new directory $destination is created locally."
}
#define the folder path in fileshare
$path = ($c.uri.localpath -replace "/$filesharename/" , "") -replace "/","\"
Get-AzureFiles -destination $destination -path $path
}
#if it's a file
elseif($c.gettype().name -eq "CloudFile")
{
$s = $temp + $c.uri.PathAndQuery -replace "/","\"
#Write-Output "downloading --- $s"
$d1 = $c.uri.PathAndQuery -replace "/","\"
$d1 = $d1.remove($d1.LastIndexOf("\")+1)
$destination =$temp + $d1
#create the folder locally
if(!(test-path $destination))
{
mkdir $destination
#write-host "the new directory $destination is created locally."
}
$path_temp = $c.uri.PathAndQuery -replace "/$filesharename/",""
Get-AzureStorageFileContent -ShareName $filesharename -Path $path_temp -Destination $destination -Context $context
}
}
}
function do-test
{
get-AzureFiles
# you can operate the files copied to $env:temp
dir $env:TEMP\testfolder
dir $env:TEMP\testfolder\t1
dir $env:TEMP\testfolder\t1\t1sub
}
do-test
测试结果:
我在 Azure 门户中的文件共享结构: