Ho can i run a powershell comand from a .ps1 file? - azure

I have the following:
$command = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$arguments = #('Set-AzLogicApp -ResourceGroupName "MyResourceGroup" -Name "MyLogicAppName" -State "Disabled" -Force')
& $command $arguments
when I run this from command it says Set-AzLogicApp : The term 'Set-AzLogicApp' is not recognized as the name of a cmdlet
Any reason why this is ?

I want to understand why are you calling Powershell.exe from .ps1 file. Anyway all the installed module will be available for execution if you are executing PS file. You can just simply import the module and run the command.
Additionally , you have to import Az.LogicApp as Joy mentioned after installing it in the machine.
Code would look like below:
Import-Module -Name "Az.LogicApp"
$command = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$arguments = #('Set-AzLogicApp -ResourceGroupName "MyResourceGroup" -Name "MyLogicAppName" -State "Disabled" -Force')
& $command $arguments
I have tried at my end and it worked for me. Please try and let me know if you need any further assistance.

Sorted , I had to run Install-Module -Name Az . thanks for all the help

Related

How start virtual machines as a job in powershell azure

Im working on a powershell script which starts multiple as jobs i have tried this
Start-AzureRmVM -ResourceGroupName "Test2" -Name "tux" -AsJob true
but all i got was this error message :
Stop-AzureRmVM : A parameter cannot be found that matches parameter name 'AsJob'.
You just need to execute the command like below if you want to start the VM as a job:
Start-AzureRmVM -ResourceGroupName "Test2" -Name "tux" -AsJob

How to get output stream for Invoke-AzureRmVMRunCommand command?

I am trying to run the below command
Invoke-AzVMRunCommand -ResourceGroupName $instance.ResourceGroupName -Name $instance.Name -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\tushar.raichand\Desktop\sample.ps1'
Sample.ps1 is as below
$output = Get-LocalUser
Write-Output $output
$output
The output i am getting for Invoke-AzVMRunCommand is
Microsoft.Azure.Commands.Compute.Automation.Models.PSRunCommandResult
First, make sure you have enough permission to show the details of a command, see Limiting access to Run Command:
Listing the run commands or showing the details of a command require the Microsoft.Compute/locations/runCommands/read permission, which the built-in Reader role and higher have.
Besides, the command Invoke-AzureRmVMRunCommand belongs to the AzureRM powershell module which has been deprecated, you may need to upgrade it to the new Az module, refer to this link to upgrade.
I test the script with the new Az command Invoke-AzVMRunCommand, it works fine.
Invoke-AzVMRunCommand -ResourceGroupName joywebapp -Name joyVM -CommandId 'RunPowerShellScript' -ScriptPath 'C:\Users\joyw\Desktop\sample.ps1'
sample.ps1:
$output = Get-LocalUser
Write-Output $output
Result:

How to create Azure Automation runbook that will start VM and then execute (local or Azure) script on the VM?

I wan't to create a Runbook that will start a specific (or parameter controlled) VM, and then run a script (locally or from blob storage) on the VM.
I have checked a lot of documentation, but so far without luck in getting it to work.
What I got so far under the same Resource Group:
VM created
Automation account created incl. Run As account
Azure Automation solution (OMS)
Credential (to my own account) under the automation account
Used several Runbook galleries and other code examples using functions as e.g.
Start-AzureVM ...
Invoke-Command ...
Anyone of you good people out there who can sample a guideline on what is needed depending on methods being used?
The VM start part is working, but I cannot get the login + executing of script to work!
I'm not a high skilled developer, and I have even doubts about choosing between the script languages in Azure.
Any help will be highly appreciated.
Thanks,
Tom
Invoke-Command
Invoke-AzureRmVMRunCommand
Set-AzureRmVMCustomScriptExtension
New-SSHSession + Invoke-SSHCommand
Code taken from e.g. gallary "Connect-AzureVM"
the parameter -ScriptPath of Invoke-AzureRmVMRunCommand should not point to the path in the remote computer, but should point to the local path of runbook environment.
Sample code like below(create a file named atestfile.txt in the remote vm):
$ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $ServicePrincipalConnection.TenantId `
-ApplicationId $ServicePrincipalConnection.ApplicationId `
-CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
#define resource group and vm name
$resourceGroup ="xxx"
$VmName ="xxx"
#define the scripts in $scriptblock, and add the content of $scriptblock to aa.ps1 in current directory of runbook
write-output "create test file"
$scriptblock = "New-Item -path c:\test -name atestfile.txt -itemtype file -force"
Out-File -FilePath aa.ps1 -InputObject $scriptblock
#Note that the -ScriptPath should not point to the remote path(in remote vm), it should point to the local path where you execute the command Invoke-AzureRmVMRunCommand
Invoke-AzureRmVMRunCommand -ResourceGroupName $resourceGroup -Name $VmName -CommandId 'RunPowerShellScript' -ScriptPath aa.ps1
#after execution, you can remove the file
Remove-Item -Path aa.ps1
write-output "done now"
Test result:

Azure Custom script extension for Linux

I am trying to provision and configure a Linux VM on Windows Azure with the following command:
$PublicConfiguration = '{"fileUris":["http://path/deployment/ubuntu-elasticsearch-installation.sh"], "commandToExecute": "sh ./ubuntu-elasticsearch-installation.sh -n Node1 -n Node2 -c cluster"}'
The script works when I execute it on any Linux VM in the same format. But when i do this only the -c parameter is passed to the script during provisioning.
On the other hand when I do it this way just for test (the script taked parameters and installs them):
$PublicConfiguration ='{"fileUris":["http://path/deployment/test.sh"], "commandToExecute": "sh test.sh apache2 unzip"}'
it installs both.
Then I do the following for both:
$ExtensionName = 'CustomScriptForLinux'
$Publisher = 'Microsoft.OSTCExtensions'
$Version = '1.2'
$vm = Set-AzureVMExtension -ExtensionName $ExtensionName -VM $vm -Publisher $Publisher -Version $Version -PublicConfiguration $PublicConfiguration
New-AzureVM -ServiceName $servicename -Location $location -VMs $vm
Does anyone have experience with Custom Scripts for Linux?
Thanks,
Vangel

Is there a Powershell command to Restart the machine and run a script without any user inputs?

Im running a powershell script which installs sharepoint pre-requisites. after the installation server needs to be restarted and another script has to run
is there a possible way to automate this process in powershell ??
Restart-and-Resume ??
Add the 2nd script to the RunOnce key under HKLM before reboot.
New-ItemProperty -Path HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce `
-Name "SPPreReq" -PropertyType String -Value "powershell -File C:\foo.ps1"
Restart-Computer

Resources