Trying to Enable PSRemoting on a List of Devices – Help Please

Welcome Forums General PowerShell Q&A Trying to Enable PSRemoting on a List of Devices – Help Please

  • This topic has 11 replies, 4 voices, and was last updated 3 weeks ago by
    Participant
    .
Viewing 7 reply threads
  • Author
    Posts
    • #283060
      Participant
      Topics: 8
      Replies: 23
      Points: 104
      Rank: Participant

      I am trying to enable PSRemoting on a list of devices running the following code/script.

      $Computers = Get-Content “C:\temp\list.txt”

      foreach($Computer in $Computers){
      psexec \\$Computer cmd.exe /c \\$Computers -s powershell Enable-PSRemoting -Force
      }

      When I run this I get the following output/error for each device in the list.

      PsExec v2.2 – Execute processes remotely
      Copyright (C) 2001-2016 Mark Russinovich
      Sysinternals – http://www.sysinternals.com

      psexec : The handle is invalid.
      At C:\Temp\EnablePSremoting.ps1:4 char:5
      + psexec \\$Computer cmd.exe /c \\$Computers -s powershell Enable-P …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : NotSpecified: (The handle is invalid.:String) [], RemoteException
      + FullyQualifiedErrorId : NativeCommandError

      Connecting to AMHRAOVDISD-008…
      Couldn’t access AMHRAOVDISD-008:
      Connecting to AMHRAOVDISD-008…

      The machine name (AMHRAOVDISD-008) it tries to connect to is replaced in each error with the machine on that line.

      Does anyone have any ideas on how I can enable PSRemoting on a list of machines in a txt file?

    • #283072
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Unable to test, how about this?

      psexec \\$Computer cmd.exe /c powershell -Command “& {Enable-PSRemoting -Force}”

      The user the script is running under must have admin on the remote system.

      • #283084
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        I tried the script using psexec \\$Computer cmd.exe /c powershell -Command “& {Enable-PSRemoting -Force}”

        and got the exact same results.

        I don’t know what it means when it says  (psexec : The handle is invalid.)

        it feels like it does not like something about the psexec command.

    • #283087
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Possibly a permissions issue. I just tried the command I posted with no issues. I trust the list.txt has hosts listed one per line?

      Also, have a look here:

      https://stackoverflow.com/questions/13663167/psexec-the-handle-is-invalid-when-running-the-command-as-system-user

      And here near the bottom for PSExec:

      https://4sysops.com/wiki/enable-powershell-remoting/

       

      • #283099
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Yes there is only one machine name per line in the text file.

        As for permissions I use the same creds to RDP into the same machines and it works fine the account I am using it a domain account that is added to the local administrators group of all the devices in out Domain.

        So I am not sure why it would not work due to permissions.

    • #283105
      Participant
      Topics: 5
      Replies: 10
      Points: 93
      Rank: Member

      Are they ping-able? Have you tried using IP instead?

      I had similar situation working remotely and had to flush dns cache.

      • #283120
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        Hi Obijuan,

        They are all pingable with the host names.  I have another script to ping the devices to check or should I say test the connection before I ever run this script to enable PSRemoting.  All are online and as I stated before from my computer I can use RDP with the machine name and connect and log into each device if I tried them all I have connected to about 8 of the devices successfully.  I have not however tried to enable PSRemoting by using their IP addresses I suppose it is worth a shot.  I will do that in the morning and see what happens with a few.

      • #283234
        Participant
        Topics: 8
        Replies: 23
        Points: 104
        Rank: Participant

        OK so I just tried it on one of the devices here are the steps I took

        1. I pinged the machine to ensure it was online successfully
        2. obtained the IP address from the successful ping
        3. Logged into the device to ensure I was able to connect with the creds being used.
        4. deleted all the NETBIOS names from the text file and replaced them with the single IP address
        5. saved the text file and opened it back up to be sure it was properly saved with a single IP address
        6. Closed the text file and ran the script again

        below is the output I received in the PS console as a result of running the script using IP address in stead of NETBIOS name…

        PS C:\Users\raf03576> C:\Temp\EnablePSremoting.ps1

        PsExec v2.2 – Execute processes remotely
        Copyright (C) 2001-2016 Mark Russinovich
        Sysinternals – http://www.sysinternals.com

        psexec : The handle is invalid.
        At C:\Temp\EnablePSremoting.ps1:4 char:5
        + psexec \\$Computer cmd.exe /c powershell -Command “& {Enable-PSRe …
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : NotSpecified: (The handle is invalid.:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError

        Connecting to 172.16.94.93…
        Couldn’t access 172.16.94.93:
        Connecting to 172.16.94.93…

         

        Does anyone have a script that they have successfully used to enable PSRemoting on Windows devices on their network that I can use as a template?

        My problem is when I try to install software via PowerShell remotely it fails with an error about WSMAN not being enabled so I try to enable PSRemoting which I was instructed to do and get this error of psexec : The handle is invalid.

    • #283282
      Participant
      Topics: 10
      Replies: 203
      Points: 996
      Helping Hand
      Rank: Major Contributor

      Did you try the example given near the end of this article using PSExec ?? (as stated previously)

      https://4sysops.com/wiki/enable-powershell-remoting/

    • #283285
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

      I believe this is a PSExec issue, probably permissions related. Couple of questions is the computer your are running the script from on the same domain?  If so, are you using domain account that is in the admin group?

      Another option you might try is to just use the Get-Service cmdlet to start the WinRM service on the remote computers.  That cmdlet does not require Windows PowerShell remoting to function remotely.

      Barring any firewall issues or group policy, once the WinRM service is running I think you should be able to do PowerShell remoting.

    • #283330
      Participant
      Topics: 8
      Replies: 23
      Points: 104
      Rank: Participant

      PS C:\> $Username = ‘xxxxxxxxxx’
      $Password = ‘xxxxxxxxxx’
      $pass = ConvertTo-SecureString -AsPlainText $Password -Force
      $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
      $Computers = get-content -path “C:\Temp\list.txt”
      foreach ($Computer in $Computers) {Start-Service -Name WinRM -Verbose}

       

      Error:
      VERBOSE: Performing the operation “Start-Service” on target “Windows Remote Management (WS-Management) (WinR
      M)”.
      Start-Service : Service ‘Windows Remote Management (WS-Management) (WinRM)’ cannot be started due to the
      following error: Cannot open WinRM service on computer ‘.’.
      At line:6 char:40
      + … ach ($Computer in $Computers) {Start-Service -Name WinRM -Verbose}
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Star
      t-Service], ServiceCommandException
      + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand

      PS C:\>

    • #283342
      Participant
      Topics: 5
      Replies: 253
      Points: 1,007
      Helping Hand
      Rank: Community Hero

      You can’t start the service remotely with just Start-Service because that cmdlet doesn’t have a computername parameter.  You need to do it just like my last post (use Get-Service and pipe to Start-Service).  There is no need to create a credential object either because Get-Service remoting will be based on the credentials you are running PowerShell locally.  If you need to use different credentials, start PowerShell with the runas utility and the /netonly switch.

      If you still get an error, please post it and I’ll see if I can help.

      If you need to run with different credentials (use the runas utility), open cmd.exe and do this to open PowerShell or the ISE (whichever one you want to use) and then run the commands above in the new console.

      or for the ISE

       

      • This reply was modified 3 weeks ago by Mike R..
Viewing 7 reply threads
  • You must be logged in to reply to this topic.