简体   繁体   中英

Ansible not running command or script in powershell elevated mode

I have tried 3 different task methods to get Ansible to execute a powershell command or a ps1 script with the command in an elevated Administrator shell. I am a DOMAIN Admin and the computer is domain joined. All other powershell module tasks prior to this one work with my domain admin SA account. The command in the problem task is Import-Module -Name DellBIOSProvider -Verbose -Force . When I test this locally under my DOMAIN admin account it fails at the operation of ..New-PSDrive.. could not be created, please run with Administrator privileges. So to get it working I have to run powershell as Admin (system32) and the Import-Module command completes the New-PSDrive operation without fail. But Ansible is not executing the command or ps1 script in elevated mode it seems even with me using become_method become and become_user for local-admin execution. I have searched for days on how to get this to work. I found one other person that describes my problem on github but the conversation was then moved to private Github issue and I found this info on stackoverflow Powershell script in elevated mode . None of these implementations work. The output of Get-PSDrive should show the DellSmBIOS PS drive after successful import so that subsequent tasks can use the new ps-drive for pwrsh configuration calls but the it is not there because Ansible is running it under my domain admin sa account.

Output when attempting to import module with my domain admin account in pwrsh:

PS C:\Users\arichardson.sa> Import-Module -Name DellBIOSProvider -Verbose -Force
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.psd1'.
VERBOSE: Populating RepositorySourceLocation property for module DellBIOSProvider.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.psm1'.
VERBOSE: Exporting function 'Set-Dell1stBootdevice'.
VERBOSE: Exporting function 'Get-DellBiosSettings'.
VERBOSE: Exporting function 'Clear-DellAdminPassword'.
VERBOSE: Exporting function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Importing function 'Clear-DellAdminPassword'.
VERBOSE: Importing function 'Get-DellBiosSettings'.
VERBOSE: Importing function 'Set-Dell1stBootdevice'.
VERBOSE: Importing function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.dll'.
VERBOSE: Starting PS provider DellSmbiosProv
VERBOSE: .NET Version: 4.0.30319.42000
VERBOSE: Performing InitializeDefaultDrives operation on the 'DellSmbiosProv'provider.
VERBOSE: Performing operation New-PSDrive for the provider DellSmbiosProv.
WARNING: Please execute this program with "Administrator" privileges.
Import-Module : PS drive DellSmbios could not be created.
At line:1 char:1
+ Import-Module -Name DellBIOSProvider -Verbose -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OpenError: (DellSmbios:PSDriveInfo) [Import-Module], InvalidOperationException
    + FullyQualifiedErrorId : DefaultDriveInitFailed,Microsoft.PowerShell.Commands.ImportModuleCommand
VERBOSE: Exporting function 'Clear-DellAdminPassword'.
VERBOSE: Exporting function 'Get-DellBiosSettings'.
VERBOSE: Exporting function 'Set-Dell1stBootdevice'.
VERBOSE: Exporting function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Importing function 'Clear-DellAdminPassword'.
VERBOSE: Importing function 'Get-DellBiosSettings'.
VERBOSE: Importing function 'Set-Dell1stBootdevice'.
VERBOSE: Importing function 'Set-DellAutoOnForSelectDays'.
PS C:\Users\arichardson.sa>

Output when executing from Elevated shell:

PS C:\Windows\system32> Import-Module -Name DellBIOSProvider -Verbose -Force
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.psd1'.
VERBOSE: Populating RepositorySourceLocation property for module DellBIOSProvider.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.psm1'.
VERBOSE: Exporting function 'Set-Dell1stBootdevice'.
VERBOSE: Exporting function 'Get-DellBiosSettings'.
VERBOSE: Exporting function 'Clear-DellAdminPassword'.
VERBOSE: Exporting function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Importing function 'Clear-DellAdminPassword'.
VERBOSE: Importing function 'Get-DellBiosSettings'.
VERBOSE: Importing function 'Set-Dell1stBootdevice'.
VERBOSE: Importing function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Loading module from path 'C:\Program Files\WindowsPowerShell\Modules\DellBIOSProvider\2.6.0\DellBIOSProvider.dll'.
VERBOSE: Starting PS provider DellSmbiosProv
VERBOSE: .NET Version: 4.0.30319.42000
VERBOSE: Performing InitializeDefaultDrives operation on the 'DellSmbiosProv'provider.
VERBOSE: Performing operation New-PSDrive for the provider DellSmbiosProv.
VERBOSE: Category 'Passwords' does not contain any supported attributes.
VERBOSE: Category 'BIOSSetupAdvancedMode' does not contain any supported attributes.
VERBOSE: Category 'StealthModeControl' does not contain any supported attributes.
VERBOSE: Category 'AdvancedConfigurations' does not contain any supported attributes.
VERBOSE: Category 'ThermalConfiguration' does not contain any supported attributes.
VERBOSE: To get more help about the Dell Command PowerShell provider, type Get-Help DellBIOSProvider.
VERBOSE: Exporting function 'Clear-DellAdminPassword'.
VERBOSE: Exporting function 'Get-DellBiosSettings'.
VERBOSE: Exporting function 'Set-Dell1stBootdevice'.
VERBOSE: Exporting function 'Set-DellAutoOnForSelectDays'.
VERBOSE: Importing function 'Clear-DellAdminPassword'.
VERBOSE: Importing function 'Get-DellBiosSettings'.
VERBOSE: Importing function 'Set-Dell1stBootdevice'.
VERBOSE: Importing function 'Set-DellAutoOnForSelectDays'.
PS C:\Windows\system32>

Output showing my user account is an admin:

C:\Users\arichardson.sa>net user arichardson.sa /DOMAIN
The request will be processed at a domain controller for domain xxxx.xxxx.xxxx

User name                    arichardson.sa
Full Name
Comment
User's comment
Country/region code          000 (System Default)
Account active               Yes
Account expires              Never

Password last set            3/20/2022 10:00:43 PM
Password expires             Never
Password changeable          3/20/2022 10:00:43 PM
Password required            Yes
User may change password     Yes

Workstations allowed         All
Logon script
User profile
Home directory
Last logon                   9/1/2022 11:35:56 AM

Logon hours allowed          All

Local Group Memberships
Global Group memberships     *RADIUS-NAC           *Enterprise Admins
                             *Domain Users         *Domain Admins
The command completed successfully.


C:\Users\arichardson.sa>net localgroup Administrators
Alias name     Administrators
Comment        Administrators have complete and unrestricted access to the computer/domain

Members

-------------------------------------------------------------------------------
Administrator
AMERICA\Domain Admins
local-admin
The command completed successfully.


C:\Users\arichardson.sa>

Turns out, Ansible creates a new PS session with each task so I ended up just having a single Ansible task that executes one powershell script which contains all the cmdlets within and it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM