Start-Sleep not working with Azure PowerShell Task in Azure Pipeline - azure-devops

I'm trying to add a sleep command in an Azure Power Shell Task in an Azure Pipeline, like in the screenshot below. But it's not working, what am I doing wrong?

Sound like the instruction isn't hit. To check, you can add a task that only do the sleep. Then you will confirm if Start-Sleep work or not.

Related

Azure DevOps - Calling a PS script from within a PS script

Can you not call a script from within a script in an Azure PowerShell task?
Background -
I have a an Azure Repo with two scripts in it (let's call them script0 and script1). There's no build going on so there's no build pipeline. There's just a release pipeline. The artifact it is picking up from is Azure Repository Git. I have just one task in the (release) pipeline and it's the Azure PowerShell task.
In script0, which is the main script, I have a for loop, which requires me to run the script1 (apart from the various other things that goes on in the loop).
For the life of me, I am unable to figure out how I can achieve that. Worst of it, it works locally. Also, everything else works in the loop. I have tried tons of things to fix it, but I will start with just this for now: The error I am being thrown when I run
$TeamFoundationCollectionUri$TeamProject/testscript.ps1 $stage $FunctionHosts[$i] (($hashtable | select -First 6).Key[$i]) $ResourceGroupName $location $functionApps $AdminClientSecret $VaultName $JsonFile
(Now, mind you - that is part of script0 - the main script).
Here's the error:
The blurred area is script0 and testscript.ps1 is script1
I have tried almost everything
Using the Call operator &
Using \, /, //
Invoke-Expression -Command "<code here>"
Invoke-Command
Also tried powershell.exe -Command <code here>
As you can tell, none of these have worked.
I got this working by using the Call operator (&) before the path where the script resides. So, I did this:
& $(System.ArtifactsDirectory)\$(System.TeamProject)\testscript.ps1 <pass the params here>
and it worked.

Azure Devops Interactive agent

For running our UI tests were using the task "Visual Studio test agent deployment", which has been deprecated now. So i have moved the tasks to use "Visual Studio Test" task.
This needs agents to be configured as interactive process rather than service. So i created a new build server with interactive process agent running with admin rights user.
For signing or code we are using signed certificates that gets installed on the build server. But for some reason this new build server seems to loose the certificate cred every single time the pipeline runs.
I have tried to to run a ps1 file to reinstall the cert with no success. Below id the code that i have for reinstalling the cert and i am using task "PowerShell on target machines" to run the script. The user running this script is admin on the box, is there a different way of getting the ps1 file to run as admin all the time?
Set-Location "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools"
Start-Process cmd.exe
Sleep 2
$WshShell = New-Object -ComObject WScript.Shell
Sleep 2
$WshShell.sendkeys(".\sn.exe -d $KeyContainer{Enter}");
Sleep 2
$WshShell.sendkeys(".\sn.exe -i $PfxCertificatePath $KeyContainer{Enter}");
Sleep 2;
$WshShell.sendkeys("$passWord{Enter}");
Sleep 2;
$WshShell.sendkeys("{Enter}");
Any help would be greatly appreciated.
Is there a way to get the cmd.exe to run as admin through the
pipeline?
As i know, for now only the version 3.0 of Powershell on Target Machine task supports our running it as admin. Apart from it, the Command-line task and PowerShell Task doesn't support this feature yet.
So I'm afraid the answer is negative, Azure Devops pipeline doesn't have the option to make us run those tasks(CMD task, PS task...) with admin rights. (According to your description, you're trying to get a different way of using PS on Target Machine task. )
Sorry for the inconvenience. Actually I think it could be one good idea so you could feel free to add your request(CMD task with admin...) for this feature on our UserVoice site, which is our main forum for product suggestions. But it may not be accepted by MS if it doesn't get enough votes.
Hope it helps:)

Powershell script triggers process only when invoked manually. Timing out when triggered via Scheduled Task

On a Windows 2012 R2 server there is a Powershell script that I can manually invoke to start a process on some EXE, this works just fine.
But when trying to trigger it via a scheduled task (invoking the same script) the start-process within the script just doesn't trigger or finish. Causing the task scheduler to terminate the task due to exceeding the timeout threshold.
Here's the core section of the script:
$exe = "c:\some\app.exe"
$arguments = "-user me -pwd secret"
$process = Start-Process $exe -ArgumentList $arguments -PassThru -Wait
return $process
Is there some way I can get some insights into what start-process is doing or why the same script works when invoked manually but not programmatically?
I want to emphasize that the way the script is invoked from the scheduled task is not a problem! The script triggers because the corresponding log file populates.
Any insights or help on this is greatly appreciated!
quick update on this since I found the problem. It turns out, it had nothing to do with either the powershell script or the scheduled task itself...
On the machine the script is running on, there is a network share that is mapped as the z:\ drive. I use it to save logs to. Now apparently that mapping/mounting is handled differently depending on whether the script is invoked interactively or programatically, because in the latter case it appears that the resoultion of the network path \\network\share\folder1 does not succeed, however there is nothing complaining about it, the process just silently does not start. If however, I point the logs to a physical local path or the explicit full network path itself, there is no problem running the script.
Lesson learned, never trust OS' drive mapping of network paths :D
Cheers

How to execute PowerShell script as Azure Automation Runbook from InlineScript inside PSWorkflow runbook?

In a PowerShell Workflow activity, I can call a native PowerShell script using InlineScript:
workflow test
{
InlineScript
{
.\script.ps1
}
}
But in Azure Automation, the dot-path (at least in my tests) was returning c:\windows\system32, and the script-as-runbook in Azure Automation did not exist there (or rather, it failed to execute because it could not find the script).
Is it possible to execute a native PS runbook stored in AAuto like this?
If so, how do I specify the path to the file?
Is this a bug/oversight in Azure Automation's parsing/compilation process of Workflow runbooks & InlineScript activities, preventing the dependent runbook from being copied to the worker?
I did a little hunting, and found that when native PS runbooks are executed:
They are first inspected for any other runbook references.
As part of the deployment to the worker for execution, a randomly-named folder is created under C:\Temp\
Referenced runbooks are eventually copied to this folder.
If runbooks are NOT found to be referenced, they are NOT copied to the temp directory.
The root runbook does not appear to be copied to the folder.
The dynamically-named folder is NOT created (under c:\Temp) when executing a Workflow runbook.
As part of the standard Workflow compilation, InlineScript activities have their contents copied to the autogenerated xaml. I'm uncertain about a linked file, though based on behavior that looks to be a runtime concern. My guess is that compilation happens each time a workflow is executed (hence the delayed start), and takes place on the worker, utilizing the standard PS workflow compilation just like local would.
I cannot (easily) convert this script to a workflow, and it is used from within other workflow activities. Right now the only way I can make this 'work' is to copy & paste the script into the first InlineScript within a workflow that requires it, which is obviously tedious & annoying from a maintenance perspective.
Presumably, as a workaround, I could use a Hybrid Worker, but that comes with a host of other issues, like ensuring the child runbooks are published there & having to maintain them separately, or AAuto not automatically pushing custom modules from the Automation Account to the worker (though this is planned), etc.
Please see https://azure.microsoft.com/en-us/blog/announcing-powershell-script-support-azure-automation-2/:
Right now, you can only invoke inline PowerShell runbooks from PowerShell runbooks, and PowerShell Workflow or Graph runbooks from PowerShell Workflow or Graph runbooks. This may change in the future.
It hasn't changed yet :)

Azure Runbook - Create from gallery using a Powershell script

I was trying to create a RunBook in Azure Automation and I chose to use a Powershell script (Cmdlet). But the creation failed with an error because azure couldn't directly convert it and a manual edit is required to make it a "workflow". I am not a pro on PS and I understand you need to know how a workflow works. But my immediate goal is to get the Runbook working and hence trying to figure out an easy way to convert a PS script to a PS workflow for the purpose of runbook... Appreciate any help
Please see this blog post for info on PowerShell script to PowerShell Workflow conversion: http://azure.microsoft.com/blog/2014/11/25/introducing-the-azure-automation-script-converter/
However, regardless of conversion, you can't interact with local files on your computer from Azure Automation, so this script won't work. See https://social.msdn.microsoft.com/Forums/en-US/03a7aace-4e3e-4dab-9a8f-4ed1002fde4e/how-to-upload-files-from-users-machine-to-blob-storage-using-azure-automation?forum=azureautomation for more details.