locked
Passing variable from PS to Batch file RRS feed

  • Question

  • I'm trying to get a script setup to remotely enable the WinRM service and then get PowerShell setup so it'll accept remote connections. I'm using PSExec for that second part, but I can't get it to work in PowerShell so I thought that maybe I could do the first part with PowerShell and then let a batch program take care of the rest with PSExec. Here's what I have so far:

    Param(
          [Parameter(Mandatory=$True)]
          [String]
          $ComputerName
          )
    
    Get-Service "WinRM" -ComputerName $ComputerName | Set-Service -StartupType Automatic | Start-Service
    
    Start-Process "C:\Users\alex.franco\Documents\Batch Programs\PSRemoting.bat"

    And the contents of the PSRemoting.bat:

    C:\PSTools\PSExec.exe \\$ComputerName -h -d powershell.exe "enable-psremoting -force"
    pause

    The issue is pulling the $ComputerName variable from PowerShell and using it in the batch file. Any suggestions how I could do this?

    Thanks!

    Friday, August 14, 2015 7:13 PM

Answers

All replies

  • Hi,

    There's already something in the script repository that does this:

    https://gallery.technet.microsoft.com/scriptcenter/Enable-PSRemoting-Remotely-6cedfcb0


    • Marked as answer by Alex.Franco Friday, August 14, 2015 7:18 PM
    Friday, August 14, 2015 7:16 PM
  • Whelp that pretty much takes care of that! Ha! Thanks 
    • Marked as answer by Alex.Franco Friday, August 14, 2015 7:18 PM
    • Unmarked as answer by Alex.Franco Friday, August 14, 2015 7:18 PM
    Friday, August 14, 2015 7:18 PM
  • Cheers, you're welcome.

    Friday, August 14, 2015 7:18 PM
  • I highly recommend and suggest that you should use Group Policy for this.  Izt takes only a couple of clicks and all machies in the OU or selected by the GP filter will be configured along with the firewall and host list.

    \_(ツ)_/

    • Proposed as answer by Mike Laughlin Friday, August 14, 2015 7:40 PM
    Friday, August 14, 2015 7:21 PM
  • I highly recommend and suggest that you should use Group Policy for this.  Izt takes only a couple of clicks and all machies in the OU or selected by the GP filter will be configured along with the firewall and host list.

    \_(ツ)_/

    Oh you and your good advice....

    http://www.briantist.com/how-to/powershell-remoting-group-policy/


    Friday, August 14, 2015 7:40 PM
  • Can't help it. It just works so well and is so easy to manage.  Why script when you can dictate.


    \_(ツ)_/

    Friday, August 14, 2015 7:41 PM
  • Believe me, I'd love to do that. It would make things a ton easier. Unfortunately I'm low on the totem pole here and the top guys aren't fans of PowerShell or using Group Policy for much of anything. Although they've been saying for a long time that Group Policy is something they should start using so maybe someday.. maybe.
    Friday, August 14, 2015 7:57 PM
  • If they are not using that of System Center then fire them.  This is the 21st century. We don't do sneaker-nets anymore.


    \_(ツ)_/

    Friday, August 14, 2015 8:05 PM