PowerShell Profile

This week I have been at the PowerShell Usergroup in Munich, Germany.

They wished to get information about PowerShell Profiles, so I commited a talk on PowerShell Profile. And here is the content.

There are 4 PowerShell Profiles loaded on startup of the console. But there can be some more Profiles in your environment. That is, beacause you have two PowerShell profiles per PowerShell console. So VSCode, PowerShell ISE and the PowerShell command prompt have two profiles each. One Profile is for All Users, one for the current user. Additionally you have got a PowerShell Profile for Windows PowerShell and a seperate one for PowerShell Core. So there can be some PowerShell Profiles on your computer.

Use the following command in your prefered console to get the actual profile pathes:

  1. $profile.AllUsersAllHosts
  2. $profile.AllUsersCurrentHost
  3. $profile.CurrentUserAllHosts
  4. $profile.CurrentUserCurrentHost

By the way, they are loaded in exactly this order (which makes sense).

The 4 profiles you have in the running PowerShell ISE

What I have in my PSProfile

I talked about which PowerShell Profiles exists and also, what I have in my profile as an PowerShell automator. As an automator my focus is on execution duration, so the most relevant part is the execution time in the prompt. It looks like this in my console:

image

There are some more featues in my PSProfile prompt, eg the color of the provider is integrated in the path, for example FileSystem and registry:

image

You can find my Profile on Github.

PowerShell Profile Pathes

Here are some examples of profile pathes.

# PowerShell Core with VSCode
ProfilePathC:\Users\patrick\Documents\PowerShell\Microsoft.VSCode_profile.ps1

# Windows PowerShell with VSCode
ProfilePathC:\Users\patrick\Documents\WindowsPowerShell\Microsoft.VSCode_profile.ps1

# Windows PowerShell ProfilePath with ISE
C:\Users\patri\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

# Windosw PowerShell in the Console
C:\Users\patri\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Scripting Guy – Understanding the six PowerShell Profiles (there could be more, as you already know) 

Some ideas how to use the PowerShell Profile in a bad way

My Profile on github

Advertisements

Grant access to Service Requests via AD Groups in User Roles

We grant access to Service Manager Serrvice Requests using AD Groups in User Roles. When a User is added to the AD Group the permissions are not updated – so they do not see the additional Service Request in the portal.

There are two things to do:

  1. Recycle App Pool
  2. User has to re-login (not always)

Here is the code to Recyle the App pool for the standard HTML Portal and the ITNetX Portal:

@('ItsmPortalv3Pool','SMSelfServicePortal_appPool') | % { Restart-WebAppPool $_} 

Link to Technet discussion

Azure Mobile App–some thoughts

Today I have been asked, what could be improved in the Azure Mobile App. Since I mainly use PowerShell, that is a bit of a challenge. In PowerShell there is no GUI – as you know. So I was thinking about ways to improve the Azure Mobile App.

I have multiple Azure AD and Live ID acounts – all can be managed in this app. And I see all subscriptions in each account. Very nice.

So my main purpose to look into the app are virtual machines. To look at the status and start or stop the machines.

So I figured out these three possibilities to improve the app:

Show all subscriptions in one list, not in Azure AD accounts

I would find it useful, to have all subscriptions in one list – not only all subscriptions for each AAD account. So I do not have to search for a Subscription in one of my accounts.

Favourites for all subscriptions

I would prefere, to have Favourites over all subscriptions – like the recent list in Office. In the recent list in Office there are all Documents of all Microsoft accounts together in one recent-list. So I do not have to change the account and the subscription, to get to the favourites. I simply have them.

This could be an additional feature. Favourites on subscription level AND Favourites for all subscriptions.

Make Favourites available in portal.azure.com

Make the Favourites available and editable in the web browser on the Azure portal.

So these are my current suggestions to the Azure Mobile App.

Error while Editing SCSM Template

Today I got a somewhat strange error message in System Center Service Manager, as I wanted to delete an Activity in an Service Request Template.

