Azure New VM in Powershell -AvailabitySetname - azure

Good morning Team.
Need some guidance on New-AZVM ----- --AvailabilitySetName .
ALL the normal params work just fine, but to use AZ load Balancer, you need a Scale set or -Availability Set for VMs.
Normal Centos VM build works 100% without -AvailabilitySetName
When I Get-AzAvailabilitySet I can see my pre-staged Set listed.
When I pass the Get-AzAvailabilitySetName as given by Get-AzAvailabilitySet I get Parameter cannot be resolved.
Does anybody have a working example, Tech-net only states it must be passed as a string .
I validated my Variable with GM and it is. What am I missing?

Found my own answer after two days of playing around, for those who might have a similar problem, it appears the new-azvm -availabiltysetname does not work. You need to define the AzSET with .id where you declare the other VM basics like size with -availabiltysetID parameter, like this > #Define the parameters for the new virtual machine.
$VirtualMachine = New-AzVMConfig -VMName $azureVmName -VMSize $azureVmSize -AvailabilitySetId $azureAvailSetID
Oh like all the others, it needs to be a string

Related

Ask Bicep Parameters Conditionally

I am creating a an Azure ARM template/Bicep for creating a virtual machine. The template will create both Windows and Linux virtual machines. In the parameters, I want to conditionally ask about the operating system SKU as shown below:
#description('Select the OS type to deploy:')
#allowed([
'Windows'
'Linux'
])
param operatingSystem string
#description('The Ubuntu version (SKU):')
#allowed([
'ubuntu2004'
'ubuntu2004gen2'
])
param ubuntuSKU string
#description('The Desktop Windows version (SKU):')
#allowed([
'win10'
'win11'
])
param windowsSKU string
Currently, the script asks the user to enter both ubuntuSKU and windowsSKU. I would like to show only the parameter which corresponds to the operatingSystem parameter.
For example, if user enters Windows for operatingSystem, the Bicep only asks parameter windowsSKU from user. If user selects Linux for operatingSystem, the Bicep only shows parameter ubuntuSKU.
One workaround is to combine ubuntuSKU and windowsSKU and refactor the rest of my code. However, I prefer to see if there is a better and smarter solution.
There's not dependency/branching in params in the bicep file, there are 2 approaches you can consider though:
combine the options into a single param and use a dictionary lookup in the template, e.g. https://github.com/bmoore-msft/AzureRM-Samples/blob/master/leap-2020/modules/module.ultimate-vm.json#L165-L176 - this works but the few combinations the better.
the other option is to provide the UI for the template and in that ui definition you can set conditions/branching on params. this is pretty involved but works well provided the template will be deployed via the portal... IDK know of an example that does this exact scenario, but this shows some of the capabilities of the overall UI/experience that may help: https://techcommunity.microsoft.com/t5/azure-governance-and-management/using-azure-templatespecs-with-a-custom-ui/ba-p/3586173

Create a site script using Get-SPOSiteScriptFromWeb cmdlet error

