0

I would need to run a powershell script to get services info on remote PCs. The script runs on MYPC, on the same subnet of the others. no domain, same subnet. local administrator for each PCs.

An example of what I wun in my script:

$ComputerName = "PC1","PC2","PC3","PC4","PC5"
$ServiceName = "Service1","Service2","Service3","Service4"

foreach ($Computer in $ComputerName)
    {
      foreach ($Service in $ServiceName)
      {
        #Get current service type (automatic/manual)
        $CurrentType = Get-Service -Name $Service -ComputerName $Computer | Where{$_.StartType -eq "Manual"}
...

But I got errors since before I have to remotely login the remote PC with administrator user.

Once loggedin, the script runs fine.

just to be clear, I login to the remote PC, simply typing, on MYPC:

\\PC1

hit then Enter

on the login window inserting then the local PC1 Admin credentials

As a workround, instead of manually inserting the commands to remotely login each PCS, I run a DOS script like this:

NET USE \\PC1\IPC$ adminpwd /USER:Administrator /PERSISTENT:YES
NET USE \\PC2\IPC$ adminpwd /USER:Administrator /PERSISTENT:YES
NET USE \\PC3\IPC$ adminpwd /USER:Administrator /PERSISTENT:YES
...
hanrp
  • 1
  • 1
  • Have a look at [PowerShell Remoting](https://learn.microsoft.com/en-us/powershell/scripting/learn/remoting/powershell-remoting-faq?view=powershell-7.2). In particular, the [Invoke-Command](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2) and [New-PsSession](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/new-pssession?view=powershell-7.2) cmdlets will be helpful. – boxdog Aug 05 '22 at 13:08

1 Answers1

0

I would highly recommend using Invoke-Command: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/invoke-command?view=powershell-7.2

This enables you to run powershell commands on remote machines (but PSRemoting needs to be enabled): https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/enable-psremoting?view=powershell-7.2

then you could do something like this:

$ComputerName = "PC1","PC2","PC3","PC4","PC5"
$ServiceName = "Service1","Service2","Service3","Service4"

foreach ($Computer in $ComputerName)
    {
  foreach ($Service in $ServiceName)
  {
    #Get current service type (automatic/manual)
      Invoke-Command -ComputerName $computer -ScriptBlock {param($svc) Get-Service -Name $svc -ComputerName $Computer | Where{$_.StartType -eq "Manual"}} -ArgumentList $Service
}
}

Invoke-command in a loop: Powershell - Using invoke-command in a for loop

Named params in invoke command: How do I pass named parameters with Invoke-Command?

If needed you can also add credentials to connect using:

[-Credential ]

as described in powershell documentation above.