How can you create a script to copy files with Automox?
Hello, I am using Automox agent to copy a test file. The file is called "hello_world.txt" and the location where I can upload it is "c:". Additionally, I have added "Exit 1" to get a response, but I still don't understand how Exit 1 works. My script checks if the file already exists before copying it. If it does not exist, it makes a copy and responds "New file created".
Someone help me, because the script don't work?
$filename = 'hello_world.txt' $filepath = 'C:\'
If (Test-Path -Path $filepath$filename -PathType Leaf) { Copy-Item .\$filename -Destination $filepath Write-Output "File copied and replaced existing" } else { Copy-Item .\$filename -Destination $filepath Write-Output "New file created" }
Page 1 / 1
Hi @SIFAQES,
Here are some quick pointers regarding Worklet Evaluation vs Remediation code:
Automox Evaluation Code is ran every time a device scans. During the scan, the Evaluation Code will determine if a device is compliant or not based on the logic you specify. An Exit 0 status within the Evaluation Code deems the device is compliant and the worklet will not execute.
Any non-zero value (such as an Exit 1) will deem the device as non-compliant and then schedule the Remediation Code to run based on your Worklet’s schedule.
Evaluation code output is not logged within Automox. Only the results of the Remediation Code run will be logged.
Regarding your script, you’ll first want to split out your Evaluation Code to determine what should happen if the file is found or not found. In our case, we’ll see if the file exists and if so perform an Exit 0. This will deem that the device is compliant and therefore end the script run. If the file does not exist, we’ll perform an Exit 1, which indicates the device is not-compliant and will trigger the Remediation Code based on your policy schedule:
Evaluation Code
#Insert the file path and file name that you want to check for. $filename = 'payload.txt' $filepath = 'C:\'
if (Test-Path -Path $filepath\$filename) { Write-Output "File already exists. Now exiting." Exit 0 } else { Write-Output "File does not exist." Write-Output "Running remediation code to copy the file from the worklet payload." Exit 1 }
Now in our Remediation Code, we’ll want something to check and see if the $filepath exists first, and if so, simply copy the file from the Worklet Payload. If the $filepath does not already exist, we’ll want to create the directory and then attempt to copy the file:
Remediation Code
#Ensure that the file is uploaded to the Worklet's payload. #Specify the target file name and destination file path. $filename = 'payload.txt' $filepath = 'C:\'
if (Test-Path -Path $filepath) { Write-Output "Filepath exists." Write-Output "Attempting to copy the file."
try { #Copy the file from the worklet payload to the filepath. Copy-Item "$PWD\$filename" -Destination "$filepath" -Force Write-Output "File $filename copied successfully!" Write-Output "Now exiting." Exit 0 }
catch { Write-Output "The file failed to copy." Write-Output "Now exiting." Exit 0 } }
else { Write-Output "Filepath does not exist." Write-Output "Creating the directory."
try { #Create the missing directory. New-Item -ItemType Directory -Path $filepath -Force Write-Output "Directory created successfully." Write-Output "Attempting to copy the file."
try { #Copy the file from the worklet payload to the new directory. Copy-Item "$PWD\$filename" -Destination "$filepath" -Force Write-Output "File $filename copied successfully!" Write-Output "Now exiting." Exit 0 }
catch { Write-Output "The file failed to copy." Write-Output "Now exiting." Exit 0 } }
catch { Write-Output "Unable to create the directory." Write-Output "Now exiting." Exit 0 } }
The cool part about Worklets is that you can use the Write-Output cmdlet to append your comments to the Automox Activity Log. In this way, we can track the progress of the script run and even log specific errors:
Lastly, here is a pretty cool Community Worklet for copying a file to a folder that exists under the User run space: