PowerShell Versions

Looking to write api events to the Windows Event log using powershell and looking for guidance on how to expose older powershell version cmdlets to the PSU api runspace. When i run the api i put a wait-debugger in it to troubleshoot why it was failing and noticed from within that runspace i only see Powershell 7 cmdlets which does not include the one i need. When i exit from the pshost process and look for the command it works fine on the server. I’ve run get-command from within the pshost process and outside for comparison.

Output:
IN PSHOST process:
[servername]: [Process:5920]: PS C:\Users\Server\Documents> Get-command event | where {_.commandType -like "Cmdlet" -and _.Name -like "event}
Cmdlet Get-Event 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Get-EventSubscriber 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Get-WinEvent 7.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet New-Event 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet New-WinEvent 7.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet Register-CimIndicationEvent 7.0.0.0 CimCmdlets
Cmdlet Register-EngineEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-ObjectEvent 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Remove-Event 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Unregister-Event 7.0.0.0 Microsoft.PowerShell.Utility
Cmdlet Wait-Event 7.0.0.0 Microsoft.PowerShell.Utility

From normal pssession remoting:
[servername]: PS C:\Users\Server\Documents> Get-command event | where {_.commandType -like "Cmdlet" -and _.Name -like "event}
Cmdlet Clear-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Get-Event 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Get-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Get-EventSubscriber 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Get-WinEvent 3.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet Limit-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet New-Event 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet New-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet New-WinEvent 3.0.0.0 Microsoft.PowerShell.Diagnostics
Cmdlet Register-CimIndicationEvent 1.0.0.0 CimCmdlets
Cmdlet Register-EngineEvent 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-ObjectEvent 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Register-WmiEvent 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Remove-Event 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Remove-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Show-EventLog 3.1.0.0 Microsoft.PowerShell.Management
Cmdlet Unregister-Event 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Wait-Event 3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet Write-EventLog 3.1.0.0 Microsoft.PowerShell.Management

Getting closer… i’ve found that if i issues the command:
powershell -version ‘5.1’ "Write-EventLog -LogName $LogName -Source $Source -EventId $EventId -Message $Message -EntryType $level

It seems to get past the error that it can’t find the cmdlet, but it’s always treating the variables as literals… ie… variable $LogName is “PSU Server”… the error i’m getting now is:
Write-EventLog : A positional parameter cannot be found that accepts argument ‘Server’. The “Server” it is referring to is in the second word in the variable $LogName

I can get it to print out the command before it executes it and it looks like this (which appears to be correct):
Write-EventLog -LogName “PSU Server” -source “PowerShell Universal Server” -EventId “60001” -Message “API Completed” -EntryType “Information”

I’ve tried $using , argumentlist, invoke-command, etc.

We need to add an option to set the API PowerShell version. That would make this much easier.

You may be able to install the WindowsCompatibility module for now to work around this:

They have some examples of using event log cmdlets on the readme.md.

Thanks Adam…much appreciated! I’ll give that a try…

Just to circle back… that worked like a charm on the first try after loading that module. THANKS!!!

1 Like

Spoke a little too soon… i upgraded to 1.3 and now the code that was working is generating the message:
Write-EventLog:
Line |
31 | Write-EventLog -LogName $LogName -Source $Source -EventId $EventId -M …
| ~~~~~~~~~~~~~~
| The term ‘Write-EventLog’ is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Thoughts?

Are you loading the module with Import-WinModule?

Import-WinModule Microsoft.PowerShell.Management

Another thing you could do if you aren’t using PS7 in universal is remove it from the powershellVersions.ps1 in %Appdata%\UniversalAutomation.universal. Then it should select Windows PowerShell.

1 Like

Thanks Adam… i rolled back to 1.2.9 and had the same issue. You were correct, i wasn’t using winmodule… i fixed in 1.2.9 and it worked, then upgraded to 1.3 and it stll worked. Sorry for the confusion and as always, thanks for the suggestions.

1 Like