Sometimes it is useful to be able to run various configuration scripts on your IoT device at start up. This article will outline a way to run a PowerShell script on startup of the Windows IoT Core operating system.
The first thing you will need to do is build the script you would like to run. In this example, we’ll keep it simple. We’ll create a log file at startup that will log the current date. Create a new file, name it StartupScript.ps1 and add the following lines of PowerShell code:
# startup script
$logFile = 'C:\startupLog.txt'
get-date >> $logFile
Next we will create a simple batch (*.bat) file that will execute the PowerShell script (or multiple scripts whatever your case may be). Create a new file, name it “Startup.bat” and populate it with the following code:
powershell -command "C:\StartupScript.ps1"
The next thing we will do is move the script and batch file to the IoT device. The device will need to be booted and on your network. On your PC, open file explorer, and in the address bar access your device by entering:
ftp://<your_iot_device_name_or_IP>
You will be prompted for credentials, use the Administrator credentials that you use on your IoT device. Once authenticated, navigate to your folder of choice (in my case I want to execute my script at the root as specified in my batch file) then drag and drop StartupScript.ps1 and Startup.bat to the FTP window in order to copy them over to your device.
Next we will need to set the PowerShell execution policy on the Raspberry Pi to allow for remote signed scripts. On your PC, run PowerShell as an Administrator (Press the Windows button, and start typing PowerShell, right click on the icon and select “Run as Administrator”).
Follow the following tutorial to learn how to establish a PowerShell session with your IoT Core device. Now that you are connected to your device, we need to set the execution policy. From your PowerShell prompt, execute the following command:
Set-ExecutionPolicy RemoteSigned
You can now exit out of your PowerShell session. Next we will use Putty to access the command prompt of the IoT Core Device. Follow this tutorial to establish a Putty connection with your IoT Core Device. Now that you are at the command prompt, navigate to the “C:\Windows\System32” folder, alternatively you can also add to this folder permanently to your path by executing the following command (Optional):
setx PATH "%PATH%;C:\Windows\System32"
Add a startup scheduled task by executing the following command:
schtasks /create /tn "Startup PowerShell" /tr c:\Startup.bat /sc onstart /ru SYSTEM
A success message should be displayed upon successful creation of the scheduled task.
From your device, or the Putty command prompt, reboot your device.
shutdown /r /t 0
When your device comes back online, open up your FTP window to the device once more. You will see the StartupLog.txt file. Copy the file over to your PC and inspect the contents.
Here are a couple more useful scheduled task commands.
#1 Deleting a scheduled task
schtasks /Delete /TN "Startup PowerShell"
#2 Running a scheduled task
schtasks /Run /tn "Startup PowerShell"
This article outlines a practical solution to running PowerShell scripts when your Windows IoT Core device boots up – a great tip to add to your toolbox!
Comments