Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Last active November 30, 2016 16:57
Show Gist options
  • Save pinecones-sx/bf0f1a6085fe39acacc0c3a9ca8e5b8a to your computer and use it in GitHub Desktop.
Save pinecones-sx/bf0f1a6085fe39acacc0c3a9ca8e5b8a to your computer and use it in GitHub Desktop.
<# Enable-RemotePS
Uses PSEXEC to enable WSMan on a remote computer. Also enables remote PS execution, then returns
information on that comptuter--whether those features are enabled (WSMan control & remote PS),
also returns all compatible versions of .NET installed on that computer and the current PS version.
Usage:
Enable-RemotePS 'computerName.domain.com'
'computer1','computer2','computer3' | Enable-RemotePS
#>
$UNC_Location_of_PSEXEC = '\\UNC\Path\psexec.exe'
$env:psExecLocaction = $UNC_Location_of_PSEXEC
function global:Enable-RemotePS{
param([parameter(ValueFromPipeline)]$targetComputer)
Process{
$tehResults = @()
# Make sure you can reach the computer
If (Test-Connection $targetComputer -Count 1){
#remotely enable winrm
$wsManEnabled = ((Test-WSMan $targetComputer -ErrorAction SilentlyContinue) -ne $null)
If (-not $wsManEnabled){
.$env:psExecLocaction $('\\' + $targetComputer) -s 'C:\Windows\system32\winrm.cmd' qc -quiet > $null
}
#remotely enable unsigned scripts
$successfullySetExecutionPolicy = Invoke-Command -ComputerName $targetComputer `
-ScriptBlock {
If ((Get-ExecutionPolicy) -ne 'Unrestricted'){
Set-ExecutionPolicy Unrestricted -Force
}
Return ((Get-ExecutionPolicy) -eq 'Unrestricted')
}
#get PSVersion from remote computer
$computerPSVer = Invoke-Command -ComputerName $targetComputer `
-ScriptBlock {$constructedVersion = (
[string]$PSVersionTable.PSVersion.Major + '.' +
[string]$PSVersionTable.PSVersion.Minor + '.' +
[string]$PSVersionTable.PSVersion.Build + '.' +
[string]$PSVersionTable.PSVersion.Revision
)
Return $constructedVersion
}
#get .NET versions from remote computer
$computerDotNetVers = Invoke-Command -ComputerName $targetComputer `
-ScriptBlock {
$rootReg = 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP'
$dotNetVersions = Get-ChildItem $rootReg -Recurse -Force |
ForEach {
$tempRegChild = $_
Switch -Wildcard ($_.PSChildName){
'1033' {break}
'Setup' {break}
'Windows*' {break}
default {$tempRegChild}
}
} |
Get-ItemProperty |
Where {$_.Version -ne $null} |
Select -Expand Version -Unique | Sort
Return $dotNetVersions
}
$tehHighestDotNetVer = $computerDotNetVers | Sort | Select -Last 1
$tehResults = [Ordered]@{
'Computer' = $targetComputer
'WSManEnabled' = $wsManEnabled
'ExecutionPolicySet' = $successfullySetExecutionPolicy
'PSVersion' = $computerPSVer
'AllDotNETs' = $computerDotNetVers
'HighestDotNET' = $tehHighestDotNetVer
}
$tehResults = New-Object PSObject -Property $tehResults
} # end if
Return $tehResults
} # end process
} # end function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment