Is it possible to create Windows PowerShell Cmdlets with F#? - powershell

Reading the Microsoft documentation for creating PowerShell Cmdlets, I notice there is no example code for F#; instead, there is a message saying that "This language is not supported or no code example is available.".
Is F# not supported for creating Cmdlets, or aren't there any examples in F#?

Take a look at this:
Writing PowerShell Cmdlets in F#. Not exactly a complete example of how to do this but it certainly seems to be possible to write cmdlets in F#.
EDIT:
There is a more extensive example here but the contextual information is a little bit out of date. Actually the contextual information is a bit out of date on both links but the first link is more recent.

Related

Intellisense for PowerShell splatting in vscode?

My understanding is that splatting variables is the preferred/recommended way to make longer function calls in PowerShell scripts. However, I use vscode as my primary IDE and understandably, extracting the parameters into a hashtable and splatting them makes intellisense unusable.
Is there any library/framework/vscode extension for splatting that allows the use of intellisense by way of naming convention or something like that?
I know this is a relatively old question but whilst looking for similar I came across Editor Services Command Suite which looks like it may be useful. It allows you to write the command out and then convert it to a splatted version:
ESCS github repo
It was this blog post by Rob Sewell (sqldbawithabeard) which brought it to my attention: blog post

Making all PowerShell script variables explicitly Typed

I'm new to PowerShell scripting and am looking to create ps1 scripts that I can used as cmdlets. My background is in using strongly typed variables, but I'm struggling to find how (or if) it is possible to ensure that all user variables in a script are explicitly typed. Some languages only allow explicitly typed variables. VBA allows the directive "Option Explicit" and I was hoping to find some way to achieve the same in any PSH scripts I create.
I've done a lot of searching (google, stackoverflow etc.) but not found anything. If there is no way to force all variable definitions to be explicitly typed, I'll have to write a cmdlet to parse my scripts to find any implicitly typed variables ... but hoping for a better solution.
I don't think you can do that in PowerShell. Closest you can get is to use Set-StrictMode which will, among other things, prohibit use of uninitialized variables.
But if you want to parse the scripts, maybe don't write your own solution. Use PSScriptAnalyzer module. It has a lot of built-in rules, unfortunately none for checking explicit types. But you can define your own rules, and maybe someone already created the one you're looking for and posted it somewhere.

What is the equivilent of whatis in powershell

I have learned the basics of using UNIX but I am now trying to learn how to use powershell. Most of the commands are similar or easy to find the equivalent of. However, I have not found the equivalent of whatis. Is there an equivalent? I have tried searching online but the search engines take "whatis" as being "what is". Is there anybody that knows what the equivalent is or if there is an equvalent at all?
use Get-Help to display help about cmdlets and concepts. It is your best friend when you can't remember commands
Get-Help <cmdlet>
I would also suggest Get-Alias to see which UNIX commands map to PowerShell cmdlets
You can even create you own aliases. You can add whatis to map to get help.
New-Alias whatis Get-Help
which would then allow you to do
whatis <cmdlet>
For good measure, the second best command to get familiar with is
Get-Command
Using Get-Command along side Get-Help will help (pun intended) you get up to speed with PowerShell. I do suggest using a filter with Get-Commad as the list tends to be very long. It allows wildcard filters
Get-Command *-command

What the Cmdlet Development Guidelines say about case use for functions in Cmdlets?

While I did find a lot of information on how to name Cmdlets and functions in the Cmdlet Development Guidelines I did not find any information on whether functions should be named in upper or in lower case.
What is the convention here?
(I do understand that Cmdlets themselves are generally named in upper case, even though they are not case-sensitive when it comes to executing.)
Naming convention can be tricky. While a fixed naming convention may provide aesthetics or simplified usage, it is not required to be followed. In general, a naming convention that I advocate for is the one that is used already in Powershell. As functions are created on verb-noun base, each word starts with a capital letter or if it is an abbreviation - all capitals, or if it is a proprietary - then as it is accordingly.
I have, for example, created some functions for myself:
Get-ServerDiag
Mount-TrueCryptVolumes
Start-RDP
Generate-RandomPassword
Nuke-Environment
You can imagine what these functions do, it is rather clear, straightforward and compliant with built-in Powershell functions. I do however have exceptions which come from "importing" a several Unix commands to Powershell (like killall, pidof etc...) You can always use a Set-Alias if you prefer to write something else.
This question, however important, is discussable as there does not seem to be the 'one, best way'. It is all, in the end, up to personal preferences.
A function is mostly a script-based cmdlet. A cmdlet is written in ex. C#, while a function is written as a script. Because of this similarity, I recommend using the same style as cmdlets for my "standalone-functions" so that it blends in with the other PowerShell cmdlets. Ex. I had a filecount(per folder)-function that I used often. I called it Get-FileCount.
However, I usually name helper functions(functions you only use in other functions) using a simpler name like convertsidtousername etc.
You could use aliases to create short names for a function.

What is a PowerShell cmdlet?

Approaching cmdlets in a conceptual way,
How are they made? Are they compiled?
Is it the equivalent of a batch file for PowerShell? Is it a script or a binary?
What is the structure used for storing these cmdlets?
A PowerShell cmdlet is a compiled piece of .NET code, more precisely a single class if I am not mistaken. Cmdlets are kind of the "native" commands in PowerShell land, being able to handle object input and output as well as usually playing nice and well with the (object-based) pipeline.
Cmdlets have no direct representation in the file system, as they are not programs or similar. They exist solely within PowerShell. You can use the Get-Command cmdlet to query all available cmdlets, functions, etc.
You can write cmdlets with a .NET language, such as C#. With PowerShell v2 there is also the possibility to write so-called advanced functions which behave similarly to cmdlets and have comparable capabilities but are interpreted PowerShell code, instead of compiled classes. This may incur a run-time overhead.
This link may help in understanding powershell cmdlet:
http://www.powershellpro.com/powershell-tutorial-introduction/tutorial-powershell-cmdlet/
See Scripting with Windows PowerShell.
A PowerShell cmdlet is a user-created extension to the PowerShell scripting language. The Cmdlet itself is a .NET class extending from PSCmdlet. Usually, additional components are included with the cmdlet to provide help and registering the cmdlet.
A cmdlet allows you to access to all functions accessible through the .NET virtual machine. This can range from simple script aids to fully functional programs.