From 44cfdc89d9d39aa24efa8b11a2e18ac0c322ea00 Mon Sep 17 00:00:00 2001 From: silversword411 Date: Fri, 24 Nov 2023 12:52:53 -0500 Subject: [PATCH] Cleaning up Runasuser template --- scripts/Win_RunAsUser_Example.ps1 | 33 +++++++++++++------------------ 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/scripts/Win_RunAsUser_Example.ps1 b/scripts/Win_RunAsUser_Example.ps1 index 9c2fcf27..5bbc6dd4 100644 --- a/scripts/Win_RunAsUser_Example.ps1 +++ b/scripts/Win_RunAsUser_Example.ps1 @@ -20,43 +20,38 @@ else { } # Make sure Tactical RMM temp script folder exists -If (!(test-path "c:\ProgramData\TacticalRMM\temp\")) { +If (!(Test-Path "c:\ProgramData\TacticalRMM\temp\")) { Write-Output "Creating c:\ProgramData\TacticalRMM\temp Folder" - New-Item "c:\ProgramData\TacticalRMM\temp" -itemType Directory + New-Item "c:\ProgramData\TacticalRMM\temp" -ItemType Directory } Write-Output "Hello from Systemland" -Invoke-AsCurrentUser -scriptblock { - +Invoke-AsCurrentUser -ScriptBlock { # Put all Userland code here - Write-Output "Hello from Userland" | Out-File -append -FilePath c:\ProgramData\TacticalRMM\temp\raulog.txt + $raulogPath = "c:\ProgramData\TacticalRMM\temp\raulog.txt" + $exit1Path = "c:\ProgramData\TacticalRMM\temp\exit1.txt" + + Write-Output "Hello from Userland" | Out-File -append -FilePath $raulogPath If (test-path "c:\temp\") { - Write-Output "Test for c:\temp\ folder passed which is Exit 0" | Out-File -append -FilePath c:\ProgramData\TacticalRMM\temp\raulog.txt + Write-Output "Test for c:\temp\ folder passed which is Exit 0" | Out-File -append -FilePath $raulogPath } else { - Write-Output "Test for c:\temp\ folder failed which is Exit 1" | Out-File -append -FilePath c:\ProgramData\TacticalRMM\temp\raulog.txt + Write-Output "Test for c:\temp\ folder failed which is Exit 1" | Out-File -append -FilePath $raulogPath # Writing exit1.txt for Userland Exit 1 passing to Systemland for returning to Tactical - Write-Output "Exit 1" | Out-File -append -FilePath c:\ProgramData\TacticalRMM\temp\exit1.txt + Write-Output "Exit 1" | Out-File -append -FilePath $exit1Path } - # End of all Userland code - } # Get userland return info for Tactical Script History -$exitdata = Get-Content -Path "c:\ProgramData\TacticalRMM\temp\raulog.txt" +$exitdata = Get-Content -Path "c:\ProgramData\TacticalRMM\temp\raulog.txt" -ErrorAction SilentlyContinue Write-Output $exitdata # Cleanup raulog.txt File -Remove-Item -path "c:\ProgramData\TacticalRMM\temp\raulog.txt" +Remove-Item -Path "c:\ProgramData\TacticalRMM\temp\raulog.txt" -ErrorAction SilentlyContinue # Checking for Userland Exit 1 -If (!(Test-Path -Path "c:\ProgramData\TacticalRMM\temp\exit1.txt" -PathType Leaf)) { - # No Exit 1 From Userland - Exit 0 -} -Else { +If (Test-Path -Path "c:\ProgramData\TacticalRMM\temp\exit1.txt" -PathType Leaf) { Write-Output 'Return Exit 1 to Tactical from Userland' - Remove-Item -path "c:\ProgramData\TacticalRMM\temp\exit1.txt" + Remove-Item -Path "c:\ProgramData\TacticalRMM\temp\exit1.txt" -ErrorAction SilentlyContinue Exit 1 } -