Import Azure Automation RunBook using Azure Automation - powershell

I am trying to create an Azure Automation job to create a new Azure Automation Runbook. I am using the following to try to get it to work.
$Context = New-AzureStorageContext $storageAccountName $storageAccountKey
$Path = Get-AzureStorageFile -ShareName "qdrive" -Path "TestWorkFlow.ps1" -Context $Context |Select-object Name |Out-String
Import-AzureRMAutomationRunbook -ResourceGroupName $ResourceGroupName -AutomationAccountName $AutomationAccountName -Path $Path -Type PowerShellWorkflow -Force -Name $Name -Published
I get an error message of
Import-AzureRMAutomationRunbook:Cannot find path 'C:\Windows\System32\
Name
------
TestWorkFlow.ps1
I need help figuring out how to send the path of the file to the $path variable in a UNC and not a URI.
Thanks!

The cmdlet needs to take a fully qualified path to the runbook .ps1 file, where the local machine has access to that path via normal local file system referencing. It looks like in this case $Path contains “Name ------ TestWorkFlow.ps1” – so therefore you are not storing the path in $Path correctly, hence the failure.

The $path variable for the -Path switch to the cmdlet needs to contain the full path, Including the filename itself. Like, "C:\Users\Johndoe\TestWorkFlow.ps1". Hope this helps.

Related

How to remove OneDrive folder using PowerShell

I'm trying to remove a user OneDrive folder using PowerShell but I'm not seeing any sucess even though I've been searching around internet so I would be really appreciated if I can get any help or suggestion.
so Just for testing purpose, I'm trying to delete my a folder in my own OneDrive called "Testing" and I wanted to delete everything in there including subfolders and files.
Connect-SPOService -Url https://company-admin.sharepoint.com
$OneDriveURLs = Get-SPOSite -IncludePersonalSite $true -Limit All -Filter "Url -like '-my.sharepoint.com/personal/'"
foreach($OneDriveURL in $OneDriveURLs)
{
Connect-SPOService -Url https://company-admin.sharepoint.com
Connect-PnPOnline -Url $OneDriveURLs
Remove-PnPFolder -Name "Google Drive" -Folder "Testing"
}
Your cmdlet format is not correct, you should follow the structure, more about it in Microsoft Docs
You would need to change <username> to the name the personal drive.
Change your format to :
$drive= https://company-admin.sharepoint.com/personal/<username>
$folder = 'testing'
Remove-PnPFolder -Name $drive -Folder $folder
If that does not work, as an alternative you can try the following powershell module OneDrive and use:
Remove-ODItem -AccessToken $Auth.access_token -ResourceId "https://sepagogmbh-my.sharepoint.com/" -path "/Upload"
You can read more about the module on their GitHub page.

Azure RunBook - Can We pass the key to a file zip the file and push it to a blob (Key refresh)

i am able to create a run book to generate a key and passed into a variable using powershell.
can i move the key into a txt file and zip the same and move it to blob.
You may use below script to accomplish the requirement of sending a value into a text file, then to zip it and then sending it to a storage blob.
$storageAccountName = "xxxxxxxxxxxxxxxxxxxx"
$storageAccountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=="
$context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
$KeyValue = "xxxxxxxxxxx"
$KeyValue | Out-File -FilePath ($env:temp+"/KeyFile.txt")
Compress-Archive -Path ($env:temp+"/KeyFile.txt") -DestinationPath ($env:temp+"/KeyFileZip.zip") -CompressionLevel optimal
Set-AzureStorageBlobContent -File ($env:temp+"/KeyFileZip.zip") -Container "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -BlobType "Block" -Context $context -Verbose
Make sure you update the storage account name, storage account key and container name correctly before using the script.
For illustration on how I accomplished it, please check below screenshots.

Powershell script to copy files from server to azure blob container

