Run scheduled task as admin on multiple Azure VMs - windows

I am running an Azure Windows VM which runs a batch file when it starts (its started from Task Scheduler).
The batch file cannot be started as SYSTEM and needs to be run as a user, so I use the VM administrator. In Task Scheduler this user shows as \. This all works fine.
My question is though - I want to be able to run many of these VMs by making a VHD of the existing VM then creating more VMs (perhaps in a ScaleSet) from that VHD.
The script needs to be run when the VM is first started AND if it is ever rebooted.
I assume though that when I do this, my start-up script will break as the MACHINE_NAME will be different for each VM.
What is a good solution for this problem?

if it started from task schedular and is a batch job, you can move it to webjobs too and schedule there. and then create as many webjobs to want of the same batch file. no need to create and maintain VMs

Related

Unable to move files after batch migration

I migrated a batch job from Windows Server 2012 to a Windows Server 2019 VM. It worked on the legacy server but not working on the newly setup virtual machine for some reason...I'm trying to automate the move of all files from a shared network drive locally to the server. The service account that's used to trigger this job via Task Scheduler has full permissions to that location.
move /y "\\example.com\europe\Department\EN\Shared Folder\*" "C:\Temp\QUANTITIES"
Any ideas why? The batch job simply gets stuck executing even when I run it with my own a-account.

Can we logon as a full windows profile using ansible

I know that Ansible uses winRM to connect to window hosts, which means it does not fully load a windows profile when connecting to a host, but rather can execute bat/powershell commands remotely.
But is there a method where I can logon as a complete Windows profile using Ansible.
My requirement is that every Monday morning I want an automated job (Preferrabley Jenkins -> Ansible) that will logon to my Windows Server machines (around 10 machines). Currently we have a support team that does this every Monday morning (because the machines are rebooted every weekend), and sometimes 1 or 2 machines are missed out, and I would like to automate this because we have Scheduled Tasks that are supposed to run early in the morning, that sometimes misses to run because support forgets to login to a particular machine.
There are 2 connection plugins for Windows in Ansible PSRP and WinRM. Whatever tasks your support team is performing manually can be automated with Ansible Windows Modules or Powershell commands. Though they are executed remotely, actions are performed on the target servers only.

AWS EC2 Windows schedule job fails while offline

I created a Windows instance on AWS EC2 with T2 Micro, where I set up a schedule job that calls a .bat file at 6 every morning.
The .bat job invokes a few other scripts that mainly carry out some Internet-related transactions, including launching browsers, call services and perform I/O operations.
The issue is in most of the time the .bat file is not able to complete its job (Sometimes it does, but pretty rare). Later on, if I connect to the EC2 instance via remote desktop with the same username and manually call that .bat file, everything works fine.
Anyone has ever experienced this kind of problem and what is the resolution?
Thank you.
It's a common problem with automation. Some of the things you're doing such as "launching browsers" may not work for unattended execution.
A good example is Selenium, if you want to run web tests unattended then you need the headless version of Selenium, here's how to set it up for Headless:
Running Selenium with Headless Chrome Webdriver

Starting a Windows Process or Service with a GUI with PowerShell DSC

I have a Unity3D application (a simple .exe) that I'm trying to automate with PowerShell DSC. However, it seems that Unity3D cannot acquire a GPU when started as a Service or background Process, which breaks my application. When started manually via double-clicking the .exe, things work fine.
Is there a way to force DSC to show a GUI when starting a Service or Process?
Thanks!
DSC doesn't currently support interactive applications during the configuration. I can think of two solutions:
Ensure that the user is logging on
The user logs on already for whatever reason
Setup the user to auto-logon during the config (and be sure to set DSC to reboot.)
Auto Logon setup KB
Ensure the app runs when the user logs on:
Setup a scheduled task to start the app when the user logs on
Schedule Task cmdlet reference
atLogon trigger reference
The app is set in the registry to run when the user logs on

Run a script on Windows startup without a user logged on

This is a Windows 2003 (or later) machine that gets switched on every morning, but no one logs on until some hours later.
I want to use the time in between to run a backup script c:\script\backup.cmd
How can I start this unattended after the machine has come up?
I tried 2 registry keys, but this resulted in the script being run after a user logs on (which is too late):
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunServices
In the end I used Windows TaskScheduler, who has such an option, but I was wondering if there is another possibility?
Short answer: GPEDIT.MSC (Start, Run, GPEdit.msc)
Windows 2000 and above [1] offer a computer Startup Scripts collection in the policy editor:
Computer Settings -> Windows Settings -> Scripts (Startup/Shutdown)
There's an equivalent logon script area (i.e. after computer startup, when a user logs on) in the User configuration bit.
Longer:
GPEDIT.MSC is the Group Policy editing console, and runs against the local computer's Local Group Policy store when it's used directly, so it's useful for setting local-only parameters. When using Active Directory, a similar interface is used to edit AD Forest-hosted group policy objects (GPOs), so the same settings are available across a bunch of machines.
The computer startup scripts run in the computer context, i.e. as LocalSystem, as you noted, so they often can't access network drives which require a certain user or group membership to work. When domain-member computers access network resources, they generally (with exceptions) authenticate using their MACHINENAME$ account - which lets you set Share/NTFS permissions to allow a computer to access those resources.
A startup script is a quick and easy way of getting a process running when the machine boots.
The computer startup process will be affected by the time it takes to run the program, though, so you might want to ensure you call it with the START command from a batch file, or specifying not to wait for the executable to complete in whatever script language you use. (the key point there is: run the script asynchronously unless it's critical, or doesn't need to be run asynchronously cos it will always take no time at all. Long boots = unhappy users).
Using a Win32 Service is an alternative option - you can use the SRVANY utility from the Resource Kit to "service-ify" pretty much any executable. VS.Net 2002 and later also let you build a managed service directly.
And Task Scheduler gets much more capable as of Vista/2008, able to run scripts at startup, on idle, and/or when Event Logs are generated or certain other conditions are met: it's pretty cool! Scheduled Tasks has the possible advantage of being able to specify the user account under which the task runs, if that's important to you.
Caveat Scriptor:
http://support.microsoft.com/kb/256320
Run Startup Scripts Asynchronously:
http://msdn.microsoft.com/en-us/library/ms811602.aspx
Vista Task Scheduler (what's new):
http://technet.microsoft.com/en-us/appcompat/aa906020.aspx
[1] Windows XP, 2003, Vista/2008, Windows 7/2008R2, Windows 8/2012, Windows 8.1/2012R2, Windows 10/Windows Server 2016. Everything. But NT4 didn't!
You have already outlined a good solution:
Setup a scheduled task to run at Start Up and allow the job to run when the user isn't logged on.
You can run a script at system startup using group policy gpedit.msc
The way you aleady do this seems fine to me; however if you want an alternative approach then services get started when the machine boots so you could write a service that detects if it's a new day (to allow for reboots) and if it is then run your backup.
If I was doing this as a service I'd use TCL because I know it and like it and it has an extension twapi that allows you to run a script as a service. Other scripting languages may well have similar facilities.
There is, if you're using Active Directory. If you can isolate the computer to its own OU or use WMI filtering, you could assign a GPO which has a startup script for the computer. This would ensure that even if someone went in via safe mode and disabled the Task Scheduler, upon startup and connection to the domain, the script would run.

Resources