-1

I would like to move a ps script to Azure to run as a runbook. It basically checks all new SharePoint sites and checks if they meet certain criteria, like enables version control if not set.

The script works like:

$sitesdonelist = "c:\log.txt"
$sitesdone = get-content -Path $sitesdonelist

foreach($sitecoll in $sitecollections) {
 $currentsite = $sitecoll.Url
 if ($sitesdone -inotcontains $currentsite) {
 checksite
 add-content -Path $sitesdonelist -Value $sitecoll.Url
 }
}

I would like this code to work on Azure and part of this is to move the sites done list to an Azure file share and append the processed sites to it. So far I see two options and none of them seem right:

  1. Download the file to $env:TEMP, append and upload on finish: I will miss all sites done if something fails halfway
  2. Download the file to $env:TEMP, append and upload after every site: would make the process slow and cause lot of unnecessary data load

Is there a better option? Can I write directly to a file on Azure file share from an Azure PowerShell runbook?

vilmarci
  • 451
  • 10
  • 31

1 Answers1

0

If you want to write something as log to a file in your Azure file share from Azure Automation runbook , pls try the PS command below :

    $appid = "<your appliaction ID>"
    $passwd = "<your Azure AD application Client secret>"
    $tenantId= "<your tenant ID>"
    $secpasswd = ConvertTo-SecureString -String $passwd -AsPlainText -Force
    $cred = New-Object Management.Automation.PSCredential ($appid, $secpasswd)
    Connect-AzAccount -ServicePrincipal -Credential $cred -Tenant $tenantId

    $fileName = "<file name,including path>"
    $storageacc =  Get-AzStorageAccount -ResourceGroupName <resource group name> -Name <storage account name> 
    $file = (Get-AzStorageFile -ShareName qsfileshare -Context $storageacc.Context -Path $fileName)[0]

    $content = "content you want to write"

    $file.UploadTextAsync($file.DownloadTextAsync().GetAwaiter().GetResult() + $content).GetAwaiter().GetResult()
#get the content of the file that we write.
    $file.DownloadTextAsync().GetAwaiter().GetResult() 

Result in automation :

enter image description here

Previously , the content of the file is "hello!!!" only, as you can see ,the content has been written to the file . Hope it helps .

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16
  • This looks like a download - append - upload - download ... solution, similar to my tries described in my question. I don't really want to down- and upload the same file 1000 times, when looping through all the sites. – vilmarci Oct 14 '19 at 10:43
  • Hi @vilmarci, If so , how about creating log files separately for each site in certain folder ? When you need to fetch logs , read all log files in that folder one by one .By this way you can avoid log file sync and issues about download - append - upload for multiple times – Stanley Gong Oct 14 '19 at 11:11
  • that's overkill, I only need one row per site in the file (the url), I want to read it once, to only process new sites, and write it per site to make sure the progress is saved, even if the script times out. – vilmarci Oct 14 '19 at 13:17