0

Good morning folks..

I have a (hopefully) quick question on how to do add a target to certain PowerShell commands that don't allow the "-Computername $Target" option.

Things like below... How would I be able to run my script I have that finds out a bunch of important info on a machine, and get info on a target machine:

    Write-Output "..Windows Product Key"
    $prodkey = (gwmi -ComputerName $Target -query ‘select * from SoftwareLicensingService’).OA3xOriginalProductKey
    Write-Output "..Local User Listings"
    $users = Get-LocalUser | Sort-Object Name
    Write-Output "..Group Memberships"
    $groups = Get-LocalGroup | Sort-Object Name
    Write-Output "..WSUS Server Configuration"
    $WSUSServer = (Get-ItemProperty -Path HKLM:\Software\Policies\Microsoft\Windows\WindowsUpdate).WUServer

As you can see, I utilize the $Target variable on things that I know will work like the gwmi and it works flawlessly. But other things I have listed above I can't figure out how to point them to another machine I am trying to run the script from.

For instance... I am running a PowerShell script from one of my domain controllers, because every vLAN in that specific environment is open from the domain controllers. So instead of having to log in to each and every single machine (over 3,000) individually, I can set up a text file with say 15 or 20 servers at a time, run my information script, and return the results all from the domain controller.

Thanks in advance for the assistance.

  • 2
    I commend to your attention [Microsoft Docs on script remoting](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/running-remote-commands) and on [remoting in general](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_remote) – Jeff Zeitlin Apr 27 '22 at 15:19
  • Thanks @JeffZeitlin. I was already trying Invoke-Command -Computername $Target {commands here}, as well as a $s=New-PSSession -Computername $Target and Enter-PSSession -Session $s, and nothing will work right when it gets down to where I need it to create the HTML report. Give me a bit and I'll edit my post above with the error I am seeing. – Charles Waters Apr 27 '22 at 16:26
  • 1
    `Invoke-Command` is definitely the answer here. Wouldn't see why `Invoke-Command -ComputerName $Target {commands here}` wouldn't work. – Abraham Zinala Apr 27 '22 at 16:58
  • As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Apr 27 '22 at 17:01
  • So here's the only things I can't get working: `Write-Output "..Group Memberships" $groups = Invoke-Command -ComputerName $Target {Get-LocalGroup | Sort-Object Name}`. Below in the HTML output, this just gives me errors. It will actually list all the groups on the remote server, but doesn't actually populate them. `$(echo $group.name)" $(Invoke-Command -ComputerName $Target {Get-LocalGroupMember -group $group.name | select Name,PrincipalSource,ObjectClass | ConvertTo-HTML})"` Other than this, it seems everything works. – Charles Waters Apr 27 '22 at 17:11

0 Answers0