I want to copy all the files in my server(azure vm) to a container (azure blob storage) is it possible through powershell?
I'm new to powershell please help me out
In any script with you please share
First, make sure you have installed the Az powershell module in your VM and the place you want to run the command. In my sample, I use my PC to run the command.
Try to store the script below in the PC, I use C:\Users\joyw\Desktop\script.ps1, the script will upload all the files in the folder C:\Users\xxxx\Desktop\test in your VM, you can change it to the path what you want.
script.ps1:
$context = New-AzStorageContext -StorageAccountName "<StorageAccountName>" -StorageAccountKey "<StorageAccountKey>"
$files = (Get-ChildItem "C:\Users\xxxx\Desktop\test" -recurse).FullName
foreach($file in $files){
Set-AzStorageBlobContent -Context $context -File "$file" -Container "<container name>" -Blob "$file"
}
Then run the command:
Connect-AzAccount
Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -Name 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\script.ps1'
If you want to login with non-interactive way, see this link.
Change the parameters to what you want, after uploading the file to the container, it will appear like the original file structure.

How to get the Config Blob Uri parameter from restored VHD from Azure Recovery Services Vault in Powershell?

Currently, I am working in a Powershell to restore the VHD file from a Virtual Machine which is being backed up by Azure Recovery Services Vault.
That being said, my difficulty is how do I get the Config Blob Uri parameter after restoring the VHD using Powershell? Even using Get-AzureRmRecoveryServicesBackupJobDetails -Job $restoreJob I don't see any option that provides this information.
As you can see in the image below, the Azure Portal shows the Config Blob Uri parameter
Once the Powershell completes the restore, then I'd like to retrieve the Config Blob Uri to perform a VM creation based on that specific VHD file, however, without such information, I have to get it manually.
Is there any possibility to get it directly from Powershell?
#get restore job detail
$details = Get-AzureRmRecoveryServicesBackupJobDetails -Job $restorejob
#restored disk properties
$properties = $details.properties
$storageAccountName = $properties["Target Storage Account Name"]
$containerName = $properties["Config Blob Container Name"]
$blobName = $properties["Config Blob Name"]
#Set the Azure storage context and restore the JSON configuration file
Set-AzureRmCurrentStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName
$destination_path = "C:\temp\vmconfig.json"
Get-AzureStorageBlobContent -Container $containerName -Blob $blobName -Destination $destination_path
$obj = ((Get-Content -Path $destination_path -Raw -Encoding Unicode)).TrimEnd([char]0x00) | ConvertFrom-Json
This will download config json file to $destination_path and you can reference your that file when building your VM.
More details at: https://learn.microsoft.com/en-us/azure/backup/backup-azure-vms-automation#restore-an-azure-vm
Also, if you know your Storage Account Name, you can retrieve config uri from there:
$storageAccountName = (Get-AzureRmStorageAccount -ResourceGroupName $resourceGroupName).StorageAccountName
Set-AzureRmCurrentStorageAccount -Name $storageAccountName -ResourceGroupName $resourceGroupName
$storageContainerName = (Get-AzureStorageContainer).Name
$configBlob = Get-AzureStorageBlob -Container $storageContainerName | where {$_.Name -match "json"}
$configName = $configBlob.Name
$configURI = "https://$storageAccountname.blob.core.windows.net/$storageContainerName/$configName"
Hope this helps.

Uploading and downloading files from Azure to Windows using PowerShell

I am writing a PowerShell script to communicate with my Microsoft Azure account, and I am facing an issue with uploading and downloading files using the Set-AzureStorageBlobContent and Get-AzureStorageBlobContent modules.
$Upload = #{
Context = $storageContext;
Container = $container;
File = "C:\... \file.csv";
}
Set-AzureStorageBlobContent #Upload -Force;
The first time I run this, it appears to start uploading, but it stays at 0% and never rises. If I cancel it and try executing my script again, I get an error that says:
"Set-AzureStorageBlobContent : A transfer operation with the same source and destination already exists."
A nearly identical thing happens when I try to download an existing blob from Azure.
$params = #{
Context = $storageContext;
Container = $container;
Blob = $blob;
Destination = $dest
}
New-Item -Path $dest -ItemType Directory -Force
Get-AzureStorageBlobContent #params
I have tried reinstalling Azure.Storage, which is the module that contains the cmdlet Get-AzureStorageBlobContent and Set-AzureStorageBlobContent, but I had no luck. Am I doing something incorrectly, or is this code just wrong somehow?
It seems to be something with your $Upload variable. Try this instead:
Get-ChildItem –Path C:\Images\* | Set-AzureStorageBlobContent -Container "yourcontainername"
more info: https://learn.microsoft.com/en-us/azure/storage/storage-powershell-guide-full