PoshCode Logo PowerShell Code Repository

Enable-RemotePsRemoting. by Lee Holmes 4 years ago
embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2141"></script>download | new post

From Windows PowerShell Cookbook (O’Reilly) by Lee Holmes

  1. ##############################################################################
  2. ##
  3. ## Enable-RemotePsRemoting
  4. ##
  5. ## From Windows PowerShell Cookbook (O'Reilly)
  6. ## by Lee Holmes (http://www.leeholmes.com/guide)
  7. ##
  8. ##############################################################################
  9.  
  10. <#
  11.  
  12. .SYNOPSIS
  13.  
  14. Enables PowerShell Remoting on a remote computer. Requires that the machine
  15. responds to WMI requests, and that its operating system is Windows Vista or
  16. later.
  17.  
  18. .EXAMPLE
  19.  
  20. Enable-RemotePsRemoting <Computer>
  21.  
  22. #>
  23.  
  24. param(
  25.     ## The computer on which to enable remoting
  26.     $Computername,
  27.  
  28.     ## The credential to use when connecting
  29.     $Credential = (Get-Credential)
  30. )
  31.  
  32. Set-StrictMode -Version Latest
  33. $VerbosePreference = "Continue"
  34.  
  35. $credential = Get-Credential $credential
  36. $username = $credential.Username
  37. $password = $credential.GetNetworkCredential().Password
  38.  
  39. $script = @"
  40.  
  41. `$log = Join-Path `$env:TEMP Enable-RemotePsRemoting.output.txt
  42. Remove-Item -Force `$log -ErrorAction SilentlyContinue
  43. Start-Transcript -Path `$log
  44.  
  45. ## Create a task that will run with full network privileges.
  46. ## In this task, we call Enable-PsRemoting
  47. schtasks /CREATE /TN 'Enable Remoting' /SC WEEKLY /RL HIGHEST ``
  48.    /RU $username /RP $password ``
  49.    /TR "powershell -noprofile -command Enable-PsRemoting -Force" /F |
  50.    Out-String
  51. schtasks /RUN /TN 'Enable Remoting' | Out-String
  52.  
  53. `$securePass = ConvertTo-SecureString $password -AsPlainText -Force
  54. `$credential =
  55.    New-Object Management.Automation.PsCredential $username,`$securepass
  56.  
  57. ## Wait for the remoting changes to come into effect
  58. for(`$count = 1; `$count -le 10; `$count++)
  59. {
  60.    `$output = Invoke-Command localhost { 1 } -Cred `$credential ``
  61.        -ErrorAction SilentlyContinue
  62.    if(`$output -eq 1) { break; }
  63.  
  64.    "Attempt `$count : Not ready yet."
  65.    Sleep 5
  66. }
  67.  
  68. ## Delete the temporary task
  69. schtasks /DELETE /TN 'Enable Remoting' /F | Out-String
  70. Stop-Transcript
  71.  
  72. "@
  73.  
  74. $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($script)
  75. $encoded = [Convert]::ToBase64String($commandBytes)
  76.  
  77. Write-Verbose "Configuring $computername"
  78. $command = "powershell -NoProfile -EncodedCommand $encoded"
  79. $null = Invoke-WmiMethod -Computer $computername -Credential $credential `
  80.     Win32_Process Create -Args $command
  81.  
  82. Write-Verbose "Testing connection"
  83. Invoke-Command $computername {
  84.     Get-WmiObject Win32_ComputerSystem } -Credential $credential

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me