Reputation: 2378
Using powershell, I need to do the following steps.
1. Login to Azure account.
2. Create a VM from the Image.
3. In the image , there is windows service.So when we created the VM, that windows service is already running on it.Stop that service.
4. Copy some binaries into the VM to a specific folder.
5. Start the service again.
Out of all these, I was able to acheive 1 and 2 steps. And I couldn't find any relevant procedure for rest of the steps. Below is the script I have used to do 1 and 2.
Login-AzureRMAccount
$UserName = "[email protected]"
$Password = ConvertTo-SecureString "password" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
New-AzureRmVm `
-ResourceGroupName "RGN" `
-Name "VMName" `
-ImageName "ImageName" `
-Location "West US" `
-Credential $psCred
So can anyone help me or guide me how the rest of the steps be done.
Thanks in advance for your time.
EDIT : The command for stopping the service is
Stop-Service -Name "CPS Dev UniSimServices" -Force
But this works, if I log into the VM and run this command. I want to connect to the newly created VM through powershell from the remote machine itself (without logging into it), and stop the service. How can I do that?
Upvotes: 0
Views: 6252
Reputation: 31384
You can interact with the VM using extension without connecting into it. You can use the PowerShell command with the custom script:
Set-AzureRmVMExtension -ResourceGroupName "myResourceGroupAutomate" `
-ExtensionName "IIS" `
-VMName "myVM" `
-Location "EastUS" `
-Publisher Microsoft.Compute `
-ExtensionType CustomScriptExtension `
-TypeHandlerVersion 1.8 `
-SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}'
Update
If you really want to use a custom script for the extension, you can use the command like this:
Set-AzureRmVMCustomScriptExtension -ResourceGroupName myResourceGroup `
-VMName myVM `
-Location myLocation `
-FileUri myURL `
-Run 'myScript.ps1' `
-Name DemoScriptExtension
For more details, see Set-AzureRmVMCustomScriptExtension.
Upvotes: 2
Reputation: 123
To stop a service use the following command Stop-Service -Name "NameOfService" -Force
, take a look on the docs of Stop-Service for any question. Then copy the files that you need via robocopy, Copy-Item or whatever and Start-Service -Name "NameOfService"
. If you have any problem to copy files from your local or remote machine to your VM just post it.
As said in the other answer you can use the Set-AzureRmVMCustomScriptExtension, the following example executes in the VM a script upload to a Blob Storage account
Set-AzureRmVMCustomScriptExtension -ResourceGroupName $ResourceGroup -Location $LocationName -VMName $ServersName -Name $scriptName -StorageAccountName $NameStorageAccount -FileName $fileName -Run $fileName -ContainerName $NameCOntainerStorage -Argument "-param1 $param1"
another option is using the command Invoke-AzureRmVMRunCommand, for example
Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroup -Name $ServersName -CommandId 'RunPowerShellScript' -ScriptPath "pathToFileExecute" -Parameter @{"Param" = "$Param"}
note: the argument -ScriptPath can be for example a shared path like \VM\e$\script.ps1 or local path.
last option is by using the Invoke-Command, for example:
Invoke-command -ComputerName $server -crdential $objectcredential -scriptblock {Stop-Service -Name "nameService" -force}
Upvotes: 1