1

I'm trying to enable-psremoting with PSexec on my servers with the following command:

psexec.exe \\server cmd /c "echo . | powershell (-verb runas -argumentlist (enable-psremoting -force))"

but it doesn't work. I'm guessing I'm messing up my double quotes. Any help?

Sune:)

Sune
  • 3,080
  • 16
  • 53
  • 64
  • 1
    "it doesn't work" doesn't work. :) You need to provide specific information about what "it doesn't work" means, including the *exact* error message you're receiving if you're getting one. Please remember that we can't read your screen (or your mind) from here, so the only thing we have to go on is what you tell us in your question. Please edit your post and provide more specific information, so that someone here can try and help you. Thanks. :) – Ken White Jan 11 '12 at 20:17
  • Are you running psexec.exe from PowerShell or cmd.exe? – Andy Arismendi Jan 11 '12 at 20:34

3 Answers3

1

Thanks for commenting all! I found out how to do it, and this is the completed code:

$user = "youruser"
$p = Read-Host "Enter domain password for $adminuser"
cls

$expression1 = "enable-psremoting -force"
$commandBytes1 = [System.Text.Encoding]::Unicode.GetBytes($expression1)
$encodedCommand1 = [Convert]::ToBase64String($commandBytes1)

$expression2 = "Set-ExecutionPolicy remotesigned -Force”
$commandBytes2 = [System.Text.Encoding]::Unicode.GetBytes($expression2)
$encodedCommand2 = [Convert]::ToBase64String($commandBytes2)

$expression3 = "Restart-Service winrm”
$commandBytes3 = [System.Text.Encoding]::Unicode.GetBytes($expression3)
$encodedCommand3 = [Convert]::ToBase64String($commandBytes3)

foreach ($server in (get-content c:\temp\enablepsremotinglist.txt))
{
    echo " "
    echo "Running on $server"   
    echo "--------------------------------------- "
    echo " "    
    psexec.exe \\$server -h -u no\$user -p $p cmd /c "echo . | powershell -EncodedCommand $encodedCommand1"
    psexec.exe \\$server -h -u no\$user -p $p cmd /c "echo . | powershell -EncodedCommand $encodedCommand2"
    psexec.exe \\$server -h -u no\$user -p $p cmd /c "echo . | powershell -EncodedCommand $encodedCommand3"
}

I hope this can be of help to someone else one day:) PS: Please keep in mind that this send your adminpassword as clear text..

Sune
  • 3,080
  • 16
  • 53
  • 64
0

It looks like you are trying to invoke PowerShell to run elevated. This might not be possible to do remotely... I was able to get this to work against a machine without UAC enabled (2003 server):

$c = Get-Credential
$u = $c.UserName
$p = $c.GetNetworkCredential().Password

$path = "C:\SysinternalsSuite"
& "$path\psexec.exe" \\server -u $u -p $p powershell.exe -Command "Enable-PSRemoting -Force"

For some reason though I had to press enter a couple times on the shell for it to keep spitting out output and eventually return me to a prompt. Not sure what's up with that...

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
0

You don't need PSExec for that. Check this script by PowerShell developer Lee.

http://poshcode.org/2141

ravikanth
  • 24,922
  • 4
  • 60
  • 60