PowerShell Script to Enable Remote PowerShell

[System.Security.Cryptography.X509Certificates.X509Certificate2]$objX509Certificate2 = New-SelfSignedCertificate `
                    -Type SSLServerAuthentication `
                    -FriendlyName "Remote PowerShell" `
                    -KeyFriendlyName "Remote PowerShell" `
                    -KeyDescription "Remote PowerShell" `
                    -Subject $env:computername `
                    -DnsName $env:computername `
                    -CertStoreLocation "cert:\LocalMachine\My" `
                    -KeyAlgorithm RSA `
                    -KeyLength 2048 `
                    -KeyExportPolicy Exportable `
                    -NotAfter (Get-Date).AddYears(25);

[System.ServiceProcess.ServiceController]$objServiceController = Get-Service -Name "WinRM";

if ( $objServiceController.StartType -ne "Automatic" )
{
    $objServiceController | Set-Service -StartupType Automatic
}

if ( $objServiceController.Status -eq "Stopped" )
{
    $objServiceController | Start-Service
}

Enable-PSRemoting -SkipNetworkProfileCheck -Force

[System.String]$strListener = '@{Hostname="'+$env:computername+'";CertificateThumbprint="'+$objX509Certificate2.Thumbprint+'"}';

winrm create winrm/config/Listener?Address=*+Transport=HTTPS $strListener;

Leave a Reply