Cannot convert string to boolean - ansible to powershell on linux - linux

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'

Related

azure VM extension Powershell Custom script

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

executing the powershell commands using python

import os
os.system("powershell.exe [Get-ItemProperty
HKLM:\\Software\\Wow6432Node\\Microsoft\\\Windows\\CurrentVersion\\Uninstall\\*| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table –AutoSize > D:\\application whitelisting\\InstalledProgramsPS.txt ]")
this is my code. I want to display the list of software installed in the system.
but i am getting error like
Select-Object is not recognized as an external or internal command.
when i execute the same command using powershell, it is working fine.
can anyone please help?
thanks in advance.
The reason is that Powershell's command parameter is not properly constructed. os.system() call will star a CMD session, and Select-Object is not recognized as an external or internal command is an error message from CMD.
Let's see what CMD does. First it will run Powershell and pass some arguments. Note the triple backslash, which is an error by itself and needs to be fixed.
powershell.exe [Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\\Windows\\CurrentVersion\\Uninstall\\*
| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate
Now, the first thing is that Powershell is invoked and (wrongly) paramerized Get-ItemProperty is passed. Because the pipe char | is used in CMD as well, it is interpreted as a command for CMD. Thus CMD tries to pipe the first command's output to Select-Object, but there isn't such a command in CMD. Thus the error.
To fix the issue, use -command "<commands>" to pass commands to Powershell. The double quotes " are used to create a single string that CMD passes to Powershell as an argument.
powershell.exe -command "Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-Table -AutoSize > D:\\application whitelisting\\InstalledProgramsPS.txt"

How to hide console output from Select-AzureRmSubscription

Does anyone know how to hide output from command Select-AzureRmSubscription inside azure workbook which runs as powershell workflow
Thanks
You can use Out-Null. Works for any PowerShell cmdlet.
Select-AzureRmSubscription | Out-null
The Out-Null cmdlet sends its output to NULL, in effect, removing it
from the pipeline and preventing the output to be displayed at the
screen.
https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.core/out-null
Select-AzSubscription -SubscriptionId $s.Id | Out-Null + ~~~~~~~~ Cannot call the 'Out-Null' command. Other commands from this module have been packaged as workflow activities, but this command was specifically excluded. This is likely because the command requires an interactive Windows PowerShell session, or has behavior not suited for workflows. To run this command anyway, place it within an inline-script (InlineScript { Out-Null }) where it will be invoked in isolation

PowerShell Start-Process loses precision on number passed as a string to a function

I have some code that edits the registry, so it needs to run as admin. To do this, I start up a new PowerShell process from my running PowerShell script, and pass in part of the registry key path, which happens to be a version number, e.g. "12.0". The function in the new PowerShell process receives the string as "12" though, not "12.0", and so I'm getting errors that it can't find the registry key.
I've created a little sample powershell script that reproduces the problem. Here's the snippet:
$ScriptBlock = {
function Test([string]$VisualStudioVersion)
{
$VisualStudioVersion # This always displays 12, instead of 12.0
$Host.UI.RawUI.ReadKey()
}
}
# Run the script block's function.
Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test(""12.0"")}"
Here I've hardcoded "12.0", but in practice I want to pass in a variable.
Any ideas on what I'm doing wrong? Thanks in advance.
Ok, after some experimenting the following seems to work correctly:
Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test('12.0')}"
and it even works when using a variable:
$version = "12.0"
Start-Process -FilePath PowerShell -ArgumentList "-Command & {$ScriptBlock Test('$version')}"
I'm still not sure why using double quotes causes it to lose the precision and single quotes keeps it, but at least I solved my problem.
Update
So it turns out I'm a dummy and the problem was that I was using C# syntax of Test(""$version"") to call the function, instead of the proper PowerShell syntax Test ""version"". With this change it now works as expected.

visual studio 2012 multiple -commands in command line arguments

In visual studio 2012 it is possible on the debug-> start options to specify command line arguments. I am working on a powershell cmdlet so I'd like to be able to parse multiple commands into powershell. My arguments looks like this
-noexit -command add-pssnapin Registerv2.0 -command New-Token -command www.google.com
the problem is it is treating -command as 1 long string i.e -command "add-pssnapin Registerv2.0 -command New-Token -command www.google.com" rather then 3 seperae commands. Does anyone know how to change this:
edit
the results I am looking for is when I run the project
power shell opens
my snapin is registered
Call the cmdlet new-token
enter in the cmdlet parameters
If you first want add-pssnapin Registerv2.0 and then New-Token to be called, you should chain them in one command, like so:
-command "add-pssnapin Registerv2.0; New-Token"
If New-Token expects a parameter, you should pass it on the command line directly, instead of trying to simulate user input.
For example, New-Item would expect a list of paths and a type as input, both can also be supplied on the command line as parameters. Like so:
New-Item foo -type directory
So, how you would pass the value www.google.com to New-Token depends on the name of the parameter. But could look like:
-command "add-pssnapin Registerv2.0; New-Token -tokenName www.google.com"

Resources