Unexpected behaviour with PowerShell in Rundeck

81 views
Skip to first unread message

mezam

unread,
Feb 25, 2023, 1:37:08 PM2/25/23
to rundeck-discuss
Hello, I have the following script, which works well in VS Code:


foreach($entry in $beingDelJsonFile){
    # point 4b
    if($entry.serverName -ne "."){
        try {
            $deletingDate = [datetime]$entry.deletingDate
            $daysDifference = (Get-Date) - $deletingDate
            if ($daysDifference.Days -gt 0) {
                $vm = Get-VM "TBD_$($entry.ServerName.Split(".")[0])*"
                If($fase){
                    #point 4.b.I
                    Remove-VM -VM $vm -DeletePermanently -Confirm:$false
                }
                if(($vm).PowerState -eq "PoweredOn"){
                    Write-Host "VM is powered on. Ensure it is powered off if you intend to proceed!"
                }
                if(!$vm){
                    Write-Host "VM $($entry.ServerName) not found. Possibly already deleted."
                }
                if($entry.ServerName.Split(".")[1] -like $gdomain.Split(".")[0]){
                   
                    $dc = (Get-ADDomainController -domain $gdomain -discover).Name +"."+ $gdomain
                    $zoneName = $gdomain
                   
                }
                # point 4.b.II
                $computer = $null
                Try {$computer = Get-ADComputer -Identity $entry.ServerName.Split(".")[0] -server $dc -ErrorAction Stop}
                Catch {
                    if($null -eq $computer){
                        Write-Output "Cannot find an object with identity: $($entry.ServerName.Split(".")[0]) under: $($entry.ServerName.Split(".")[1])"}
                    }
                if($computer){
                    Remove-ADObject -Identity $computer.ObjectGUID -Server $dc -Credential $Creds -Confirm:$false
                }
                Invoke-Command -ComputerName $dc -Credential $Creds -ScriptBlock {
                    #point 4.b.III
                    $dnsAHost = $null
                    Try {$dnsAHost = Get-DnsServerResourceRecord -ZoneName $args[1] -RRType "A" -Name $args[0] -ErrorAction Stop}
                    Catch {Write-Output "Cannot find DNS entry: $($args[0]) under: $($args[1])"}
                    if($dnsAHost){
                        $dnsAHost | Remove-DnsServerResourceRecord -ZoneName $args[1] -Confirm:$false -Force
                    }
                } -ArgumentList $entry.ServerName.Split(".")[0], $zoneName
                $deleted += $entry
                $deleted.Count
            }
        } catch {
            Write-Host "Error parsing date string: $($entry.deletingDate)"
            Write-Host $_.Exception.Message
        }
    }
}

it outputs:

VM server1 not found. Possibly already deleted.
Cannot find an object with identity:  server1   under: domain
Cannot find DNS entry:  server1   under:  dnszone
1
VM  server2  not found. Possibly already deleted.
Cannot find an object with identity:  server2  under:  domain
Cannot find DNS entry:  server2 under:  dnszone
2


which is correct. When run in rundeck the output is:

VM server1  not found. Possibly already deleted.
Error parsing date string: 2023-02-21
VM server2 not found. Possibly already deleted.
Error parsing date string: 2023-02-21


It skipped the entire block from point 4.b.II until $deleted.Count. I've rebooted teh RD server but nothing. I have various scripts working, it is the first time I experience this. I'm worried. RD v.4.8 on Windows 2019 and MariaDB SQL. WAR setup.



mezam

unread,
Feb 26, 2023, 4:07:00 AM2/26/23
to rundeck-discuss
forgot to say that the VS Code test is run on the same RD server, so PS is the same

rac...@rundeck.com

unread,
Feb 27, 2023, 6:45:06 AM2/27/23
to rundeck-discuss
Hi Mezam,

Could you share the job definition to take a look? hide or change any sensitive information.

Regards!

mezam

unread,
Feb 27, 2023, 8:23:25 AM2/27/23
to rundeck-discuss
Hi Reiner, thanks, here it is attached
Virtual_Servers_Decommission_2nd_Phase.xml

rac...@rundeck.com

unread,
Feb 27, 2023, 8:38:51 AM2/27/23
to rundeck-discuss
Hi Mezam,

Are you running that script on PS 7? Also, take a look at this way to pass arguments on PS Scripts.

Greetings.

mezam

unread,
Feb 27, 2023, 8:52:32 AM2/27/23
to rundeck-discuss
no PS7, not even installed, plus in this case the same code works when executed in VS Code on the same Windows Rundeck server. I suppose RD is using the same powershell that VS Code uses. what is the difference? The powershell plugins used?

rac...@rundeck.com

unread,
Feb 27, 2023, 9:55:14 AM2/27/23
to rundeck-discuss
Rundeck uses the powershell.exe interpreter defined in the server operating system (considering that you're running your job against the windows localhost). Some issues are solved by upgrading PS to the latest version, a good test could be to test that directly on the Powershell terminal, check the version and test it on Rundeck using the latest PS version on a test environment. Also, consider this.

Greetings.

mezam

unread,
Feb 28, 2023, 4:09:18 AM2/28/23
to rundeck-discuss
once again the issue self fixed. Without intervention. This has nothing to do with PS versions and how parameters are set and arguments passed. I just remembered that this job was once set and working. Then a month later modified. It's the second time that after a job is defined and working, IF it is later amended it does not "detect/run/I don't know how to define this behaviour" the modification applied. Then after a number of hours, it just simply starts working. 
Reply all
Reply to author
Forward
0 new messages