I am trying to create a site script from an existing team site but when I run the script that I followed from this Microsoft document it asks me for the WebURL WebURL Prompt, even though it is in the script, then when I provide it at the prompt it gives me the error Error.
I am not new to site scripts but have not used them much. I would like to create one from an existing site that I have created for the PM team. Please advise. I am using the latest SharePoint Online Management Shell and I am logged in. I am also a Global Admin.
Any assistance would be helpful as I have done everything I can think of to do and Googled my heart out but cannot figure out what is going on.
Welcome to StackOverflow!
From the screenshot you posted, it looks like you did not include ` (back tick) at the end of each row in your command. Those are necessary in order for PowerShell to understand entirety of your command which spans across multiple rows, like in the following example:
Get-SPOSiteScriptFromWeb `
-WebUrl https://contoso.sharepoint.com/sites/template `
-IncludeBranding `
-IncludeTheme `
-IncludeRegionalSettings `
-IncludeSiteExternalSharingCapability `
-IncludeLinksToExportedItems `
-IncludedLists ("Shared Documents", "Lists/Project Activities")
Alternative would be to enter entire command and all parameters in single row, like this:
Get-SPOSiteScriptFromWeb -WebUrl https://contoso.sharepoint.com/sites/template -IncludeBranding -IncludeTheme -IncludeRegionalSettings -IncludeSiteExternalSharingCapability -IncludeLinksToExportedItems -IncludedLists ("Shared Documents", "Lists/Project Activities")
Hope this helps :)
Dragan

Azure Devops logging commands in release pipeline

I am trying to customize the output of my pipeline release through setting some env variables into a task.
I found the following link:
https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/logging-commands?view=azure-devops&tabs=powershell
which however does not seem to work.
What I am doing is simply to create a pipeline with a single task (either bash or PS), and there declaring the commands specified in the link through the inline version of the task.
Has anyone already successfully managed to make these commands work?
Do I do something wrong and/or incomplete?
Does anyone have a better way to customise the pipeline with relevant information from a task? E.g. through the release name, or the description and/or tag of the specific release?
Edit:
Write-Host "##vso[task.setvariable variable=sauce;]crushed tomatoes"
Write-Host "##vso[task.setvariable variable=secretSauce;issecret=true]crushed tomatoes with garlic"
Write-Host "Non-secrets automatically mapped in, sauce is $env:SAUCE"
Write-Host "Secrets are not automatically mapped in, secretSauce is $env:SECRETSAUCE"
Write-Host "You can use macro replacement to get secrets, and they'll be masked in the log: $(secretSauce)"
this is the code, copy and pasted. Now I tried also with the script, and it does not work either.
I use an hosted windows agent.
When you set a new variable with the logging command the variable is available only in the next tasks and not in the same task.
So, split your script to 2 tasks, in the second task put the last 3 lines and you will see that the first task works:
this also puzzled me for a while, in the end i found out that if you want to modify the $env:path you can call the special task called task.prependpath by using the special logging command syntax like "##vso[task.prependpath]local directory path". you can find more of this kind of special commands from their source :
https://github.com/microsoft/azure-pipelines-tasks/blob/master/docs/authoring/commands.md

How to pass extra parameters along with New-AzureRmResourceGroupDeployment cmdlet

I'm writing a powershell script to create VM using New-AzureRmResourceGroupDeployment cmdlet, which is as below.
New-AzureRmResourceGroupDeployment -Name VmDeployment `
-TemplateFile C:\template\template.json `
-TemplateParameterFile C:\template\parameters.json
This is used create a VM. In parameters.json , there are some parameters like virtualMachineName, networkInterfaceName etc which are hardcoded.
Now I'm trying to automate these scripts , i.e they run on there own from a tool , when ever some condition is met.
My requirement here is , whenever this script runs, it has to increase the number in the VMName . Suppose the VM Name is now VMName1, it has to be VMName2 when the script runs next time. Similarly VMName3 when the script runs next time. Since the virtualMachineName parameter is hardcoded, this is not happening now. Is there anyway I can pass virtualMachineName as a parameter in the script itself rather than taking it from the json file.
Any guidance is highly appreciated.Thanks!
You can definitely do this, and fortunately there are a handful of ways too.
Pass inline parameters. It says in the Azure PowerShell docs for Templates that you can use inline parameters with a local parameter file and the inline parameters take precedence. Relevant paragraph:
You can use inline parameters and a local parameter file in the same deployment operation. For example, you can specify some values in the local parameter file and add other values inline during deployment. If you provide values for a parameter in both the local parameter file and inline, the inline value takes precedence.
This is valuable because it provides you explicit control over the VM Name parameter, but it is up to the caller (you in this case) to pass an inline parameter. Please note this only works with local parameter files and not remote files (i.e. -TemplateParameterFile and not -TemplateParameterUri). The resulting command would look something like:
New-AzureRmResourceGroupDeployment -Name VmDeployment `
-TemplateFile C:\template\template.json `
-TemplateParameterFile C:\template\parameters.json `
-virtualMachineName VMName42
Modify original parameters.json. You can write some PowerShell/Python/Favorite-scripting-language to parse paramters.json, find the VM Name parameter, find the integer suffix, increment it, and overwrite the file with the new version. This has the benefit of not having to remember to pass an inline parameter, and you won't have to track the version number anywhere as it is already stored in parameters.json. This has one major drawback: it modifies the original JSON which can be dangerous.
Copy parameters.json and modify temporary copy. You can write a script to copy parameters.json to another temporary JSON file and then increment the VM Name parameter during the copy just like in option 2. Pass this temporary file to New-AzureRmResourceGroupDeployment. This has the benefit of not modifying the original parameters.json file but requires you to track the version number somewhere (e.g. another local file, a command line parameter, environment variables, etc.).
For simplicity, I would recommend option 1. It already works out-of-the-box and does not require any external scripts.

