Posted on Leave a comment

Start windows service with powershell

Sometimes the windows services manager may not be enough for interaction with the services. Using the GUI you can set a service to start on startup by changing the status from Manual to Automatic but there could be cases that you want to execute this functionality with powershell. A reason for that could be a failure on the service startup that you want to check through code.

Using powershell you can check if a service is running with Get-Service. The below example checks if docker service is running and if not it will print a message on the output of the command line.

if (-not((Get-Service -Name "com.docker.service").Status -eq "Running")) { echo not running }

We can now check how to implement the start of the service using powershell. As shown below the docker desktop service is not running at the moment.

 if (-not((Get-Service -Name "com.docker.service").Status -eq "Running")) { Start-Service -Name "com.docker.service" }

After running the powershell above, we will get the service started.

Using the powershell you can create an automatic job with task scheduler and check this behavior on the computer startup.

Posted on Leave a comment

##[error]Script failed with error: Error: Unable to locate executable file: ‘pwsh’.

In AzureCLI@2 you may choose from a variety of options when it comes on how this task will be executed on the agent machine. I usually choose powershell for windows machines and powershell core for Unix based machines (pwsh).

Recently I got an error on a Windows machine when using Powershell core. The latest version of powershell which is currently on 7.* version can be used as pscore in the AzureCLI@2 task.

      - task: AzureCLI@2
        displayName: az cli task
        inputs:
          azureSubscription: 'SERVICE-CONNECTION'
          scriptType: 'pscore'
          scriptLocation: 'inlineScript'
          inlineScript: |
           script

Error message:

##[error]Script failed with error: Error: Unable to locate executable file: ‘pwsh’. Please verify either the file path exists or the file can be found within a directory specified by the PATH environment variable. Also verify the file has a valid extension for an executable file.

Solution:

In order to bypass this problem you should make sure that the latest version of powershell which is multiplatform should be installed on your system. At the time of this article this version is 7.*

After installing powershell you should make a restart also on the machine in order for the environmental variables to be added on PATH. Then you can execute your pwsh tasks on your agent machines.

Posted on Leave a comment

Initialize and format windows disk with powershell

Sometimes you may need to automatically create windows disk for a virtual server using an automation mechanism. With the below powershell you can initialize a new emtpy disk with GPT partition and format it according to your needs.

You can get your available disks using:

Get-Disk

You can use the below powershell and change the below settings:

DriveLetter : What your drive letter will be
AllocationUnitSize: Default is 4k, but in my case I define 64k
DiskNumber: number of disk from Get-Disk command
NewFileSystemLabel: name of the volume

 Initialize-Disk -number 4 -partitionstyle GPT ; New-Partition -DiskNumber 4 -UseMaximumSize ; Format-Volume -DriveLetter G -FileSystem NTFS -AllocationUnitSize 65536 -NewFileSystemLabel Volume-Name

After applying the volume will be created and initialized.

Posted on Leave a comment

Execute powershell command without username password on Azure virtual machine

You can use az cli task on Azure devops in order to execute a powershell command inside a virtual machine without having to connect on it with username and password. In order to do that you have to use AzureCLI task and invoke a RunPowerShellScript.

    - task: AzureCLI@2
      displayName: execute command inside vm
      inputs:
        azureSubscription: 'subscription'
        scriptType: 'ps'
        scriptLocation: 'inlineScript'
        inlineScript: 'az vm run-command invoke --command-id RunPowerShellScript --name $(vm_name) -g $(vnet_rg_name) --scripts "hostname"'

You should use your own variables regarding the vm_name and rg_name. This task will execute on a windows-latest machine and will output the hostname of the machine that is given as input with the variables.