Passing parameter to script from Invoke-AzVMRunCommand - azure

I trying to execute the Invoke-AzVMRunCommand to execute a PS function that accept parameters. The code below shows call to the Invoke-AzVMRunCommand
$runcmdparameters=#{
"VolumeLable"="sdsd";
"azurelun"="1,3,4"
}
Invoke-AzVMRunCommand -ResourceGroupName $ServerResourceGroupName -VMName $VMVame -ScriptPath "c:\Configurestorage.ps1" -CommandId 'RunPowerShellScript' -Parameter $runcmdparameters -Verbose
The PS Script that I need to execute on the server is
function Configure-Storage
{
Param(
[parameter(Mandatory=$true)][String]$VolumeLable,
[parameter(Mandatory=$true)][String[]]$azurelun
)
#create a storage pool for user databases.
Out-File "C:\Temp\log.txt" -InputObject $VolumeLable -Append
}
Configure-Storage -VolumeLable $VolumeLable -azurelun $azurelun
The script fail with Cannot bind argument to parameter 'VolumeLable'
because it is an empty string.
Microsoft documentation on Invoke-AzVMRunCommand is not very helpful.
How can I pass the parameter to the script?

I think you need to do this:
Param(
[parameter(Mandatory=$true)][String]$VolumeLable,
[parameter(Mandatory=$true)][String[]]$azurelun
)
#create a storage pool for user databases.
Out-File "C:\Temp\log.txt" -InputObject $VolumeLable -Append
Because what happens now: your script doesnt really accept parameters, you function does, but you are calling the script, not the function. and then you call the function inside the script. another way of fixing this - add parameters to the script itself

Related

Issue with Start-ThreadJob ScriptBlock Unable to find powershell script

I am using Start-ThreadJob and ScriptBlock to execute a powershell script in a new thread. It works fine on my local but on the preprod server, I am getting an error.
Code Block where I am initiating a new thread
Start-ThreadJob -InputObject $fileType -ScriptBlock {
./Functions/Download-FilesFromFTP.ps1 $args[0] $args[1] $args[2] $args[3] $args[4] $args[5]
} -ArgumentList $ftpServer,$user,$password,$completeSourceFolder,$completeStagingFolderPath,$completeLogFolderPath
As mentioned earlier, this code block works perfectly on my local. On Preprod env I get the following error when I display jobs using Get-Jobs command.
Powershell version on my local
Powershell version on preprod server
The version of the module ThreadJob is same on both servers
Start-ThreadJob runs the new thread with the same current location as the caller, which is unrelated to where the executing script is located.
If you want to refer to a file relative to the script's own location, use the automatic $PSScriptRoot variable, and refer to it in the thread script block via the $using: scope:
Start-ThreadJob -InputObject $fileType -ScriptBlock {
& "$using:PSScriptRoot/Functions/Download-FilesFromFTP.ps1" #args
} -ArgumentList $ftpServer,$user,$password,$completeSourceFolder,$completeStagingFolderPath,$completeLogFolderPath
Note the use of #args in order to also pass all positional arguments, reflected in the automatic $args array, through as individual arguments to the target script via splatting.

Any way to get rid of an obsolete parameter in Azure PowerShell script (AzureRM to Az changing)

