Link to home
Start Free TrialLog in
Avatar of CuriousMAUser
CuriousMAUser

asked on

How do you run the PowerShell command - Enable-PSRemoting - globally on the desktop machines?

Hi Experts,

I have a script to gather Administrator information from each Windows 7 desktop or Windows 2008 server but I'm not sure how to run the command 'Enable-PSRemoting' globally so the script run successfully and doesn't display the 'RPC is Unavailable' error.

Thank you,
Avatar of McKnife
McKnife
Flag of Germany image

Hi.

I'm quite sure there's a way with GPOs. Did you drill through the gpo reference, yet? http://www.microsoft.com/en-us/download/details.aspx?id=25250
ASKER CERTIFIED SOLUTION
Avatar of Steve Whitcher
Steve Whitcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CuriousMAUser
CuriousMAUser

ASKER

Hi Steve,

Thank you. The final comment states an important remote command, 'winrm quickconfig -q'  which needs to execute manually on every remote desktop once. Wow. Fortunately, we only have 708 desktops, the Anonymous response has 5000 systems. Yikes.

Thank you, again.
Tom
I wasn't at my desk yesterday, so I couldn't dig up this reference.  I thought I'd throw it out there this morning as another option.  Lee Holmes offers a script in his book "Windows Powershell Cookbook" which uses WMI to remotely enable powershell remoting.  The Powershell Cookbook module is available online and includes this script as a function "Enable-RemotePSRemoting".  Here is the script itself, if you don't want to get the module for some reason:

    ##############################################################################
    ##
    ## Enable-RemotePsRemoting
    ##
    ## From Windows PowerShell Cookbook (O'Reilly)
    ## by Lee Holmes (http://www.leeholmes.com/guide)
    ##
    ##############################################################################
    
    <#
    
    .SYNOPSIS
    
    Enables PowerShell Remoting on a remote computer. Requires that the machine
    responds to WMI requests, and that its operating system is Windows Vista or
    later.
    
    .EXAMPLE
    
    PS > Enable-RemotePsRemoting <Computer>
    
    #>
    
    param(
        ## The computer on which to enable remoting
        $Computername,
    
        ## The credential to use when connecting
        $Credential = (Get-Credential)
    )
    
    Set-StrictMode -Version 3
    
    $VerbosePreference = "Continue"
    
    $credential = Get-Credential $credential
    $username = $credential.Username
    $password = $credential.GetNetworkCredential().Password
    
    $script = @"
    
    `$log = Join-Path `$env:TEMP Enable-RemotePsRemoting.output.txt
    Remove-Item -Force `$log -ErrorAction SilentlyContinue
    Start-Transcript -Path `$log
    
    ## Create a task that will run with full network privileges.
    ## In this task, we call Enable-PsRemoting
    schtasks /CREATE /TN 'Enable Remoting' /SC WEEKLY /RL HIGHEST ``
        /RU $username /RP $password ``
        /TR "powershell -noprofile -command Enable-PsRemoting -Force" /F |
        Out-String
    schtasks /RUN /TN 'Enable Remoting' | Out-String
    
    `$securePass = ConvertTo-SecureString $password -AsPlainText -Force
    `$credential =
        New-Object Management.Automation.PsCredential $username,`$securepass
    
    ## Wait for the remoting changes to come into effect
    for(`$count = 1; `$count -le 10; `$count++)
    {
        `$output = Invoke-Command localhost { 1 } -Cred `$credential ``
            -ErrorAction SilentlyContinue
        if(`$output -eq 1) { break; }
    
        "Attempt `$count : Not ready yet."
        Sleep 5
    }
    
    ## Delete the temporary task
    schtasks /DELETE /TN 'Enable Remoting' /F | Out-String
    Stop-Transcript
    
"@
    
    $commandBytes = [System.Text.Encoding]::Unicode.GetBytes($script)
    $encoded = [Convert]::ToBase64String($commandBytes)
    
    Write-Verbose "Configuring $computername"
    $command = "powershell -NoProfile -EncodedCommand $encoded"
    $null = Invoke-WmiMethod -Computer $computername -Credential $credential `
        Win32_Process Create -Args $command
    
    Write-Verbose "Testing connection"
    Invoke-Command $computername {
        Get-WmiObject Win32_ComputerSystem } -Credential $credential

Open in new window

Thank you, Steve.

Three books I have which I'm still digesting are:
PowerShell Deep Dives - Jeffery Hicks
Windows PowerShell Best Practices - Ed Wilson
PowerShell In Depth - Don Jones, Jeffery Hicks & Richard Siddaway

I'll check out the PowerShell Cookbook. Thank you.
Thank you, more information, the better.