Restart Lenovo Laptop Audio

I have a Lenovo W540 Laptop at work.  Often times, when docking/undocking, the audio stops working or the audio will not play through the headphones or the On Screen Display stops working or the hotkeys don’t do the right thing.  Below is a batch script I assembled that will stop all affected services and re-start them.  It has worked so far, but let me know if you find a case where it does not work.

 

::Restart audio services and On Screen Display services
net stop AudioSrv
net stop AudioEndpointBuilder
net stop TPHKLOAD
net stop TPHKSVC
net start AudioEndpointBuilder
net start AudioSrv
net start TPHKLOAD
net start TPHKSVC
taskkill /F /FI "IMAGENAME eq Ravcpl*"
Advertisements

Fastest method to get MSMQ message counts

Getting MSMQ message counts can be painfully slow if there are large numbers of messages in a given queue. This PowerShell script is the fastes known way I have found to get the message counts of a queue. The time to get the counts is linear regardless of the number of messages.


<#
.SYNOPSIS
Gets all queues' information from the specified computer

.DESCRIPTION
Gets all queues' information from the specified computer

Adapted from http://www.codeproject.com/Articles/346575/Message-Queue-Counting-Comparisions

.EXAMPLE
Get-MessageQueueInfo.ps1 -MachineName rg-p-blu2 | sort -Property name| ft -Property Name,MessagesInQueue,BytesInQueue -AutoSize
#>

param([string] $MachineName = $env:COMPUTERNAME)
$path = "\\{0}\root\CIMv2" -f $MachineName
$scope = new-object System.Management.ManagementScope ($path)
$scope.Connect()
$QueryString = "SELECT * FROM Win32_PerfFormattedData_msmq_MSMQQueue"
$query = new-object System.Management.ObjectQuery ($queryString)
$searcher = new-object System.Management.ManagementObjectSearcher ($scope, $query)
$searcher.Get()



Kill a process that is listening on a network port

Sometimes, you want to forcibly deploy/install a piece of software that requires a particular service port. This script will find any process that is listening on that port and kill it and if it is a windows service, also disable the service.

param([string]$Port = '80')

$a = netstat -a -n -o

$a -split [environment]::NewLine | foreach {
    $parts = $_.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries) 
    $isMatch = $parts | where {$_ -like "*:$Port*" }
    if($isMatch -and $parts[3] -like 'LISTENING') {
        $proc = Get-Process -Id $parts[4] -ErrorAction SilentlyContinue
        if(-not $proc) {continue}
        Write-Warning "A process was found listening on port $($parts[4])"
        $foundService = (Get-WmiObject Win32_Service -Filter "ProcessId='$($parts[4])'") 
        if($foundService) {
            Write-Host "The process was running as a service, the service will be stopped and disabled ($($foundService.PathName))"
            [void]$foundService.StopService()
            [void]$foundService.ChangeStartMode("disabled")
        }

        Write-Host "Killing process $($proc.Name)"
        kill -Id $parts[4] -Force
        $s = Get-Service
    }
}


Parse output of handle.exe into objects for use with PowerShell

There are times when you need to determine the process that is locking a file.  Using the SysInternals “handle.exe” command line tool, we can search for processes that are using (i.e. locking) a particular file or directory. The code below translates the output of handle.exe into objects that are more friendly for use with PowerShell.


Add-Type -Language CSharp @"
public class Handle{
    public string ProcessName { get;set;}
    public int ProcessId {get;set;}
    public string Type {get;set;}
    public string User {get;set;}
    public string HandleId { get;set;}
    public string Name { get; set;}
    public string AppPoolName {get;set;}
}
"@;

function Get-HandlesAndProcessIds {
    [OutputType([Handle[]])]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true)]
        [object[]]$HandleInfo)
<# .Description Converts the text output of the SysInternals tool "handle.exe" into an object array .Example $handles = & handle.exe -a  -u -accepteula "MY_LOCKED_ASSEMBLY.dll" $allHandles = Get-HandlesAndProcessIds -HandleInfo $handles $allHandles --------------------------- ProcessName : dllhost.exe ProcessId   : 6960 Type        : File User        : DOMAIN\user HandleId    : B34 Name        : MY_LOCKED_ASSEMBLY.dll AppPoolName :  #>
    $handle = new-object Handle
    foreach ($line in $handles) {
        $line=$Line.trim()
        if($line -like "*pid:*") {
            $result = $line | Select-String –pattern '^([^ ]*)\s*pid: ([0-9]*)\s*type: ([^ ]*)\s*([^ ]*)\s*(.*?): (.*)'

            $handle.ProcessName =  $result.Matches[0].Groups[1].Value
            $handle.ProcessId = [int]::Parse( $result.Matches[0].Groups[2].Value)
            $handle.Type = $result.Matches[0].Groups[3].Value
            $handle.User = $result.Matches[0].Groups[4].Value
            $handle.HandleId =  $result.Matches[0].Groups[5].Value
            $handle.Name = $result.Matches[0].Groups[6].Value

            if($handle.ProcessName -like "w3wp.exe") {
                $process = Get-AppPoolProcesses | where { $_.ProcessId -eq $handle.ProcessId }
                if($process) {
                    $handle.AppPoolName = $Process.AppPoolName
                }
            }

            $handle
        }
    }
}

function Get-AppPoolProcesses {
    Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' -ComputerName 'LocalHost' |
        select AppPoolName, ProcessId |
            sort -Property AppPoolName
}

 

Free Proverb Teleprompter software

I was recently tasked with coming up with a teleprompter solution for my church. One of the requirements was that the software could controlled with a remote control. None of the teleprompter software out there (free or commercial) was able to support custom key bindings. So I decided to create my own. It is pretty full featured, comparable to many of the commercially available ones. It requires the .NET 4.0 framework.

Download Proverb Teleprompter here

System colors for WPF

Have you ever wanted to allow your WPF application’s to use the current user’s color scheme? Many people use custom color schemes for accessiblity or just for the heck of it. To keep your WPF application consistent with the rest of Windows, use the SystemColors brushes when possible.

The link below is a little utility application that shows the available colors and corresponding WPF brush names.

Download Source and Binary (VS2010 beta 2 solution and .NET 3.5)

SystemColors