This ist the message:
Date: 12/12/2018 9:08:49 AM
Application:
Application Version: 7.5.7487.0
Severity: Error
Message: 
: Verification failed with 1 errors:
-------------------------------------------------------
Error 1:
Found error in 1|zmQ_ISERequests|1.0.0.0|Offering6eb4772697264d14a1d6c91b83def062/GeneratedId_PresentationMappingTemplate0/GeneratedId_Sources0/GeneratedId_Source2/GeneratedId_Targets0/GeneratedId_Target1|4|RequestOffering/PresentationMappingTemplate/Sources/Source/Targets/Target with message:
Invalid Presentation Mapping Template. The Path attribute must point to a valid object within the object template.
-------------------------------------------------------
: Invalid Presentation Mapping Template. The Path attribute must point to a valid object within the object template.

Solution

So first, the Template is used in a Service Request. And in the Service Request one Input “Query Result” was configured to be added to Activity I wanted to delete. As I changed this configuration to an other Activity the error has gone.

First steps with VSCode (again)

Till now I did not use Visual Studio Code on a regular bases. Why? I always started using it for debugging my scripts, because I’ve been told, it is outstanding for debugging.
But it did not work for me. Each time something happend and I switched back to ISE.
Last week I open sourced my ISE profile – and than it happend, that I got a message from Tyler Leonhardt.

image

So now I simply have to have a better look on vs code. I did not have immediatly. Because now it is possible to position the powershell window on the right side (very important for me).

So I’ve had a look at Mike Robbins hints and the hints of Timothy Warner.

What I wanted

  • PowerShell like behavior – to start a script and code while the script runs
  • command window on the right side (for the second screen)
  • Nice icons
  • Autosave (I like that)
  • Wanted to try some autoformating

Now I started with the following mix:

The Extensions

  • Powershell (of course)
  • vscode-icons (nicer icons)
  • Markdown All In One (always good – with preview)
  • Azure Tools (I did not try this till now – but it looks impressing)

The Settings

I did a new mix of settings I like most. “powershell.integratedConsole.focusConsoleOnExecute” is the most relevant setting for me.

{
"workbench.iconTheme": "vscode-icons",
"window.zoomLevel": 0,
"editor.renderWhitespace": "all",
"editor.renderControlCharacters": true,
"files.trimTrailingWhitespace": true,
"files.defaultLanguage": "powershell",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 1000,
"editor.formatOnType": true,
"editor.formatOnPaste": true,
"powershell.integratedConsole.focusConsoleOnExecute": false,
"editor.mouseWheelZoom": true
}

Hope that helps you – this will be continued.

[Update 2018-10-30]
I got an advice, I’d like to mention here:
You may want to revise format on type format on paste and format on save. All good options but most of my issues resolve around those. I have reported them all. I use format on save only but i consider to disable it and even use format manually on request only.
See here
[Update 2018-11-21]
I followed the advice, because the VSCode broke really often. Now it is better.

[Update 2018-12-12 – Encoding]

The German Umlaute are mistyped in VSCode out of the box. For example when opening  a script, a ‘�’  appears instead of an ‘ä’.

The issue is closed on Github (see here). I tried the hint and set “files.autoGuessEncoding”: true – works for me now.

Did not set “files.encoding”: “windows1252” – because I never used this encoding before (till now it was UTF-8-BOM).

My favourite ISE profile

Currently I use Visual Studio code and PowerShell ISE at the same time.
I have had a nice ISE profile for a while now, which I’d like to introduce.

Welcome Message

Below you see the welcome message.

image

The welcome message includes:

  • actual User
  • actual Computer
  • Admin Modus
  • Datum / Uhrzeit

The Commandline

But the main advantage is in the command line itself. This looks like:

image

Here you can see at a glance:

  1. Time when the last command ended
  2. Execution time of last command humanized
  3. Admin mode (PS in red)
  4. PowerShell Provider (Green for FileSystem)

Where can you get it

The ISE Profile is on Github. https://github.com/pwahlmueller/ISE-Config