Подстраиваем по себя PowerShell ISE

Windows PowerShell ISE (Integrated Scripting Environment) удобная графическая оболочка для написания, просмотра, редактирования powershell-овских скриптов. Вот его достоинства по конкретнее:

  • The Windows PowerShell Integrated Scripting Environment (ISE) is a host application for Windows PowerShell. In Windows PowerShell ISE, you can run command.
  • One thought on “ PowerShell: Add VMware PowerCLI to PowerShell ISE on demand ” Roman 2016-04-21. Thanks, this was very helpful. Other places, I’ve only seen mention of VMware.VimAutomation.Core, which doesn’t cut it for all of the commands I need to run.
  • So I have a script that works fine when I run it from PowerShell ISE. However, I need to automate it, so I run powershell.exe.script.ps1, but I get errors about some of the commands not being.
  • multiline editing
  • tab completion
  • syntax coloring
  • selective execution
  • context-sensitive help
  • keyboard shortcuts support

Выглядит он так

I still don’t leverage the PowerShell Integrated Script Editor (ISE) as much as I should. But after reading a few recent entries from The Scripting Guys on inserting help and headers into a script.

Писать в нем скрипты одно удовольствие :)

Все алиасы, функции, переменные которые мы пишем во время работы сохраняются только в течении только той сессии в которой они созданы и при закрытии ISE или powershell-овской командной строки они теряются. Чтобы сохранить переменые, функции или теже алиасы нам надо использовать powershell-овские профили. Профили загружаются каждый раз как мы открываем ISE или powershell-овскую командную строку. Чтобы они загрузились без ошибок Execution Policy должен нам позволить загрузить конфигурацию. Для того чтобы избежать головной боли ставим Execution Policy RemoteSigned (Так делать не очень рекомендовано, но для лабы, тестов т.д. вполне подойдёт)

Set-ExecutionPolicy RemoteSigned

Для того посмотреть где находится профиль набираем $profile

Чтобы открыть его используем ноутпад и делаем изменения которые нам нужны. Например мне удобно чтобы PowerShell Snap-in VMware.VimAutomation.Core загружался автоматический во время открытия ISE.

notepad $profile

(если профиль не открывается тогда надо его сперва создать командой New-Item -ItemType File -Path $profile -Force)

Ise

после этого внести нажи изменения в профиль

и сохраняем изменения и отныне каждый раз при открытии ISE будут доступны командлеты для управления vSphere. Точно также можно внести изменения для командной строки Windows PowerShell-овского профиля.

Подробнее о профилях можно почитать на MSDN-е.

I still don’t leverage the PowerShell Integrated Script Editor (ISE) as much as I should. But after reading a few recent entries from The Scripting Guys on inserting help and headers into a script, I thought I’d dig in a little more. I’ve a few things to share but today I want to show you what I did to insert a datetime into a script.

I’m assuming like many of you, Notepad is still a trusty tool. One of my favorite features is the F5 shortcut which will insert the current date and time. Very often when working on scripts in the ISE, I wanted to insert a current date and time and longed for that shortcut. Now I have it and it is really pretty simple. At the command prompt in the ISE all you need is this expression:
[cc lang=”Powershell”]
$psise.CurrentFile.Editor.InsertText($(get-date))
[/cc]

Powershell Ise Sierra Vista

That’s it. The current date and time will be inserted in the current file at the cursor. Of course, I don’t want to always have to type that, so in my ISE profile script, Microsoft.PowerShellISE_profile.ps1, which is in your Windows PowerShell folder with your other profiles, I added some code to add this command to the menu with a keyboard shortcut. If you don’t have the profile script, you’ll have to create it.

In my ISE profile script I added the following line.
[cc lang=”PowerShell”]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Insert Datetime”,{$psise.CurrentFile.Editor.InsertText($(get-date))},”ALT+F5″) | out-Null
[/cc]

What I’ve done is invoke the Add() method from the Submenus object. This method takes 3 parameters:; The text that will display under the Add-Ons menu, a script block that will execute, and a keyboard shortcut. I pipe it to Out-Null purely for cosmetic reasons. Otherwise, I see the command in the output window whenever I run it.

Now, whenever I press ALT+F5, I get the date and time. F5 is already reserved for executing the script so I had to accept a slight substitution. By the way, I also load the functions from The Scripting Guys in my ISE profile and add menu items for them as well; one with a keyboard shortcut and one without.
[cc lang=”PowerShell”]
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Add Header”,{Add-HeaderToScript},$null) | out-Null
$psISE.CurrentPowerShellTab.AddOnsMenu.Submenus.Add(“Add Help”,{Add-Help},”ALT+H”) | Out-Null
[/cc]

Powershell Ise Sierra

Powershell Ise Server 2012 R2

I have a little more to share on this topic but that will be for another day.