Skip to content

Instantly share code, notes, and snippets.

@Nonary
Created May 27, 2022 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nonary/1fd9123e5f1e2393df74eed111e4487a to your computer and use it in GitHub Desktop.
Save Nonary/1fd9123e5f1e2393df74eed111e4487a to your computer and use it in GitHub Desktop.
NVIDIA/Moonlight External Firewall Block/Unlocking Script
function Enable-MoonlightExternally() {
Get-ScheduledJob -Name "Wait for Moonlight to Terminate" -ErrorAction SilentlyContinue | Unregister-ScheduledJob -ErrorAction SilentlyContinue
start_monitoring_job
Write-Host "External Connections are permitted for moonlight until the next time you disconnect a stream."
}
function Disable-MoonlightExternally {
Get-ScheduledJob -Name "Wait for Moonlight to Terminate" -ErrorAction SilentlyContinue | Unregister-ScheduledJob -ErrorAction SilentlyContinue
Get-NetFirewallRule | Where-Object {$_.DisplayName -like '*NVIDIA Shield*'} | Get-NetFirewallAddressFilter | Set-NetFirewallAddressFilter -RemoteAddress "192.168.1.0/24"
}
function start_monitoring_job {
$duration = (Get-Date) + (New-TimeSpan -Seconds 5)
$trigger = New-JobTrigger -At $duration -Once
Get-NetFirewallRule | Where-Object {$_.DisplayName -like '*NVIDIA Shield*'} | Get-NetFirewallAddressFilter | Set-NetFirewallAddressFilter -RemoteAddress Any
Register-ScheduledJob -Name "Wait For Moonlight to Terminate" -ScriptBlock {
$lock = $false
while($true){
$streaming = Get-Process nvstreamer -ErrorAction SilentlyContinue
if($streaming -and $lock -eq $false){
$lock = $true
Write-Host "Curently streaming..."
}
elseif ($null -eq $streaming -and $lock) {
Write-Host "Terminating lease!"
Disable-MoonlightExternally
break;
}
Write-Host "Sleeping for 3 seconds..."
Start-Sleep -Seconds 3
}
} -Trigger $trigger -ErrorAction SilentlyContinue | Out-Null
}
function End-RDPSessionForMoonlight {
$session_text = & query session | select-string rdp-tcp
[int]$session_id = $session_text -join "," | select-string "\s[0-9]\s" | Select-Object matches -ExpandProperty matches | Select-Object value -ExpandProperty value
& tscon $session_id /dest:console
}
Export-ModuleMember -Function Enable-MoonlightExternally, Disable-MoonlightExternally, End-RDPSessionForMoonlight
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment