I want to create a task which calls a Powershell script to do some processing. However I can't access Azure Batch runtime environment variables from my Powershell script.
I can access via cmd, command below correctly outputs
D:\batch\tasks\applications\test12020-05-04-17-32':
cmd /c echo %AZ_BATCH_APP_PACKAGE_Test#1%
However equivalent Powershell command below shows incorrect output
%AZ_BATCH_APP_PACKAGE_Test#1%':
powershell -Command echo %AZ_BATCH_APP_PACKAGE_Test#1%
Of course there is a hacky way around this which is to create a .bat script to call my .ps1 script. But I was looking for a better solution.
Is it possible to access Azure Batch runtime environment variables from Powershell?
As iRon said, echo is not really a PowerShell command, try the code as below:
powershell -Command $Env:ComSpec
The environment variables in my local is as below:
And here is the PowerShell running snapshot:
Related
I am trying to deploy eShopOncontainers to AKS with the help of eShopOncontainers-wiki-github.
I don't know where/how to run the below command. They mentioned "Deploy Public Images From DockerHub" for windows use below command.
.\deploy-all.ps1 -externalDns aks -aksName -aksRg -imageTag linux-dev -useMesh $false
I've tried both Azure cli and windows powershell, the below error is showing
The term '.\deploy-all.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program.
I have a task: to install solution (wsp) on remote machine using Azure DevOps Pipelines.
I have folder with *.wsp. And I have a PS-script. If I do it manually on this machine it works fine. No errors.
But when I use Azure DevOps I have a message:
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1:1 char:1
+ Add-PSSnapin "Microsoft.SharePoint.Powershell"
At the very beginning of my PS script I have this:
Add-PSSnapin "Microsoft.SharePoint.Powershell"
I need it, because I use Uninstall-SPSolution, Remove-SPSolution, Add-SPSolution and Install-SPSolution cmdlets inside script.
I've tried to add
Add-PSSnapin "Microsoft.SharePoint.Powershell"
to "profile.ps1", I've tried to run powershell inside powershell. Again and again error is the same. I don't understand how to fix it.
https://i.stack.imgur.com/UNM7R.png
https://i.stack.imgur.com/NTiWs.png
https://i.stack.imgur.com/sA6tU.png
https://i.stack.imgur.com/bK9kq.png
Try preceding your command with Get-PSSnapIn:
if (Get-PSSnapin "Microsoft.SharePoint.Powershell") {
Add-PSSnapin "Microsoft.SharePoint.Powershell"
}
Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
At C:\Windows\SysWOW64\WindowsPowerShell\v1.0\profile.ps1:1 char:1
Based on the error message, when the powershell execute the ps file, it get the error.
In Auzre Devops Powershell task, it will use the powershell.exe from C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.
The exe path you use to execute the powershell file is different from the default.
You could run the following command in azure devops:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered
and
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe Get-PSSnapin -Registered
You can check if the Microsoft.SharePoint.Powershell exists.
Then you could use the correct powershell.exe path to execute the ps file.
For example:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe "path/xx.ps"
I am trying to SysPrep a VM in Azure via Azure DevOps YAML pipelines. In the same pipeline (previous stage), I am deploying the VM via Azure Resource Manager (ARM) templates and copying scripts and files to the machine. I want to automate the whole process so RDP'ing into the machine is not an option. When I run SysPrep on the VM, I am getting the following error message:
##[error]The remote session query failed for <insertMyFQDN>.eastus.cloudapp.azure.com with the following error message: Access is denied.
From my repo I have a couple of files, including a PowerShell script that SysPreps the machine (sysPrepvm.ps1) - taken from Run SysPrep remotely through commands from Azure powershell. The script works when I am logged in on the machine and run in manually.
sysPrepvm.ps1
$sysPrepPath = 'C:\Windows\System32\Sysprep\Sysprep.exe'
$arguments = '/generalize /oobe /shutdown /quiet'
Invoke-Command -ScriptBlock {param($sysPrepPath,$arguments) Start-Process -FilePath $sysPrepPath -ArgumentList $arguments} -ArgumentList $sysPrepPath,$arguments
I am using the built-in task in Azure DevOps, "Powershell on Target Machines", with which I am able to invoke other commands or scripts, so I am confident that the task works.
My Stage in the YAML Pipeline looks like this:
- stage:
displayName: SysPrep
variables:
azFQDN: $[stageDependencies.Connect.connect.outputs['SetEnvVars.azFQDN']]
jobs:
- job: SysPrepVhD
steps:
- task: PowerShellOnTargetMachines#3
inputs:
Machines: '$(azFQDN)' # FQDN on the machine
UserName: '$(adminUser)'
UserPassword: '$(adminPw)'
ScriptType: 'FilePath'
ScriptPath: 'C:\Windows\System32\Sysprep\fishtank\sysPrepvm.ps1'
Is there a limitation of running this remotely? I haven't been able to find a work-around yet, so any answer is greatly appreciated.
Edit
I have also tried running the script with -Verb RunAs, as an Inline script instead of File, as well as tried the accepted answer on the previous post:
Sysprep an Azure VM using PowerShell task in a pipeline
I managed to find a way using Custom Script Extension instead. I didn't think of doing this at first, since the ARM template I am using already has a Custom Script Extension on the machine to enable WinRM, and cannot have multiple Custom Script Extensions installed at the same time. Also, I didn't want to execute SysPrep as soon as the machine booted because it had to run other scripts and settings first. In my YAML pipeline, I managed to remove the existing one to redeploy a new Custom Script Extension with the script included.
I posted the entire solution on GitHub - https://github.com/Crytlig/azsysprep - for anyone in interested. It obviously needs a bit of cleaning but it works as is.
I've been slowly working out how to call a PowerShell script to transform IIS logs using LogParser 2.2. I've settled on using Azure Data Factory Batch Service Custom Activity to run the PowerShell script. I've been able to figure out how to address many of the file path issues that arise in running PowerShell from within Azure Custom Batch Activity, but I can't figure this one out.
Currently I'm just trying to print via Write-Host the environment variable AZ_BATCH_APP_PACKAGE_powershellscripts#1.0 I've been able to print other environment variables, but I believe the #1.0 at the end of this one is causing all my grief. BTW the 1.0 is the version of the application loaded into the batch framework in Azure.
All of the following attempts have failed:
powershell powershell Write-Host "$AZ_BATCH_APP_PACKAGE_powershellscripts#1.0"
powershell Write-Host "$AZ_BATCH_APP_PACKAGE_powershellscripts#1.0"
powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts#1.0"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts\#1.0"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts/#1.0"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts`#1.0"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts`#1`.0"
powershell powershell Write-Host "$env:AZ_BATCH_APP_PACKAGE_powershellscripts\`#1.0"
powershell powershell Write-Host "$AZ_BATCH_APP_PACKAGE_powershellscripts`#1.0"
These works, but are either cmd window or not the variable I want:
powershell powershell Write-Host "$env:AZ_BATCH_TASK_DIR"
powershell powershell Write-Host "$env:AZ_BATCH_ACCOUNT_URL"
cmd /c echo %AZ_BATCH_APP_PACKAGE_powershellscripts#1.0%
So what is the secret syntax sugar to getting this to work in Azure?
Sure, you can do this:
powershell powershell Write-Host "$((Get-Variable -Name 'AZ_BATCH_APP_PACKAGE_powershellscripts#1.0').Value)"
Or this:
powershell powershell Write-Host (Get-Variable -Name "AZ_BATCH_APP_PACKAGE_powershellscripts#1.0").Value
I went through close to 50 tries before getting this to work like so:
powershell powershell Write-Host (Get-ChildItem Env:AZ_BATCH_TASK_DIR).Value
powershell powershell Write-Host (Get-ChildItem Env:AZ_BATCH_APP_PACKAGE_powershellscripts#1.0).Value
Now this was just a stepping stone to running a PowerShell script stored in an attached application to the Azure Batch module. I'm hopeful Microsoft will add a Databrick or better way to run a PowerShell script in Azure Data Factory, but until then this is the only method I found to run a powershell script:
powershell powershell -command ("(Get-ChildItem Env:AZ_BATCH_APP_PACKAGE_powershellscripts#1.0).Value" + '\Powershell\processWebLogsFromAzure.ps1')
This should work for anyone just trying to run from the Batch Task Dir:
powershell powershell -command ("$env:AZ_BATCH_TASK_DIR" + '\wd\processWebLogsFromAzure.ps1')
Hope it helps someone!
I have an Azure Powershell Runbook which is used to invoke a java jar inside the Hybrid Worker Groups.
Currently we pass the absolute java path and run the program, Iwant to change this and pass JAVA_HOME instead. I have set java home as environment variable and is able to access inside the hybrid worker.
$java = $Env:JAVA_HOME
Write-Output "$java"
this give the right path when ran inside the hybrid.
When passing this value via runbook it is not returning any value. The Write-Output returns empty when running via Automation Runbooks
$java = $Env:JAVA_HOME
Write-Output "$Env:JAVA_HOME"
$arguments = "-cp C:\\prgm\jws\jar\jwsClient-0.0.1.jar"
$p = Start-Process -FilePath $java -ArgumentList $arguments
Any help is appreciated.
Just a suggestion. Based on my experience, JAVA_HOME is usually used to store JDK path while in development. And to run a jar, we just need JRE.
But, I am not sure how Azure runbook sets its environment variables. You can output all environment variables as following:
Get-ChildItem env:* | sort-object name
If you know which variable stores the path of JRE, you may retrieve it with script then.