Working with the Yandex Cloud CLI and API in Microsoft Windows
Yandex Cloud services provide a variety of interfaces for implementing your projects. Usually, Yandex Cloud services have a web interface in the management console
The Yandex Cloud documentation contains step-by-step guides for working with different interfaces. Sample CLI commands and API requests usually use the Bash syntax. You can run them as is in the Linux console, macOS terminal, or WSL
If you use Yandex Cloud in Windows with no WSL, sample CLI commands and API requests may run incorrectly. To use the examples provided in the documentation, install the Bash interface on your PC, create a VM to work with Yandex Cloud, or adapt commands to the syntax of the Windows command shell you use. For more information on Windows command shells, see the official Microsoft documentation.
How to run Bash commands in the Windows terminal
Windows Subsystem for Linux
-
To check whether WSL is installed on your PC, run the following command in cmd or PowerShell:
wsl -u rootIf WSL is installed, the terminal will switch to Bash mode:
root@<machine_name>:/mnt/c/Users/<username># -
If not, install
WSL and repeat the previous step.
If you are using Windows without WSL, you can install a Bash emulator, such as Git Bash
How to adapt your code to command line shells native for Windows
If you want to run your Bash code in cmd or PowerShell, bring it to the appropriate format:
- Replace basic syntax elements.
- Check the format of equivalent commands.
- Replace other commands with their equivalents.
- Check the names of system variables and auxiliary syntax.
Replace basic syntax elements
-
Replace the directory separator in file paths:
/in Linux and\in Windows. -
Replace line break characters. Instead of
\, use^in cmd and`in PowerShell. -
Check out the rules for using all kinds of quotation marks, as they are handled differently in Linux
and PowerShell . -
Check out the syntax for using variables.
Examples of using variables
Action Linux Windows cmd PowerShell Writing a variable export VAR="1"set VAR="1"$Env:VAR="1"Reading a variable ${VAR}%VAR%$Env:VAR
Check the format of equivalent commands
Many commands in cmd and PowerShell have aliases similar to Bash commands. For example, the Set-Location PowerShell command (change to a different directory) will be run if you type cdin the terminal.
If the code execution fails:
-
Check if the Bash command has an equivalent in your terminal. For example, in PowerShell, run:
aliasThis will output a list of all aliases defined in the system settings:
CommandType Name Version Source ----------- ---- ------- ------ Alias % -> ForEach-Object Alias ? -> Where-Object Alias ac -> Add-Content Alias asnp -> Add-PSSnapin Alias cat -> Get-Content Alias cd -> Set-Location Alias CFS -> ConvertFrom-String 3.1.0.0 Microsoft.PowerShell.Utility Alias chdir -> Set-Location Alias clc -> Clear-Content Alias clear -> Clear-Host ...For a complete list of aliases, see the
aliascommand output. -
Make sure the command in your command shell performs the same function as its equivalent Bash command.
-
Check the command format: the alias keys may differ. For example, the
lscommand has an alias in PowerShell, but the argument keys differ.
Replace other commands with their equivalents
-
If a command has no alias, find its equivalent.
List of equivalent commands in Linux, cmd, and PowerShell
Action Linux Windows cmd Windows PowerShell List files in a directory ls -sdirdir,ls,Get-ChildItemList files in a directory and sub-directories ls -Rtreels,Get-ChildItemCreate a file touchcopy nul >New-ItemRename a file mvrenren,Rename-ItemCopy a file cpcopycopy,Copy-ItemMove a file mvmovemove,Move-ItemDelete a file rmdeldel,Remove-ItemCompare file content difffcdiff,Get-Content,Compare-ObjectFind file strings or object properties grepfindSelect-String,Where-ObjectOutput command help man <command><command> /?Get-Help <command>Output the name of the current directory pwdcd$PWDCreate directory mkdirmdmkdir,New-ItemOutput current date and time datetimeGet-DateConfigure file access policies chown,chmodattribattrib,Set-XFileOwnerSchedule the command run cronatschtasks,Register-ScheduledJobAccess a web resource over HTTP wgetwgetInvoke-WebRequest -
If a command has no direct equivalent, you can perform the same action using a combination of commands or other methods. For example, to convert a file to Base64
in PowerShell, use the static Convert method:[Convert]::ToBase64String([IO.File]::ReadAllBytes(''<filename>'')). -
Consider the specifics of your command shell. For example, PowerShell works with objects and their properties rather than text. Therefore, instead of searching by
grep, you will need to use theWhere-ObjectPowerShell command in most cases. It handles objects with required property values. To search for a sequence of characters, use theSelect-Stringcommand.
Check the names of system variables and auxiliary syntax
- Check the names of environment variables. For example, instead of the
USERenvironment variable used in Linux, theUSERNAMEvariable is used in Windows. - If your code contains comparison, selection, or loop statements, change the syntax to meet the requirements of your command shell. To learn more about the difference between the Bash and PowerShell auxiliary syntax, see Bash vs PowerShell Cheat Sheet
.