I want to pass the variables inside my terraform script when I call the PowerShell script inside "azurerm_virtual_machine_extension". i don't know how to pass arguments to my powershell script. I have used the following code.
"commandToExecute": "powershell.exe -ExecutionPolicy Unrestricted -file ${var.main_hybridscriptname} -AAResourceGroupName ${var.aobclResourceGroup} -OMSResourceGroupName ${var.aobclResourceGroup} -SubscriptionID ${var.azure_subscription_id} -AutomationAccountName ${module.automationAccount.AutomationName} -HybridGroupName ${var.hybridgroupname} -WorkspaceName ${azurerm_log_analytics_workspace.aobclloganalyticsworkspace.name} -appidSPN ${var.SPN_APP_ID} -SPNpswd ${var.SPN_PSWD} -tenantID ${var.azure_tenant_id}"
error in script execution when i use a script of test without argument, it worksstrong text
Related
This code:
$username = 'Username'
$password = 'Password'
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential $username, $securePassword
Start-Process -FilePath powershell -WorkingDirectory "$env:ALLUSERSPROFILE" -Credential $credential -WindowStyle Hidden -ArgumentList "-NoProfile -Command `"Start-Process -FilePath wscript -Verb RunAs -ArgumentList '\`"$((Get-Location).Path -replace "'", "''")\test.vbs\`" \`"/CurrentDirectory:$((Get-Location).Path -replace "'", "''")\`" \`"/AppData:$($env:APPDATA -replace "'", "''")\`"'`""
works when I run it line by line from the PowerShell command prompt.
The problem occurs both when I associate the .ps1 extension to "Windows PowerShell" and then double-click on the script, and when I compile the script in .exe through IronMan Software's "PSScriptPad".
A similar problem is present at the following link:
Powershell script executes correctly when I choose "run with powershell", but not when I "open with" powershell or double-click it
but in my case it doesn't even work with "Run with PowerShell" and perhaps the cause of the problem is the same one that also affects executables.
Another useful link is the following:
https://stackoverflow.com/a/58245206/45375
How can I solve the problem?
Windows 10 Pro 64-bit
Powershell Version: 5.1.19041.1237 (Integrated in Windows 10).
The compiled executable works correctly when it is not in a directory containing the apostrophe characters while in the case of double-clicking on a .ps1 script, the directory containing it cannot even contain spaces.
This is a useful link:
https://social.technet.microsoft.com/Forums/en-US/bf3344de-3af6-48e3-9f43-f595bb41c62d/bug-powershell-starts-w-error-when-opened-by-context-menu-in-folder-w-apostrophe-in-its-name?forum=win10itprogeneral
For the apostrophes path problem in .exe files, I sent a bug report to IronMan Software PSScriptPad, which was then taken care of.
For the problem of paths with apostrophes or consecutive whitespaces when double-clicking or doing "Run with PowerShell" on .ps1 scripts, I have solved by fixing the following registry values:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.ps1]
#="Microsoft.PowerShellScript.1"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Directory\Shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Drive\shell\Powershell\command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoExit -Command \"Set-Location -LiteralPath \\\"%V\\.\\\"\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\DefaultIcon]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\",0"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\0\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -Command \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force }; & \\\"%1\\\"\""
[HKEY_CLASSES_ROOT\SystemFileAssociations\.ps1\Shell\Edit\Command]
#="\"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe\" -File \"%1\""
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell]
"ExecutionPolicy"="RemoteSigned"
I read this whole thread about how to pass boolean values to a parameter to powershell on Linux. Nothing worked for me.
My code in Ansible is as follows:
- name: Install PowerCLI
shell: pwsh -Command "Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCEIP:$False -Confirm:$False -InvalidCertificateAction Ignore"
I've used many variants, such as -ParticipateInCEIP:False, or -ParticipateInCEIP False, or -ParticipateInCEIP $false, but I get always the same error, that it expects boolean, but I sent string.
I am running this Ansible task against a Linux machine with Powershell installed.
Any tips on how to make it work?
Best,
Francis
When you shell: pwsh -Command "something -switch:$powershellVariable", with double quotes, $powershellVariable will be evaluated by the Linux shell before passing it to PowerShell.
Unless you have an actual $powershellVariable defined in your shell, it will be passed to PowerShell as something -switch:
Try rewriting with single quotes:
shell: pwsh -Command 'Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCEIP:$False -Confirm:$False -InvalidCertificateAction Ignore'
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.
could someone please help me with this? I have issues running an EXE from azure blob storage combined with a configuration file. The exe is called from the blob and then needs an argument that calls an xml configuration file in the same blob directory as the exe file. The exe is called fine but the config file is not. Any ideas?
If i run the c:\temp\setup.exe /configure configuration.xml it runs fine so i'm really not sure?
$Path = $env:TEMP
$Installer = "setup.exe"
$OfficeConfig = "configuration.xml"
Invoke-WebRequest "https://********.blob.core.windows.net/office1/setup.exe?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-16T16:00:03Z&st=2019-07-16T08:00:03Z&spr=https&sig=7sO7m3PJMne8hFYAwHY6YFZZG6AfUuhC8x%2BoR7QkWuY%3D" -OutFile $Path\$Installer
Invoke-WebRequest "https://********.blob.core.windows.net/office1/Configuration.xml?sv=2018-03-28&ss=bfqt&srt=sco&sp=rwdlacup&se=2019-07-16T16:00:03Z&st=2019-07-16T08:00:03Z&spr=https&sig=7sO7m3PJMne8hFYAwHY6YFZZG6AfUuhC8x%2BoR7QkWuY%3D" -OutFile $Path\$OfficeConfig
Start-Process -FilePath $Path\$Installer -Argumentlist'/configure $Path\$OfficeConfig' -Verb RunAs -Wait
Remove-Item $Path\$Installer
You should change the single quote to double quote for the -Argumentlist parameter in the Start-Process cmdlet, like below:
change -Argumentlist '/configure $Path\$OfficeConfig' to -Argumentlist "/configure $Path\$OfficeConfig"
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