Scheduling in azure runbook - powershell

I would like to schedule stop/start Azure virtual machines with an Azure automation runbook.
This is what I've done so far. Created new automation runbook from gallery in Azure management portal. Chose 'Stop Windows Azure Virtual Machines on a Schedule' from 'VM Lifecycle Management' (it's this script). Runbook created, went to Author page of this runbook and pressed test. Provided the necessary parameters. Then several errors appeared on the output, starting with this:
1/21/2015 9:15:26 AM, Error: New-ScheduledTaskTrigger : The term 'New-ScheduledTaskTrigger' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At Stop-AzureVMsOnSchedule:41 char:41
Why this cmdlet cannot be found? I tried to upload the ScheduledTasks module in assets menu from my machine (c:\Windows\SysWOW64\WindowsPowerShell\v1.0\Modules\ScheduledTasks) but it complained about that the format is invalid.
Could you give me a hint why this script is not working or do you know an alternative what to use for scheduling tasks in an automation runbook?

"Stop Windows Azure Virtual Machines on a Schedule" is an Azure PowerShell script not designed to work as a runbook in Azure Automation. You can see that when you attempt to import it from gallery:
I recommend instead you import the runbook for stopping VMs created by jeffbow. By hooking this runbook up to an Azure Automation schedule asset you will be able to schedule shutting down your Azure VMs:

This script needs to be edited to work in Azure Automation.
New-ScheduledTaskTrigger does not work in Azure Automation environment.
To schedule Automation runbook, use New-AzureAutomationSchedule
Details at https://msdn.microsoft.com/en-us/library/dn690271.aspx

Related

Azure devops pipelines running on Self hosted agent - MSAL module

I am using azure devops to perform some tasks on Azure AD. I have prepared powershell script for authenticate to MS graph. I am using Azure devops pipelines, script files are stored on azure devops repo. I am using self hosted agent, its Windows Server 2019, all required modules, MSAL, Graph are installed. I am testing my script locally from OS and it works fine. When I am running azure devop pipeline am facing following error Get-MsalToken : The term 'Get-MsalToken' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try". I am using Powershell task in my pipeline.
I installed Powershell Core on my self hosted agent. In Azure devops pipelines, Powershell task options, I selected option Advanced/Use Powershell Core. I tested my pipeline, all works as expected.

Azure Devops pipelines Deploying a scheduled task using PowerShell script

I have an Azure DevOps pipeline with a PowerShell task, which when triggered, successfully creates and configures a Windows Scheduled Task on my Windows Server.
The configuration sets the task to run using an AD Service account, 'Run whether the user is logged on or not' & 'Run with the highest privileges'.
When I try to run this task however, I get the message "The user account does not have permission to run this task."
If I manually create a scheduled task using all the same details, it runs without issue.
I've seen other threads about going into the C:\Windows\System32\Tasks folder, and change the security to update the Owner, but I still get the same result.
Has anyone had any luck setting a Scheduled task up via PowerShell script, and/or what the resolution to the permission issue?
After much tinkering around, I found that the Azure DevOps agent was set up using the default SYSTEM credentials.
I created a new ADO Agent that runs as an admin Service account, and when the Scheduled task was deployed, it ran without issue.

How to run PowerShell task with admin privileges in Azure Devops pipeline

I am trying to create RamDisk in DevOps agent servers using PowerShell task from Pipeline. The script can create the Ramdisk rive but unable to format and mount it. Its giving error that it needs elevated prompt.
How I can run PowerShell task with admin privileges in Azure DevOps pipeline
Since you add azure-devops-self-hosted-agent tag, I assume you are using a self-hosted build agent for build. Then you can try to update the build agent to run with an administrator account.
If you configured the agent to run as a service, it starts
automatically. You can view and control the agent running status from
the services snap-in. Run services.msc and look for the agent. If
you need to change the agent's logon account, don't do it from the
Services snap-in. Instead, try to re-configure the agent and run it
with an administrator account.

VSTS PowerShell Task and Azure AD cmdlets

I would like to run a PowerShell script during deployment using a PowerShell Script Task in VSTS release. I need to do some operation with Azure Active Directory, but right now I have no access to it, although running locally works fine.
For example running Get-AzureRmADApplication gives "Insufficient privileges to complete the operation".
Any solutions or suggestions to this issue?
P. S. Actually I was using Azure PowerShell task.
I can reproduce the issue, there is a similar issue: New-AzureRmADApplication: Access denied to the specified API version
I submit a new issue Get-AzureRmADApplication Access denied to the specified API version, you can follow up it.
When running commands against Azure Active Directory you will have to give the application it is running under Read Directory rights in Azure Active Directory.

Visual Studio Team Services - release definition - run task (PowerShell) as admin

In VSTS I have a release definition, which needs to run a PowerShell script as administrator.
The release agent is configured to run as an account, which is a local administrator and has the required permissions, but UAC is restricting those permissions, unless PowerShell is executed "as administrator".
Can I run PS as admin from VSTS without disabling UAC completely on the server?
I don't think there is a way to do that using PS task. Can you try "Run PS on target servers" and provide admin creds. You will need to provide the machine name of the agent (so this is not ideal) as an input to the task. That might not require UAC. We will file this as a feature request.
I ran into this same problem. To fix it I did this (YMMV):
I uninstalled the Azure DevOps agent
Verified that the agent user was an admin
Reinstalled the Azure DevOps agent
When I originally installed the agent, the user the agent was running as was not an admin (I added that user to the Builtin\Administrators group after I installed the agent). I'm not sure if that caused a problem, but uninstalling/reinstalling solved it for me.
I do not think that the Inline or Path options are the issue. Maybe installing an Agent in the Deployment Environment Machine, assigning in the installation process the credentials of a user you are sure has administrative privileges on the SharePoint farm could help you. Then in VSTS verify that the agent is Online and working in the Deployment Group Section.
We got around not being able to use elevated Powershell commands by creating a light .Netcore Worker service with http request capability running in an elevated service privilege state. You can send a Invoke-RestMethod command from a regular Powershell YML task and it will trigger your custom logic on the other end of the ASP controller. We use it to remove Appx packages before re-installing for our Unit tests. The repo is located at, AzureAdmin