mback Posted November 7, 2022 Posted November 7, 2022 Is it possible to get currently signed in user in a batch script? We have power-shell execution disabled besides for calling bat scripts. Currently I am working on a project that I require getting the current user logged in ex: pirateesports01, pirateesport02, ... I tried using %username% during testing and it worked fine but then when I pushed it through filewave, it was a no go. I looked at the log and it was using Administrator, which I should have guess but since I was logged in as esports01 i hopped it would use that. I also tried using %userprofile%, still no luck. %userprofile% returns C:\windows\system32\config\systemprofile as the user profile so that wont work for my script either. install_script.bat
mback Posted November 7, 2022 Author Posted November 7, 2022 I found this script that seems to work for finding the username I want but I am having trouble using the environment variables. https://fwkb.atlassian.net/wiki/spaces/KB/pages/4331036/Script+Best+Practices has an example of batch file for how to pass the information into the script but how to I use it in the batch script? I can't seem to have any luck in finding an example for batch for the environment variables. getUser.bat
mback Posted November 21, 2022 Author Posted November 21, 2022 (edited) This post was recognized by Josh Levitsky! mback was awarded the badge 'Great Content' and 20 points. After a lot of trouble I figured it out! Attached is a Zip with the files I used. Pre Steps and Info: Create a Task in Task Scheduler that works and does what you want. In this case I created two. First one runs Everyday at 7:30 AM or the earliest it can if it misses 7:30, and it will also run on Reboot. This task calls a script that resets IPV4 "Wi-Fi" interface back to DHCP for IP and DNS. The second task I made is for when an account matching a certain name (EX: pirateesports01) logs in then run a script. The script it calls changes the IP to match and IP that have a filter exception so it does not get blocked. Before installing the script I would restart the machine so its fresh. This also makes sure the only user on is the user you want. I DID NOT TEST THIS WITH OTHER USERS LOGGED ON!! This was just a quick and dirty solution to a problem without me needing to enable the filter bypass for X IP's every time esports happens. In filewave for the script in the ENV variables I made one that is passwd that is the Administrator password. File Breakdown: Account Login Folder This holds all the exported account login task (in the zip it's 5 accounts). The login task calls the LoginScript that changes the IP based on Machine name. Esports Reset DHCP This is the task to call the reset IP to DHCP. Info above. install_script.bat I tried to make good comments in the script. It installs the account logon task based on what user is logged in (or does nothing is the username doesn't match) and installs reset DHCP task. Login Script This changes the IP based on what the machine name is. I did this second check just to make sure kids are not signing into another machine. Reset IP DHCP This resets the WiFi back to DHCP and removes manual DNS. Uninstall Script This removes the Login task and the Reset DHCP task. It also sets the IP back to DHCP just to be safe. P.S.: We have the Esports account locked on our Active Directory for only able to be logged in after school. Might need to add a script to shutdown the machine at 11:50 PM incase a user forget to logoff and don't want the IP exception to still be applied, just to be safe. This is just quick and dirty but I tried to put a few checks in place to prevent kids from messing it up. I also have no reason why I did X this way and Y that way other than I found an example online and worked, or I coded Y first then went back and did X and didn't want to change Y so I make two Vars that store the exact same info. EsportsLogin Example.zip Edited November 21, 2022 by mback Giving more info on why the script is the way it is. 1
Moderators Josh Levitsky Posted November 21, 2022 Moderators Posted November 21, 2022 @mback thanks for sharing this. With 14.9.1 and the Alliance eMeeting I didn't get to contribute to your answer but thanks for sharing what you worked out here. 1
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now