0
votes

I'm trying to run a powershell script from rundeck(linux), If I run the script locally[Deletes some files from multiple terminal servers](Windows server) it is working as expected however if I call it from rundeck server(winrm configured) it seems that the script cant access the remote folders I'm trying to access.

I tried running the script using the same user but still shows different result.

Script bellow:

$userAD = "someuser"
$servers = Get-Content C:\TSList.csv
$Folder = "c$\Users\$userAD\"
$TSFolderShare = "\\sharepath"

Write-Output "#####Start of script#####"
Write-output `n
Write-output "Checking if $userAD user profile exist in Terminal servers..."

sleep -seconds 1

foreach ($server in $servers) {

Test-Path "\\$server\$Folder" -PathType Any
Get-ChildItem "\\$server\$Folder" 

    if (Test-Path "\\$server\$Folder" -PathType Any) {
        Write-output  "Resetting user profile in $server.."
                   Get-ChildItem "\\$server\$Folder" -Recurse -Force -ErrorAction SilentlyContinue  | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue 
        
        sleep -seconds 1
        Write-output "Done." 

        if( (Get-ChildItem "\\$server\$Folder" | Measure-Object).Count -eq 0)
             {
                Write-output "Done." 
             }
        
    }
    else
    {
        Write-output  "Resetting user profile in $server.."
        sleep -seconds 1
        Write-output  "User profile does not exist in $server."          
        #Write-output "\\$server\$Folder does not exist in $server!" -ForegroundColor Red
    }

}

EDIT: It seems my problem is when running my script from another script with RunAS.

Below I'm trying to access a folder from another server using ps script, but since I want to integrate this to Rundeck I need to call my ps script from my linux server using python. I did a test running the ps script directly and calling the test path script using another script with RunUs using the same user I used to run the script manually

  1. Scenario 1 Running PS script via separate PS script with RunAS(my_account)

    $username = "my_account" $password = "my_password" $secstr = New-Object -TypeName System.Security.SecureString $password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)} $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr

    Invoke-Command -FilePath "C:\testpath.ps1" -Credential $cred -Computer localhost

(C:\testpath.ps1) Content below:

Test-Path "\\server\c$\Users\myaccount\"

result:

Access is denied + CategoryInfo : PermissionDenied: (\server\c$\Users\myaccount:String) [Test-Path], UnauthorizedAccessException + FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.PowerShell.Commands.TestPathCommand + PSComputerName : localhost

False

  1. Scenario 2 Running C:\testpath.ps1 directly as my_account

Test-Path "\\server\c$\Users\myaccount\"

result: True

2
Hi, What message are you seeing when the script is executed from Rundeck? Can you share it? (please change or hide any sensitive information). Regards!MegaDrive68k
Can you share your job definition to check how the script is defined? Also your windows node definition (resources.xml) to take a look. Thanks! – MegaDrive68k 37 mins agoMegaDrive68k
Hello, Thanks for the response I updated the post, basically my original script will delete the content of a directory from another terminal server, if I run the script manually(via ISE or ./) it is working however if I call the script using another script with the path I'm trying to access is denied, I also defined to use the same user as what I used when running it manually. Thank youMarvene E

2 Answers

0
votes

You're facing a double-hop issue with Rundeck and Powershell, here the explanation. That's asked before, take a look a this, and here a good workaround. Also this to solve it.

1
votes

I used session configuration in powershell to solve the issue. This way allows you to tie a credential to a PowerShell session configuration and reuse this configuration for all future connections.

https://4sysops.com/archives/solve-the-powershell-multi-hop-problem-without-using-credssp/

Thanks a lot!