As you may know, MSFT is getting rid of AzureRM cmdlets in favor of Az.
There are a lot of issues regarding this since the proposed native aliases "Enable-AzureRmAlias" seems to stop being updated.
I have a script based on AzureRM in one repo, that triggers by Azure DevOps release pipeline step function (Azure PowerShell based),
that has the following piece of code:
$var = (Get-AzureKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText
"Enable-AzureRmAlias" command activated as well..., that converts the code like this:
$var = (Get-AzKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText
The problem is, that ".SecretValueText" was deprecated a while ago. Instead of it, a new parameter has been added to the Get-AzKeyVaultSecret cmdlet - "-AsPlainText"
so... theoretically the final construction has to be like this:
$var = Get-AzKeyVaultSecret -VaultName $vaultName-Name $Key -AsPlainText
Challenges!
I can't upgrade the original script in the repo to Az due to the necessity of back-compatibility.
The only way to solve it - is to create some kind of alias in Azure PowerShell inline script (that triggers the main script in the repo)
I stuck with this ".SecretValueText"
My original idea to put the following into the inline script doesn't seem to be working:
function Get-AzKeyVaultSecretNew {
Param(
$vaultName,
$Key
)
$var = Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText
return $var
}
Set-Alias -Name Get-AzKeyVaultSecret -Value Get-AzKeyVaultSecretNew
Any ideas on how to accomplish this?
This should theoretically help your situation. You can run this code at the beginning of the PowerShell session that will be calling your scripts. You will need to make sure any necessary modules are loaded so that the secret object types are loaded.
$Script = { Get-AzKeyVaultSecret -VaultName $this.VaultName -Name $this.Name -AsPlainText }
Update-TypeData -TypeName 'Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecretIdentityItem' -MemberName 'SecretValueText' -MemberType ScriptProperty -Value $Script
The idea is to add the SecretValueText property back to the Microsoft.Azure.Commands.KeyVault.Models.PSKeyVaultSecretIdentityItem objects.
You can try using below workaround to replace below piece of code:
(Get-AzureKeyVaultSecret -VaultName $vaultName-Name $Key).SecretValueText
with Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText via using RegEx Find & Replace task. Check below steps:
1, Add task RegEx Find & Replace to replace the orginal code with the converted code. See below:
FindRegex: '\(Get-AzureKeyVaultSecret -VaultName \$vaultName -Name \$Key\)\.SecretValueText'
ReplaceRegex: 'Get-AzKeyVaultSecret -VaultName $vaultName -Name $Key -AsPlainText'
2, -AsPlainText parameter is only available in the latest az 5.3.0 version. Since the version installed in cloud agent is 4.7.0. You need to install the az 5.3.0 version before executing your script. See below. Use a powershell task to install az 5.3.0 version.
New-Item -Path "C:\Modules" -Name "az_5.3.0" -ItemType "directory"
Save-Module -Name AZ -RequiredVersion 5.3.0 -Path "C:\Modules\az_5.3.0"
3, Then you can invoke your script in the azure powershell task directly.

Assistance needed with powershell script

I am trying the execute the powershell script found at :
https://github.com/amanbedi18/Azure-KeyVault-Automation/tree/master/UploadSecrets
https://github.com/amanbedi18/Azure-KeyVault-Automation
This script is used to automate upload of secrets to azure key-vault.
I have created the below json file and named it /user/kv/AzureSecretsMetaData.json
[
{
"key": "test1",
"value": "1"
},
{
"key": "test2",
"value": "2"
}
]
I then copied contents of setKeyVaultSecrets.ps1 to /user/kv/setKeyVaultSecrets.ps1
I have tried the below commands that were mentioned in the readme and powershell script files and get the below errors, could someone please help me execute this script correctly and point out what I am doing wrong here.
./setkeyVaultSecret.ps1 -KeyVaultName 'avkv01'
setkeyVaultSecret.ps1: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.
./setkeyVaultSecret.ps1 -KeyVaultName 'avkv01' -KVSecretMetadataFilePath '/home/aditya/kv/AzureSecretsMetaData.json'
setkeyVaultSecret.ps1: Parameter set cannot be resolved using the specified named parameters. One or more parameters issued cannot be used together or an insufficient number of parameters were provided.
Any help is greatly appreciated. Thank You
If these are scripts you found, they are very poorly written and I would advise not using them. I would suggest installing the az powershell module and using those scripts as a reference in writing your own script. You can install the az powershell module with powershellget:
Install-module az -force -allowclobber
The way the script that you are trying to call is written very strangely. It defines two different parameter sets, but each of the two parameters are mandatory for BOTH parameter sets?! That doesn't make a lot of sense. If you must run them, you will probably need to modify them a bit. They don't really seem finished.. Why not modify the script and get rid of the parameter sets altogether? Also, the example in the script indicates that the "$KVSecretMetadataFilePath" parameter is not mandatory, since it is not being called, but is defined as being mandatory when defining your parameter. You need to make clear if this parameter is required or not. If it is required Make it look more like this:
<#
.PREREQUISITE
1. An Azure key vault and its name as parameter.
2. Json template should be properly populated with valid json schema in sampleSecretValues.json in KeyVaultjson directory.
.PARAMETER vaultName
The name of the key vault.
.EXAMPLE
. setKeyVaultSecret.ps1 -KeyVaultName 'somekeyvault'
#>
# provision keys and secrets to a key vault
Param(
[Parameter(Mandatory=$true)]
[String]
$KeyVaultName,
[Parameter(Mandatory=$true)]
[String]
$KVSecretMetadataFilePath
)
Install-Module -Name AzureADPreview -ErrorAction SilentlyContinue -Force
Import-Module Azure -ErrorAction SilentlyContinue
Import-Module AzureRM.Resources
Set-StrictMode -Version 3
$json = Get-Content $KVSecretMetadataFilePath | Out-String | ConvertFrom-Json
$json | ForEach {
$secretToSearch = Get-AzureKeyVaultSecret -VaultName $KeyVaultName -Name $_.key -ErrorAction SilentlyContinue
if($secretToSearch -ne $null)
{
echo "The secret $_.key already exists !"
}
Else
{
$NewSecret = Set-AzureKeyVaultSecret -VaultName $KeyVaultName -Name $_.key -SecretValue (ConvertTo-SecureString $_.value -AsPlainText -Force ) -Verbose
Write-Host
Write-Host "Source Vault Resource Id: "$(Get-AzureRmKeyVault -VaultName $KeyVaultName).ResourceId
}
}
If it is not required, remove the line "[Parameter(Mandatory=$true)]" above the $KVSecretMetadataFilePath declaration.

Azure Powershell - iterate through a list (txt) with db names and set them as variables in script

I have the following challenge at the Moment:
With T-SQL I created a list of databases attached to our SQL instance and put this list in a txt file, so far no Problem.
Now with PowerShell I want to set up LongTermRetention backups in Azure with following cmdlet:
Set-AzSqlDatabaseBackupLongTermRetentionPolicy -ServerName $serverName -DatabaseName [name of database] -ResourceGroupName $resourceGroup -WeeklyRetention P53W -MonthlyRetention P48M -YearlyRetention P10Y -WeekOfYear 52
(I checked the Syntax of the cmdlet by inserting a database name manually and it works)
the variables given are set prior (ServerName, resourceGroup)
Where I am struggling now is, how I can get the names of the txt file one after another as parameter "DatabaseName" into my cmdlet, as I have never done something like this.
I would be very grateful for either a link on how to do this or maybe a solution posted here.
Thank you in advance and Kind regards
EDIT: I found a solution, Maybe not the most elegant, but working:
$files = Get-Content "C:\DEV\testfile.txt"
foreach ($file in $files){
Set-AzSqlDatabaseBackupLongTermRetentionPolicy -ServerName $serverName -DatabaseName $file -ResourceGroupName $resourceGroup -WeeklyRetention P53W -MonthlyRetention P48M -YearlyRetention P10Y -WeekOfYear 52
}
I haven't thought it might have been that easy.
If you have a flat text file where your database names are present like this:
server1
server2
server3
......
You can use the Get-Content cmdlet to read the file and iterate over each line using the ForEach-Object cmdlet. You can access the current item using $_ ($_ represents the current value in the pipeline):
Get-Content -Path 'pathToYourFile.txt' | ForEach-Object {
Set-AzSqlDatabaseBackupLongTermRetentionPolicy `
-ServerName $_ `
-DatabaseName [name of database] `
-ResourceGroupName $resourceGroup `
-WeeklyRetention P53W `
-MonthlyRetention P48M `
-YearlyRetention P10Y `
-WeekOfYear 52
}

Cannot convert the value of type "System.String" to type "System.Collections.Hashtable"

In Azure Devops I'm trying to run a block of Azure Powershell inline script:
Invoke-AzVMRunCommand -ResourceGroupName xxx-Name xxx -CommandId 'RunPowerShellScript' -ScriptPath $env:SYSTEM_DEFAULTWORKINGDIRECTORY/_alias/xxx.ps1 -Parameter $env:xxx -Verbose
As you can see I am passing a parameter to the xxx.ps1 script. This is a small piece of the content of the xxx.ps1 script:
Param(
[string]$xxx
)
#create AD Group
$ADGroupName = "AD" + $xxx+ "_AD"
.....
When I run this code I get the following error in the logs:
[error]Cannot bind parameter 'Parameter'. Cannot convert the "valueofthevariable" value of type "System.String" to type "System.Collections.Hashtable".
How can I correctly pass the value as parameter?
Thanks!
After some more searching online I found my mistake:
In the Powershell command Invoke-AzVMRunCommand I used a String as input type for Parameter, this has to be a hashtable.
I changed that command to the following:
Invoke-AzVMRunCommand -ResourceGroupName xxx-Name xxx -CommandId 'RunPowerShellScript' -ScriptPath $env:SYSTEM_DEFAULTWORKINGDIRECTORY/_alias/xxx.ps1 -Parameter #{xxx = $env:xxx} -Verbose

Resources