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.
-
AuthorPosts
-
-
December 29, 2020 at 3:30 pm #283060
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.compsexec : 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 : NativeCommandErrorConnecting 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?
-
December 29, 2020 at 3:52 pm #283072
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.
-
December 29, 2020 at 4:04 pm #283084
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.
-
-
December 29, 2020 at 4:17 pm #283087
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:
And here near the bottom for PSExec:
https://4sysops.com/wiki/enable-powershell-remoting/
-
December 29, 2020 at 4:46 pm #283099
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.
-
-
December 29, 2020 at 5:37 pm #283105
Are they ping-able? Have you tried using IP instead?
I had similar situation working remotely and had to flush dns cache.
-
December 29, 2020 at 10:15 pm #283120
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.
-
December 30, 2020 at 8:34 am #283234
OK so I just tried it on one of the devices here are the steps I took
- I pinged the machine to ensure it was online successfully
- obtained the IP address from the successful ping
- Logged into the device to ensure I was able to connect with the creds being used.
- deleted all the NETBIOS names from the text file and replaced them with the single IP address
- saved the text file and opened it back up to be sure it was properly saved with a single IP address
- 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.compsexec : 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 : NativeCommandErrorConnecting 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.
-
-
December 30, 2020 at 11:12 am #283282
Did you try the example given near the end of this article using PSExec ?? (as stated previously)
-
December 30, 2020 at 11:14 am #283285
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.
PowerShell1Get-Service -Name WinRM -ComputerName $Computers | Start-ServiceBarring any firewall issues or group policy, once the WinRM service is running I think you should be able to do PowerShell remoting.
-
December 30, 2020 at 2:27 pm #283330
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.StartServiceCommandPS C:\>
-
This reply was modified 3 weeks ago by
othompson09.
-
This reply was modified 3 weeks ago by
-
December 30, 2020 at 2:40 pm #283342
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.
PowerShell12$Computers = Get-Content “C:\temp\list.txt”Get-Service -Name WinRM -ComputerName $Computers | Start-ServiceIf 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.
PowerShell1C:\Users\micha>runas /netonly /user:domain.com\administrator powershellor for the ISE
PowerShell1C:\Users\micha>runas /netonly /user:domain.com\administrator powershell_ise-
This reply was modified 3 weeks ago by
Mike R..
-
This reply was modified 3 weeks ago by
-
-
AuthorPosts
- You must be logged in to reply to this topic.