$connectionName = "AzureRunAsConnection" try { # Get the connection "AzureRunAsConnection " $servicePrincipalConnection = Get-AutomationConnection -Name $connectionName "Logging in to Azure..." $account = Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } Write-Output $account $todaydate = Get-Date -Format MM-dd-yy $LogFull = "AzureScan-$todaydate.log" $LogItem = New-Item -ItemType File -Name $LogFull " Text to write" | Out-File -FilePath $LogFull -Append #Get key to storage account $acctKey = (Get-AzureRmStorageAccountKey -Name $storageaccount -ResourceGroupName $resourcegroup).Value[0] #Map to the reports BLOB context $storageContext = New-AzureStorageContext -StorageAccountName $storageaccount -StorageAccountKey $acctKey #Copy the file to the storage account Set-AzureStorageBlobContent -File $LogFull -Container "files" -BlobType "Block" -Context $storageContext -Verbose