CloudFormation - Windows EC2 UserData Powershell

Any suggestions to have the CloudFormation UserData to execute Powershell commands in an elevated PS session? I’m trying to execute "New-Partition -DiskNumber 1 -AssignDriveLetter -UseMaximumSize " but getting an error like my permissions are insufficient for that cmdlet.

I confirmed that I can rdp into the instance, open an elevated PS session and run that command without any problem.

Thanks!


Related discussions:

In my experience, it can take 5+ minutes for the local admin credential to be created. Therefore, do you think my userdata PS script needs a wait command (~5mins) to ensure the admin account is available before the rest of the script processes?

You need to tell the userdata to execute as powershell otherwise it executes in CMD I think, did you make sure to do a script_code_here definition?

Yep, sure did. I think my problem is that the userdata PS script is executing before the local admin account has been fully provisioned.
Good thought! Thanks for chiming in.

Thats interesting… i don’t see how that would be a thing really considering the dependency userdata has on users being available. I mostly use Linux but have dabbled in Windows and haven’t encountered that issue before, are there errors in-the logs?

Windows isn’t as friendly for userdata and many other things. I don’t fully understand the execution environment for userdata right at instance creation but I’m working on it.

That is true, let me know if you need more help, more than happy to pitch in where possible.

Not sure this will help but I’m not using UserData directly from my cloudformation anymore. Now I use cloudformation init even for very simple scripts. I find it to be more consitent and simplier to reuse.

Init is a good idea, I’ll see what I can do with it for this task. Thanks!