首页 > 解决方案 > 在 PowerShell Runbook 中使用 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-PSDriveAzure Powershell 中不存在。我在 Azure 中发现这是一个模块,但是当其中一个需要更新 Azure 中的 .Net 时,我停止安装依赖项。

有人可以给我一个端到端的指南,我怎样才能使用 Azure 文件共享实现真正的文件共享功能?

编辑:如果我可以将所有文件从共享复制到当前临时文件夹,那么可接受的解决方法是,因为从那里我可以使用$env:TEMP.

标签: azurepowershellazure-storageazure-powershellazure-runbook

解决方案


更新:递归地将所有文件和文件夹复制到 $env:temp

powershell 运行手册中的代码:

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 门户中的文件共享结构:

在此处输入图像描述


推荐阅读