I've recently deployed a POC automation platform involving ServiceNow and
AWX. I have a use case to automate access to windows servers.
The ServiceNow form will be supplying me 10 windows server names at a time.
I did write this Powershell script to handle multiple computer objects and
it works as expected when I run it directly on Windows Server. It does
prompt me for multiple computer names and does the work as expected. But
when I put the same script on AWX it does not recognize the $Computer
variable which is a sort of dynamic variable. Here is the script.
function Set-AccessOnRemoteMachine {
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[string[]]$ComputerName,
[Parameter(Mandatory = $True)]
[string]$User,
[Parameter(Mandatory = $True)]
[String]$Group,
[Parameter(Mandatory = $True)]
[String]$AccessType
)
if (($AccessType -eq 'Grant') -and ($Group -eq 'Remote Desktop Users'
)) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -
ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param(
$Group, $User) net localgroup $Group $User /add } -ArgumentList $Group,
$User
Write-Output "Successfully added the user $User
in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #if
elseif (($AccessType -eq 'Grant') -and ($Group -eq 'Administrators')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -
ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param(
$Group, $User) net localgroup $Group $User /add } -ArgumentList $Group,
$User
Write-Output "Successfully added the user $User
in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #elser"
} #foreach
} #elseif
elseif (($AccessType -eq 'Revoke') -and ($Group -eq
'Remote Desktop Users')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -
ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param(
$Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group,
$User
Write-Output "Successfully added the user $User
in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #elseif
elseif (($AccessType -eq 'Revoke') -and $Group -eq ('Administrators')) {
foreach ($Computer in $ComputerName) {
if ((Invoke-Command -ComputerName $Computer -
ScriptBlock { HOSTNAME }) -eq "$Computer" ) {
Invoke-Command -ComputerName $Computer -ScriptBlock { param(
$Group, $User) net localgroup $Group $User /delete } -ArgumentList $Group,
$User
Write-Output "Successfully added the user $User
in the Group $Group on the computer $Computer"
} #if
else {
Write-Output "$Computer is not reachble"
} #else
} #foreach
} #elseif
} #Function
Set-AccessOnRemoteMachine -ComputerName $Computer -Group $Group -User $User
-AccessType $AccessType
Here is the error that I get in AWX.
Invoke-Command : One or more computer names are not valid. If you are
trying to pass a URI, use the -ConnectionUri parameter, or pass URI objects
instead of strings. At C:\temp\Set-AccessOnRemoteMachine.ps1:28 char:9 +
Invoke-Command -ComputerName $Computer -ScriptBlock { param($ ... +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +
CategoryInfo : InvalidArgument: (System.String[]:String[])
[Invoke-Command], ArgumentException + FullyQualifiedErrorId :
PSSessionInvalidComputerName,Microsoft.PowerShell.Commands.InvokeCommandCommand
There are future use cases in the pipeline that will require foreach
PowerShell loop. Hence knowing and making the PowerShell foreach loop work
is very crucial for me.
Please help understand how to handle PowerShell foreach loop in AWX and
Ansible Core.
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/8eef63d7-98eb-4b87-b4b3-9d663f4eaf3c%40googlegroups.com.