Skip to content

Commit

Permalink
Merge pull request #196 from silversword411/main
Browse files Browse the repository at this point in the history
Cleaning up Runasuser template
  • Loading branch information
silversword411 authored Nov 24, 2023
2 parents 3a52a1a + 44cfdc8 commit 5a19e2f
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions scripts/Win_RunAsUser_Example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 5a19e2f

Please sign in to comment.