none
Starting a service remotely for a list of computers RRS feed

  • Question

  • I need to start the remote registry service on lots of computers, so thought I could script/batch it somehow to try to do them all at once, rather than going one by one.  Went back to batch since I'm more familiar with it than Powershell unfortunately still.  Gave it a shot via Powershell but it didn't appear to work, so tried batching the sc command.

    But the only bad way about the following approach, is it pops up a window per machine, and i can't see the results so can't tell if it started or failed on each machine.  I'm not sure how to capture the output to a file perhaps, or honestly if this is super easy in powershell and i just don't know how properly.   Was hoping someone here may have some finer points to make this a little better.  

    for /f "delims=" %%i in (C:\StartRemoteReg.txt) do (
      start c:\psexec \\%%i sc start remoteregistry
    )

    • Moved by Bill_Stewart Wednesday, September 4, 2019 9:16 PM This is not "scripts on demand"
    Thursday, April 11, 2019 5:54 PM

All replies

  • SC does not require PsExec.

    SC` \\%%i start remoteregistry


    \_(ツ)_/

    Thursday, April 11, 2019 6:20 PM
  • good catch!

    can you think of any other tweaks i should make?

    Thursday, April 11, 2019 7:52 PM
  • Yes.  Use PowerShell. Batch scripts are going to disappear shortly.

    Get-Content C:\StartRemoteReg.txt | 
          ForEach{Start-Service remoteregistry -Computer $_ }

    OR

    $computers Get-Content C:\StartRemoteReg.txt
    Start-Service remoteregistry -Computer $computers


    \_(ツ)_/


    • Edited by jrv Thursday, April 11, 2019 8:01 PM
    Thursday, April 11, 2019 8:00 PM
  • I'm unable to find a -Computer parameter for Start-Service in the online documentation.

    Thursday, April 11, 2019 8:24 PM
  • function Enable-RemoteRegistry
    {
    
    <#
    
    .SYNOPSIS
    
        Enable the Remote Registry service
    
    .DESCRIPTION
    
        Enable the Remote Registry service on local or remote computer using WMI
    
    .PARAMETER ComputerName
    
        A single computer name or an array of computer names. The default is localhost ($env:COMPUTERNAME).
    
    .EXAMPLE
    
        Enable-RemoteRegistry 'COMPUTER1'
    
    .EXAMPLE
    
        'COMPUTER1', 'COMPUTER2' | Enable-RemoteRegistry
    
    #>
    
        [CmdletBinding()]
        param(
            [Parameter(
                Position = 0,
                ValueFromPipeline = $true,
                ValueFromPipelineByPropertyName = $true
            )]
            [Alias("CN", "Computer")]
            [String[]]
            $ComputerName = $env:COMPUTERNAME
        )
    
        process
        {
            foreach ($Computer in $ComputerName)
            {
                try
                {
                    $WmiParams = @{
                        Class        = 'Win32_Service'
                        Filter       = 'Name="RemoteRegistry"'
                        ComputerName = $Computer
                        ErrorAction  = 'Stop'
                    }
    
                    $Service = Get-WmiObject @WmiParams
                    
                    if ($Service.StartMode -in 'Disabled', 'Manual')
                    {
                        $Service.ChangeStartMode('Automatic') | Out-Null
                    }
    
                    if ($Service.State -eq 'Stopped')
                    {
                        $Service.StartService() | Out-Null
                    }
    
                    $Service.Dispose()
                    $Service = Get-WmiObject @WmiParams
    
                    [PSCustomObject] @{
                        Computer  = $Computer
                        Service   = $Service.DisplayName
                        StartMode = $Service.StartMode
                        State     = $Service.State
                        Status    = $Service.Status
                    }
                } 
                
                catch
                {
                    Write-Warning "$Computer`: $_"
                }
            }
        }
    }
    

    Thursday, April 11, 2019 8:27 PM
  • I'm unable to find a -Computer parameter for Start-Service in the online documentation.

    Sorry.  I typed too fast.

    Get-Content C:\StartRemoteReg.txt | 
          ForEach{
               Get-Service remoteregistry -Computer $_  |
                   Start-Service
          }

    OR

    $computers Get-Content C:\StartRemoteReg.txt
    Get-Service remoteregistry -Computer $computers | Start-Service



    \_(ツ)_/

    Thursday, April 11, 2019 8:30 PM
  • Thanks to both of you for the replies.  Are there advantages to using one of your solutions over the other?  or does one work in certain situations better than another?
    Sunday, April 14, 2019 9:21 PM
  • Thanks to both of you for the replies.  Are there advantages to using one of your solutions over the other?  or does one work in certain situations better than another?

    If remoting is not required then you should not use it.  It just slows things down and adds unnecessary complexity with no advantages.


    \_(ツ)_/

    Sunday, April 14, 2019 10:28 PM
  • I'm sorry, I don't quite understand what you're getting at there, could you explain more jrv?
    Sunday, April 14, 2019 10:33 PM
  • I'm sorry, I don't quite understand what you're getting at there, could you explain more jrv?

    It is very clear.  What is not clear?


    \_(ツ)_/

    Monday, April 15, 2019 1:02 AM
  • What script are you talking about? Which part of the script?  do you mean "remoting" as part of the code, remoting as in doing this against remote machines, or something else?
    Monday, April 15, 2019 8:25 PM
  • What script are you talking about? Which part of the script?  do you mean "remoting" as part of the code, remoting as in doing this against remote machines, or something else?

    "remoting" usually refers to WinRM.  It is an industry standard for establishing secure remote sessions.

    Native PS CmdLets can connect remotely by various methods that can include WinRM but are not always WinRM.  WinRM is a bit heavy when creating a connection and other methods like WMI/DCOM, SMB/Shares, RSAT and remote registry are direct and do not require installation and configuration of WinRM.

    You have to learn about how Windows has allowed us to remotely mange most aspects of Windows before WMF and WinRM. 

    Yes we can call any method "remote" such as accessing a file remotely.  Mostly, in PowerShell, "remoting" refers to WinRM.

    A simple example.  "Get-WmiObject" uses DCOM.  "Get-CimInstance" can use either DCOM or WinRM.  It will use the most secure and convenient method unless optioned otherwise. "Invoke-Command" always uses WinRM.

    Does this help to sort this out for you?


    \_(ツ)_/

    Monday, April 15, 2019 8:57 PM
  • Crystal clear now, thanks for the in-depth explanation.
    Tuesday, April 16, 2019 1:53 PM