Convert PS variables to Parameters

Jay Thomas
Jay Thomas used Ask the Experts™
on
Hi all,

I have a small PS script which i downloaded to encrypt VMs which works just fine. I have defined variables which work. I'd like not to have to hardcode the variables, instead, when I run the script I'd like to be asked to input them. I know I can do this with parameters but find the search results confusing as there appears to be several types of parameter types. Also, the command at the bottom of the script would surely fail because it is still looking for the variables. Is this a big job to accomplish? Thanks you for looking.

###########################################
$rgName = 'MySecureRg';
 $vmName = 'MySecureVM';
 $KeyVaultName = 'MySecureVault';
 $KeyVault = Get-AzureRmKeyVault -VaultName $KeyVaultName -ResourceGroupName $rgname;
 $diskEncryptionKeyVaultUrl = $KeyVault.VaultUri;
 $KeyVaultResourceId = $KeyVault.ResourceId;

 Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $rgname -VMName $vmName -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId;

##############################################
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I tried this but it errors after I supply the first response. I suspect because of the way I am trying to specify multiple parameters.

Param([Parameter(Mandatory=$true)][string]$NameofKeyVaultResourceGroup)
Param([Parameter(Mandatory=$true)][string]$NameofVirtualMachine)
Param([Parameter(Mandatory=$true)][string]$NameOfKeyVault)
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Try it like this. In addition to parameters, I changed the Set-AzureRmVMDiskEncryptionExtension call to use "splatted"  arguments, that is, a hash table with the arguments to pass.
Easier to read than a long line.
Param(
	[Parameter(Position=0, Mandatory=$true, HelpMessage="The name of the Resource Group")]
	[string]$RgName,
	[Parameter(Position=0, Mandatory=$true, HelpMessage="The name of the VM")]
	[string]$VmName,
	[Parameter(Position=0, Mandatory=$true, HelpMessage="The name of the KeyVault")]
	[string]$KeyVaultName
)
$keyVault = Get-AzureRmKeyVault -VaultName $KeyVaultName -ResourceGroupName $RgName
$splat = @{
	ResourceGroupName = $RgName
	VMName = $vmName
	DiskEncryptionKeyVaultUrl = $keyVault.VaultUri
	DiskEncryptionKeyVaultId = $keyVault.ResourceId
}
Set-AzureRmVMDiskEncryptionExtension @splat

Open in new window

Author

Commented:
Hello ObDa, your script did work, thank you very much. I realised I had left one variable out of the script I uploaded. Rather than come straight back here and ask for help I have spent the last hour trying to get it to work. But no luck. The updated script is here, and just below is my effort to amend the one you did for me. Sorry for being a pain, could you possibly make the amendment for me?

 # Name of KeyVault Resource Group
 $rgName = '';

 # Name of VM Resource Group
 $VirtualMachineResourceGroupName = ''

 # Name of Virtual Machine
 $vmName = '';

 # Name of the KeyVault
 $KeyVaultName = '';

 # The KeyVault Name within the the Resource Group
 $KeyVault = Get-AzureRmKeyVault -VaultName $KeyVaultName -ResourceGroupName $rgname;

 # Keyvault name combined with the Keyvault URI
 $diskEncryptionKeyVaultUrl = $KeyVault.VaultUri;


 $KeyVaultResourceId = $KeyVault.ResourceId;

 #Set encryption
 Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $VirtualMachineResourceGroupName -VMName $vmName -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId;

Open in new window


#########################################################################

Param (
[Parameter(Mandatory=$true)][string]$NameofKeyVaultResourceGroup,
[Parameter(Mandatory=$true)][string]$NameofVirtualMachine,
[Parameter(Mandatory=$true)][string]$NameOfKeyVault,
[Parameter(Mandatory=$true)][string]$NameofVMResourceGroup

)

 # The KeyVault Name within the the Resource Group
 $KeyVault = Get-AzureRmKeyVault -VaultName $KeyVaultName -ResourceGroupName $rgname;

 # Keyvault name combined with the Keyvault URI
 $diskEncryptionKeyVaultUrl = $KeyVault.VaultUri;


 $KeyVaultResourceId = $KeyVault.ResourceId;

 #Set encryption
 Set-AzureRmVMDiskEncryptionExtension -ResourceGroupName $VirtualMachineResourceGroupName -VMName $vmName -DiskEncryptionKeyVaultUrl $diskEncryptionKeyVaultUrl -DiskEncryptionKeyVaultId $KeyVaultResourceId;

Open in new window

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This should work; fixed the issue with the position as well ...
Param(
	[Parameter(Position=0, Mandatory=$true, HelpMessage="Name of the KeyVault")]
	[string]$KVName
	[Parameter(Position=1, Mandatory=$true, HelpMessage="Name of the KeyVault Resource Group")]
	[string]$KVResourceGroup,
	[Parameter(Position=2, Mandatory=$true, HelpMessage="Name of the Virtual Machine")]
	[string]$VMName,
	[Parameter(Position=3, Mandatory=$true, HelpMessage="Name of the VM Resource Group")]
	[string]$VMResourceGroup,
)
$keyVault = Get-AzureRmKeyVault -VaultName $KVName -ResourceGroupName $KVResourceGroup
$splat = @{
	ResourceGroupName = $VMResourceGroup
	VMName = $VMName
	DiskEncryptionKeyVaultUrl = $keyVault.VaultUri
	DiskEncryptionKeyVaultId = $keyVault.ResourceId
}
Set-AzureRmVMDiskEncryptionExtension @splat

Open in new window

Author

Commented:
Absolutely brilliant!  Going to look at it side by side see if I can't learn something here. Thank you very much!
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
For the sake of completeness and full disclosure: you seem to have noticed that there was a comma missing after the first argument, and one too many after the last (changed the order; one of those last-second things).
In case anybody else stumbles over this, here's the corrected version:
Param(
	[Parameter(Position=0, Mandatory=$true, HelpMessage="The name of the KeyVault")]
	[string]$KVName,
	[Parameter(Position=1, Mandatory=$true, HelpMessage="Name of KeyVault Resource Group")]
	[string]$KVResourceGroup,
	[Parameter(Position=2, Mandatory=$true, HelpMessage="Name of Virtual Machine")]
	[string]$VMName,
	[Parameter(Position=3, Mandatory=$true, HelpMessage="Name of VM Resource Group")]
	[string]$VMResourceGroup
)
$keyVault = Get-AzureRmKeyVault -VaultName $KVName -ResourceGroupName $KVResourceGroup
$splat = @{
	ResourceGroupName = $VMResourceGroup
	VMName = $VMName
	DiskEncryptionKeyVaultUrl = $keyVault.VaultUri
	DiskEncryptionKeyVaultId = $keyVault.ResourceId
}
Set-AzureRmVMDiskEncryptionExtension @splat

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial