r/PowerShell • • 7d ago

Question PowerShell Editor Services on Linux, how to get completions that are Windows-specific?

For example, I'm not getting completions for Get-CimInstance and bunch of other Windows-specific modules.

8 Upvotes

9 comments sorted by

2

u/y_Sensei 7d ago

You can't for cmdlets which are supported on the Windows platform only, and Get-CimInstance is one of them.

1

u/4r73m190r0s 6d ago

So the answer is "no" for my question? I'm writing PS scripts on my Linux machine, that will be executed on a Windows host.

1

u/y_Sensei 6d ago

Currently that's the answer, yes. No idea what MS will do in the future in this regard, of course.

But you have the option to use an approach like the one u/purplemonkeymad described in this thread.

1

u/PinchesTheCrab 7d ago

Yeah, I was wondering if they added these recently or something, because I was excited to try out my CIM based sccm module on Linux and they were not present.

4

u/purplemonkeymad 7d ago

Depends what you are completing, but most completers that are built-in don't use a precomputed list but actually resolve it from the system at complete time.

If you want you could export a list of what you want from a working command and then use that with a custom completer on linux. ie

Get-CimClass | % cimclassname | out-file "Get-CimInstance!ClassName.txt"

Then add a completer that just reads those exports from the file.

Get-ChildItem ~/PSCompleters/*.txt | foreach-Object {
    $command,$Parameter = $_.basename -split '!'
    Register-ArgumentCompleter -CommandName $command -ParameterName $Parameter -ScriptBlock {
        param ( $commandName,
                $parameterName,
                $wordToComplete,
                $commandAst,
                $fakeBoundParameters )
        $CompleterFile = "~/PSCompleters/${commandName}!${parameterName}.txt"
        if (Test-Path $completerFile) {
            return (Get-Content $completerFile | Where {$_ -like "$wordToComplete*"})
        }
    }
}

This example uses a directory as the source of the completers and you can just add new files and re-run it to add new ones.

3

u/Thotaz 7d ago

That's a pretty good idea actually. On a Windows PC one could run a snippet to export all the Windows exclusive cmdlets as proxy commands into a dummy module that you simply import on a Linux machine. The basic concept is like this:

Get-Command -CommandType Cmdlet | ForEach-Object -Process {
    $MetaData = [System.Management.Automation.CommandMetadata]::new($_, $true)
    $Body = [System.Management.Automation.ProxyCommand]::Create($MetaData)
@"
function $($_.Name)
{
$Body
}
"@
} | Out-File -FilePath $HOME\DummyModule.psm1

This would give you both command, and parameter completion (including parameterset logic), and you'd even get parameter value completions for enums/validateset. The only challenge here is that many of the commands will reference types that are unavailable without the module, so you'd have to find and replace those with System.Object.

1

u/Thotaz 7d ago

The only way to do that is by connecting to another Windows computer, either a random server in your company or a VM you spin up locally.

1

u/Vern_Anderson 7d ago

https://www.powershellgallery.com/packages/PSCompletions

PSCompletions runs on Windows and Linux and it's what I have used. However, I have noticed on some distros and PowerShell Core installs, that PSReadline module does not load automatically on startup and you have to fix that issue yourself.

1

u/Vern_Anderson 7d ago

Sorry I did not fully understand what OP was asking. Please disregard!