Created
January 16, 2021 04:14
-
-
Save bob-ross27/e02a3df6b3c6a290a693d59be36299cc to your computer and use it in GitHub Desktop.
Toggle the Windows device status for the provided webcam.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Enable or Disable a webcam. | |
.DESCRIPTION | |
Toggle the Status of a Webcam by disabling the PnPDevice. Useful for completely disabling an unneeded device | |
without physically disconnecting it. | |
Can also be used to output the current status of the provided webcam. | |
Webcam name is sourced from the -WebcamName parameter, trying the WEBCAM_NAME env var if no parameter used. | |
.EXAMPLE | |
Toggle-Webcam.ps1 | |
.EXAMPLE | |
Toggle-Webcam.ps1 -WebCamName "Logitech HD Webcam C270" | |
.EXAMPLE | |
Toggle-Webcam.ps1 -Status | |
.EXAMPLE | |
Toggle-Webcam.ps1 -WebCamName "Logitech HD Webcam C270" -Status | |
#> | |
param ( | |
# Only output the status and exit. | |
[switch]$Status = $false, | |
# Name of the webcam to use. | |
[string]$WebcamName | |
) | |
# Check for Admin (required to enable PnP Devices | |
function Check-Admin { | |
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] “Administrator”)) { | |
return $false | |
} | |
return $true | |
} | |
# Try Arg, then Env var, return $false if no name detected. | |
function Get-WebCam-Name { | |
$name = If($WebcamName) { $WebcamName } ElseIf($env:WEBCAM_NAME) { $env:WEBCAM_NAME } Else { $false } | |
return $name | |
} | |
# Try to get the webcam Pnp object. | |
function Get-Webcam { | |
$webcamName = Get-Webcam-Name | |
if (-not($webcamName)) { | |
Write-Warning "No Webcam name detected from env var or passed as argument." | |
return $false | |
} | |
$webcam = Get-PnpDevice -FriendlyName $webcamName -ErrorAction SilentlyContinue | |
if ($webcam) { | |
return $webcam | |
} | |
return $false | |
} | |
# Check the status without modifying the device. | |
function Output-Status { | |
# Get a new webcam object in case the status changes. | |
$webcam = Get-Webcam | |
$webcamStatus = If ($webcam.Status -eq "Ok") {"Enabled"} Else {"Disabled"} | |
$webcamObj = New-Object PSObject -Property @{ | |
"Webcam Name" = $webcam.FriendlyName | |
"Status" = $webcamStatus | |
} | |
# Output status table. | |
$webcamObj | Format-Table -Property "Webcam Name", "Status" | |
} | |
# Test webcam exists | |
$webcam = Get-Webcam | |
if (-not($webcam)) { | |
Write-Warning "Webcam not detected." | |
Break | |
} | |
# Status flag passed - output status and exit. | |
if ($Status) { | |
Output-Status | |
Break | |
} | |
# Exit if not running as admin. | |
if (-not (Check-Admin)) { | |
Write-Warning “You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!” | |
Break | |
} | |
# Toggle Webcam | |
if ($webcam.Status -eq "Error") { | |
Write-Host "Webcam is currently disabled - Enabling Webcam..." | |
Enable-PnpDevice -InputObject $webcam -Confirm:$false | |
Output-Status | |
} | |
else { | |
Write-Host "Webcam is currently enabled - Disabling Webcam..." | |
Disable-PnpDevice -InputObject $webcam -Confirm:$false | |
Output-Status | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment