Home > Net >  from PowerShell 5.1 to 7.2.5 Get-Service -ComputerName doesn't exist anymore, how do I get comp
from PowerShell 5.1 to 7.2.5 Get-Service -ComputerName doesn't exist anymore, how do I get comp

Time:09-02

I'm using Get-Service to show a filtered list and it worked fine in 5.1. This is how it worked in 5.1 for me:

Get-Service -DisplayName $displayName -ComputerName $computers | Sort-Object MachineName | format-table Name,Status,DisplayName,Machinename –autosize 

However, in 7.2.5 -ComputerName is no longer there.

CodePudding user response:

The -ComputerName parameters on purpose-specific cmdlets such as Get-Service, Get-Process and Restart-Computer only work in Windows PowerShell and aren't available in PowerShell (Core) 7 anymore, because they are based on .NET Remoting, a form of remoting unrelated to PowerShell that has been declared obsolete and is therefore not part of .NET Core / .NET 5 , which PowerShell (Core) is based on.

Thus, switch to using PowerShell's remoting, where general-purpose remoting cmdlets such as Invoke-Command cmdlet facilitate execution of arbitrary commands remotely, using a modern, firewall-friendly transport.

  • However, note that this requires all target computers to be set up for PowerShell remoting first.
  • Once they are, they can also be used with the CIM cmdlets (e.g., Get-CimInstance), the successors to the obsolete, also Windows PowerShell-only WMI cmdlets (e.g., Get-WmiObject) - see this answer.

Thus, assuming the target computers are set up for PowerShell remoting, the equivalent of your command is:

Invoke-Command -ComputerName $computers { 
  Get-Service -DisplayName $using:displayName 
} | 
  Sort-Object PSComputerName |
  Format-Table Name, Status, DisplayName, PSComputerName –AutoSize 

Note the use of the $using: scope to refer to the value of a variable from the caller's scope, and the use of the .PSComputerName property, which PowerShell's remoting infrastructure decorates all output objects with.

  • Related