Powershell: commandline applications not working after calling method from module

I have created a powershell module (.psm1) file that includes a few other powershell scripts. We use it for sharepoint.
So basically, here's what happens:
I have a deploy script that retrieves the module location from the registry
It loads the module using the Import-Module cmdlet (using -force switch)
This module in turn loads the Sharepoint 2010 snap in and a few other scripts that I created
It runs runs a deployment script that references functions from the included scripts
It also runs a command line application and sends the output directly to the screen
The script will usually work the first time. However, after a few number of tries the commandline tool will stop working and sending output to the screen altogether. And if I try to run a commandline tool (not a cmd-let) after running my script, it don't worky anymore: no output, nothing is done. Its just the same as hitting enter on a blank prompt. anything powershell specific or running GUI applications will work fine but running any console application will not produce any concievable results. the only solution to this, is to just close my powershell and open it again. it will work for usually once and I will have to close it again. our users certainly wont be happy about that..
The most 'notable' things on the script:
scriptblocks are used extensively (for logging), a script block is sent to a handler that executes it using invokecommand and logs the step
its manipulating sharepoint objects
all objects are properly disposed of
no static variables are created nor changed
There are a few global variables shared across all scripts
What I have tried:
I striped my code to a bare minimum: loading an xml file, and restaring a few windows services but I'm still getting this intermittently. I have no idea which part of the code could cause this. I would love to post the code, but our company policy forbids me to. so my aplogies..
Update as per the comment below:
here's roughly how I use codeblocks. I have this function below that is used everytime I want to make the user aware of a task that I'm executing and what it outcome is.
function DoTask($someString, $scriptBlock, $param)
{
try
{
OutputTaskDescription $someString
InvokeCommand $scriptBlock -ArgumentList $param
OutputResultOK
}
catch
{
OutputResultError $_.tostring()
}
}
it could then be used like this:
$stringVar = "something"
$SpSite = new-spsite
deploySomething 'Deploying something' -param $spsite -ScriptBlock {
dosomethingToObject $stringvar
dosomethingToObject $spSite.Name
}
it would then output something like:
Deploying Something ------------- OK
Deploying Something ------------- ERROR
Also notice that I pass the $spsite in the argument list and I just use the string directly. I still don't understand how this works but it seems like I can access all primitive typed variables even without passing them as arguments but I have to pass more complex objects are params, else they dont have any value.
Update:
after much searching and days of pain. I have found others with the same pain. My code exhibits the same exact symptoms as described here:
http://connect.microsoft.com/PowerShell/feedback/details/496326/stability-problem-any-application-run-fails-with-lastexitcode-1073741502
I guess there is no solution yet to this problem.
After a little while I've noticed that if I've ran some very memory intensive functions, I too have gotten that behavior where everything you try to execute just goes to the prompt again. I'd recommend setting Set-PsDebug -Trace 2 to see what those functions are actually doing. I fixed my issue by doing this and figuring out how to make my functions more efficient.

Resources