-1

I'm currently trying to remote to multiple servers that are on PowerShell 4.0 whereas the local computer currently runs on 5.0+. I am attempting to query servers that are Server 2016 for the Local Administrator group, but will fail for servers that are on 2012. Which upon researching the error messages for cmdlets from Microsoft.Powershell.LocalAccounts indicates that the modules don't exist for 2012.

Rather than updating the PowerShell version, or installing the modules to each server, I wondering if it's possible to execute cmdlets from the local 2016 into remote sessions of the 2012 servers.

codewario
  • 19,553
  • 20
  • 90
  • 159
DanLo
  • 21
  • 1
  • It's ultimately probably easier to call .NET APIs directly instead of trying to make newer modules work on machines with older PS versions (you'd have to copy these newer modules there, but even then there's no guarantee that they'll work). – mklement0 Oct 12 '21 at 02:50
  • I assume .NET APIs may have been referring to the command I used in my answer, correct? – DanLo Oct 12 '21 at 18:18
  • 1
    No, I meant actual .NET APIs, which PowerShell allows you to use, which are unrelated to standard utilities (external programs) such as `net.exe`. Pragmatically speaking, as in your case, using such utilities may be sufficient, which can make for a simpler solution. In general, however, using PowerShell cmdlets and, as a fallback, .NET APIs directly is preferable due to their object-oriented nature, which obviates the need to _parse text output_, which is both cumbersome and brittle. – mklement0 Oct 12 '21 at 18:40

2 Answers2

1

Technically, yes, you could copy modules from the local server to one of the module directories on the remote server's $env:PSModulePath and import them in the remote session, but as @mklement0 stated there is no guarantee current modules will work with PowerShell 4.0, let alone Server 2012.

That said, Microsoft.PowerShell.LocalAccounts is a module provided by PowerShell 5.1, not the OS, and the features should still work on 2012. However, I don't have a 2012 instance I can test with. But you may be able to get your task done by upgrading your servers' PowerShell version to PowerShell 5.1 (Windows Management Framework 5.1) to leverage the additional features and built-in modules this version brings. But any modules that are shipped with later Windows versions and not PowerShell itself will remain unavailable.

codewario
  • 19,553
  • 20
  • 90
  • 159
1

$localAdminUsers = net localgroup administrators

After some talking with a colleague, I realized I had overcomplicated my script, when I could have used the command prompt. From there, I parse the string for the users I was looking for.

Thank you for your responses.

mklement0
  • 382,024
  • 64
  • 607
  • 775
DanLo
  • 21
  • 1
  • Glad you found a solution that worked for you. Outside of this I would still recommend upgrading servers to WMF 5.1 as this gets you the latest supported version of Windows PowerShell, and there are many improvements and fixes to it over 4.0. Not to mention module compatibility skyrockets :) – codewario Oct 15 '21 at 13:50