Convert PS variables to Parameters

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;

##############################################
LVL 1
Jay ThomasAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jay ThomasAuthor 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)
oBdACommented:
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

Jay ThomasAuthor 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

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

oBdACommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jay ThomasAuthor Commented:
Absolutely brilliant!  Going to look at it side by side see if I can't learn something here. Thank you very much!
oBdACommented:
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.