:::: MENU ::::
Monthly Archives: May 2018

Turn off Bluetooth if no Device is connected actively

Get the Exe file from the following website -

https://www.nirsoft.net/utils/bluetooth_viewer.html

create the following power shell script

$LogFile = "C:\Windows\MSSCCM\Logs\Toggle_Bluetooth_OFF.log"
Function LogWrite
{
Param ([string]$logstring)
$logcontent = "$date $logstring"
Add-Content $Logfile -Value $logcontent
}

New-Item -ItemType Directory -Path "C:\Windows\MSSCCM\Scripts\Bluetooth" -Force -ErrorAction SilentlyContinue

$date=Get-Date -Format g
$PREDirectory = Split-Path -Parent $MyInvocation.MyCommand.Definition
$InstDir = Split-Path -parent $MyInvocation.MyCommand.Definition

LogWrite "START $date*********************************************"

$Parameters = '/sxml "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml"'
$Commandtorun = "btView.exe"
$process= Start-Process $CommandToRun $Parameters -PassThru -wait -workingdirectory $InstDir
$ExtVal = $process.ExitCode
logwrite "created the XML file with code:$ExtVal"

if(Test-Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml")
{

logwrite "Searching for any connected devices"

$IS_device_connected = Select-String -Path "C:\Windows\MSSCCM\Scripts\bluetooth\bluetooth.xml" -Pattern "<connected>Yes</connected>" -SimpleMatch -Quiet


if(!$IS_device_connected)
{

logwrite "NO Devices are connected, so toggling the bluetooth setting OFF"

################################################################
#*********TURN OFF BLUETOOTH AS NO DEVICE IS CONNECTED*********#
################################################################
 Function Bluetooth {
 [CmdletBinding()] 
 Param (
 [Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$BluetoothStatus
 )
 
 If ((Get-Service bthserv).Status -eq 'Stopped') { Start-Service bthserv }
 Add-Type -AssemblyName System.Runtime.WindowsRuntime
 $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
 
 Function Await($WinRtTask, $ResultType) {
 $asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
 $netTask = $asTask.Invoke($null, @($WinRtTask))
 $netTask.Wait(-1) | Out-Null
 $netTask.Result
 }
 
 [Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 [Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
 $radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
 $bluetooth = $radios | ? { $_.Kind -eq 'Bluetooth' }
 [Windows.Devices.Radios.RadioState,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
 Await ($bluetooth.SetStateAsync($BluetoothStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
}
 


Bluetooth -Verbose -BluetoothStatus Off


LogWrite "Turned OFF the bluetooth on the machine"

####################################################################
####################################################################

}
else
{

LogWrite "Exiting the Script as there are Bluetooth devices connected to the machine "

}


}
else
{

LogWrite "There was an error running the tool to create the XML file"

}

LogWrite "END $date******************************************************"
LogWrite ""


CCM repair

#Stop and remove ccm service
& sc stop ccmsetup
& sc delete ccmsetup

#Remove ccm certs
Remove-Item ‘HKLM:\SOFTWARE\Microsoft\SystemCertificates\SMS\Certificates\*’ -Force

#Remove ccm files
Remove-Item -Path $env:windir\ccm -Recurse -Force
Remove-Item -Path $env:windir\system32\ccm -Recurse -Force
Remove-Item -Path $env:windir\ccmcache -Recurse -Force
Remove-Item -Path $env:windir\ccmsetup -Recurse -Force
Remove-Item -Path $env:windir\system32\ccmsetup -Recurse -Force
Remove-Item -Path $env:windir\smscfg.ini -Force
Remove-Item -Path $env:windir\sms*.mif -Force

#Remove ccm registry keys
$RegRoot = “HKLM:\Software\Microsoft”
Remove-Item -Path “$RegRoot\ccm” -Recurse -Force
Remove-Item -Path “$RegRoot\ccmsetup” -Recurse -Force
Remove-Item -Path “$RegRoot\sms” -Recurse -Force

#Remove ccm wmi namespaces
Get-WmiObject -Query “SELECT * FROM __Namespace WHERE Name=’CCM'” -Namespace “root” | Remove-WmiObject
Get-WmiObject -Query “SELECT * FROM __Namespace WHERE Name=’SMS'” -Namespace “root\cimv2” | Remove-WmiObject

#Repair WMI
$Path = ‘C:\Windows\System32\wbem’
Stop-Service -Name Winmgmt -Force
Remove-Item “$Path\repository” -Recurse -Force
& wmiprvse /regserver
Start-Service -Name Winmgmt
Get-ChildItem $Path -Filter *.dll | ForEach-Object { & regsvr32.exe /s $_.FullName } | Out-Null
Get-ChildItem $Path -Filter *.mof | ForEach-Object { & mofcomp.exe $_.FullName } | Out-Null
Get-ChildItem $Path -Filter *.mfl | ForEach-Object { & mofcomp.exe $_.FullName } | Out-Null
& mofcomp.exe ‘C:\Program Files\Microsoft Policy Platform\ExtendedStatus.mof’ | Out-Null