Auto Answer Powershell Prompts from Scripts - powershell

I have written a powershell script which uses a third party Cmdlet (https://www.powershellgallery.com/packages/OdooCmdlets/21.0.8137.1).
The script is working fine on my local enviroment, but i need to apply it to an Azure Function to generate a daily execution (since it process data into a Azure SQL Database).
On its first line (see script below), i have establish the connection parameters where a prompt / pop-up window appears, which is easy to anwser when via user interaction from a local enviromnt (Powershell ISE)
However, when i try to run the script on the azure function the following error appears:
ERROR: A command that prompts the user failed because the host program or the command type does not support user interaction. The host was attempting to request confirmation with the following message: Would you like to install a license now? (You may use "TRIAL" as the Product Key to activate a trial license.)
I need to adapt the script in order to auto-anwser the prompt / pop-up window, or find another solution.
Powershell Script:
Import-Module OdooCmdlets
$odoo = Connect-Odoo -User 'xxxxx' -Password 'xxxx' -URL 'xxxxx' -Database "xxxx"

I've never used the cmdlets before but inspecting the help files: https://www.powershellgallery.com/packages/OdooCmdlets/21.0.8137.1/Content/lib%5Cnet20%5CCData.OdooCmdlets.Commands.dll-help.xml it appears there is an -RTK parameter available.
Is it possible you have a key which you can pass as part of the connection which would suppress the prompt?
Failing that, the developer docs have examples for Python which you could either translate to PowerShell and not use the Cdata Odoo module or just use python in your Azure Functions
https://www.odoo.com/documentation/15.0/developer/misc/api/external_api.html

Related

Why am I getting errors when running PowerShell command "Get-WindowsCapability" in .NetCore console application?

New to PowerShell in .NetCore and the script works fine in PowerShell ISE, but when I run through my console application.
Here is the script:
Get-WindowsCapability -Online -Name Microsoft.Windows.Notepad*
Here is the output through PowerShell ISE:
Name : Microsoft.Windows.Notepad~~~~0.0.1.0 State :
Installed DisplayName : Notepad Description : View, edit, and search
through plain text documents and source code files instantly.
DownloadSize : 301710 InstallSize : 647868
Here is my code snippet from my .NetCore console application:
var psInstance = PowerShell.Create();
psInstance.AddScript("Get-WindowsCapability -Online -Name Microsoft.Windows.Notepad*");
var output = psInstance.Invoke();
psInstance?.Runspace?.Close();
if (psInstance.HadErrors)
{
var error = psInstance.Streams.Error.Select(e => e.ToString());
}
Here is the error I'm seeing in my pInstance object:
Count = 5
[0]: "The 'Get-WindowsCapability' command was found in the module 'Dism', but the module could not be loaded due to the following error:
[File
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Dism\Dism.psm1
cannot be loaded because running scripts is disabled on this system.
For more information, see about_Execution_Policies at
https://go.microsoft.com/fwlink/?LinkID=135170.]\r\nFor more
information, run 'Import-Module Dism'."
[1]: "The 'Get-WindowsCapability' command was found in the module 'Dism', but the module could not be loaded due to the following error:
[File
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\Dism\Dism.psm1
cannot be loaded because running scripts is disabled on this system.
For more information, see about_Execution_Policies at
https://go.microsoft.com/fwlink/?LinkID=135170.]\r\nFor more
information, run 'Import-Module Dism'."
[2]: "[localhost] Connecting to remote server localhost failed with the following error message : WinRM cannot process the request.
The following error with errorcode 0x8009030e occurred while using
Negotiate authentication: A specified logon session does not exist. It
may already have been terminated. \r\n Possible causes are:\r\n -The
user name or password specified are invalid.\r\n -Kerberos is used
when no authentication method and no user name are specified.\r\n
-Kerberos accepts domain user names, but not local user names.\r\n -The Service Principal Name (SPN) for the remote computer name and port does not exist.\r\n -The client and remote computers are in
different domains and there is no trust between the two domains.\r\n
After checking for the above issues, try the following:\r\n -Check
the Event Viewer for events related to authentication.\r\n -Change
the authentication method; add the destination computer to the WinRM
TrustedHosts configuration setting or use HTTPS transport.\r\n Note th
at computers in the TrustedHosts list might not be authenticated.\r\n
-For more information about WinRM configuration, run the following command: winrm help config. For more information, see the
about_Remote_Troubleshooting Help topic.\r\n Other Possible Cause:\r\n
-The domain or computer name was not included with the specified credential, for example: DOMAIN\UserName or COMPUTER\UserName."
[3]: "Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or
empty, and then try the command again."
[4]: "Cannot validate argument on parameter 'Id'. The argument is null. Provide a valid value for the argument, and then try running the
command again."
I a running the console app as Administrator.
Why does the script run successfully through PowerShell IDE, but throw errors through the sample app?
Is there something I additionally have to do to my PowerShell instance object?
tl;dr
Configure your PowerShell SDK-based session with an execution policy that permits script execution, such as RemoteSigned
See this answer for an example.
Background information.
The obsolescent[1] Windows PowerShell ISE invariably runs Windows PowerShell, whereas your .NET (Core) C# application of necessity uses PowerShell (Core)'s SDK.
The two PowerShell editions have separate execution policies, and it sounds like while Windows PowerShell on your system is configured to permit script execution, PowerShell (Core) is not.
It may be surprising that the Dism module, which Get-WindowsCapability is a part of, is subject to the script execution policy, given that modules aren't .ps1 script files.
However, many modules are, namely if they (also) contain PowerShell code, which is not only true for script modules that are entirely implemented in PowerShell code (*.psm1), but also for primarily binary modules (those that provide their cmdlets via assemblies, i.e. compiled code), if such modules also contain PowerShell code and/or formatting files (*.format.ps1xml) or type-definition files (*.types.ps1xml).
The most robust solution is to set the desired execution policy on a per-session basis, as shown in the linked answer.
[1] The Windows PowerShell ISE is no longer actively developed and there are reasons not to use it (bottom section), notably not being able to run PowerShell (Core) 6+. The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension.

unattend setup returns error 0xfffd0000 during specialize pass

I'm trying to run a PowerShell script using unattend.xml file for Windows Server 2012 R2 setup. I specified that PowerShell file to execute at specialise pass stage. Please check the below unattended script for PowerShell.
powershell.exe -executionpolicy bypass -noprofile -File "\\192.168.3.5\deploy\demo.ps1"
Everytime when I ran the setup using the unattended file it skips the PowerShell execution with the error. I have checked unattended log and found the below error code for the powershell execution.
Process returned with exit code 0xfffd0000
I have full control to the network shared path. Any idea on this?
One Year Later: I believe the error to be related to the "location" or Domain of the account being used. Depending on how you reference the account in your script, you may need to add the domain prefix or suffix to set the location context for the account referenced in the script DOM\username or username#domain.local

Run specific commands in PowerShell under different credentials?

I am trying to run a specific command line function in my PowerShell script. The catch is the command needs elevated permissions to be able to execute.
Here is a condensed example:
# PowerShell code...
query session /server:"SERVERNAME" #NEEDS ELEVATED PERMISSIONS
# More PowerShell code
The query command needs to be run under elevated permissions.
I have tried the following:
Invoke-Command -ScriptBlock {
query session /server:"SERVERNAME"
} -Credential get-credential
But this doesn't work because the -ComputerName parameter needs to be present when using a -Credential parameter. I want to be able to run this without using a remote server.
I know I can get around it by having the users start up PowerShell under their elevated account credentials, but I'd rather just prompt for credentials while the script runs and just run that single command under their administrator account credentials. Everything else the script does is fine under normal credentials.
There are some add-ins for PowerShell, but I actually found the simplest way was to:
Sysinternals in regular command line
With the PSexec process, you can pass IP address, usermame, and password
Fiddle with it to a point you're happy
Create a batch file to then run from PowerShell if that is the desired deploy to environment
When creating the method, have it consume parameters if you want the call out to be dynamic and consume different usernames/passwords/IP addresses to log into
If the exec will always run on "computerA" using "loginA" and "pwA" then there is obviously no need to parameterize
*Sysinternals cannot be used to outright hack a terminal. The user of a remote exec must first have the same Sysinternals tools installed to the system that is to accept remote executables, that tool must be opened once and given GUI-based approval to allow run on said system must be physically addressed.
Note: Any remote PSexec's using credentials will execute with the same level of permissions that the provided username/password is granted on that system.
Here is the link: (PsExec v2.2). Although I recommend going a level or two up and downloading the entire toolbox.

Unable to set the permissions of an app through PowerShell

I've been trying to set the permissions of a native application added through PowerShell with no success.
I'm following the instructions on this page:
https://msdn.microsoft.com/en-us/library/azure/dn790557.aspx
I can add the application through PowerShell (step #1), but when I try to set the permissions (step #2) I get several issues.
If I use the Microsoft Active Directory Module PowerShell, I am able to connect but when trying to select a subscription (with the correct values), the result is that the subscription does not exist. Out of curiosity, if I execute the Get-Subscription cmdlet I get an empty output. Even if I go ahead and issue the New-AzureRoleAssignment cmdlet, I get the Object reference not set to an instance of an object error.
If I use the Azure PowerShell, I am not able to issue the Connect-MsolService cmdlet since it says that the cmdlet is not found. Even though, if I go ahead and try the New-AzureRoleAssignment cmdlet, I get the same error as above.
Someone knows what am I doing wrong or if I'm missing a step?
Launch the Azure Powershell Console and use Import-Module <moduleName> for the module containing Connect-MsolService.
If you are using this combination of modules frequently, I would recommend to put that Import-Module command in the default profile for that console. I do not know how to do this, but google should.

How can I execute scripts in a code created powershell shell that has Host.Ui.prompt commands in it?

I have a Powershell Commandlet which prompts a user from a secure string based on a condition. Now I want to automate the testing of this commandlet for which I use a Powershell Remote Runspace to Invoke the commandlet. Currently it fails with this error.
Write-Host : A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows.
How can I automate this?
It sounds like you are running powershell via c#. You can't prompt the user for input from the powershell script. You either need to pre-provide the necessary info in the script, or prompt for the info from your application and then pass the info to the powershell script.
As ojk mentioned the easiest way to accomplish this would probably be to use a powershell function then pass the necessary parameters to it via the code.