PS Oneliners: Set Skype4B services to manual or automatic startup

If you need to reboot a Skype for Business server, you might not always want the services to start automatically afterwords for various reasons. For instance if you are doing a shutdown of an entire pool, you’d want to run

Start-CsPool -PoolFqdn skypepool.contoso.com

to do a cold start of the pool, instead of the services starting automatically.

Use these oneliners to set the services to manual startup and back again to automatic afterwords.

Script: Set-CsKhiCollectorSet.ps1

Lync Key Health Indicators are an essential tool in health monitoring your Lync deployment, and is the first step in deploying the Lync Call Quality Methodology

After adding the KHI counters to applicable Lync servers, it can be quite the chore to log in to all of them to start the performance counters. It is possible to reach all computers from a Performance Monitor snapin, but with a lot of servers that too can take some time.

This script finds all servers that normally run KHI counters, and starts or stops the counters on them. It can also get the current running status of the counters. It is also possible to supply a csv with servers to run on to the script.

Download latest version here

Script: Create live tiles that changes power scheme

After i got my Surface Pro, I’ve more often than before found myself needing to change between power schemes. On my laptop, I’ll usually set it to “Max performance” and just leave it there, but on the Surface it’s necessary to conserve power a bit more.

I’ve thought about making a live tile to do this, so I wrote a PowerShell script that will create one live tile for each configured powerscheme on the machine and pins it to the start screen. The code is based on a codesample for creating shutdown tiles, and this ScriptingGuy post. The script needs to be run as Administrator. Enjoy!

Download Create-PowerSchemeTiles.zip or copy the sourcecode:


#requires -Version 3.0

#####################################################################################
# Create-PowerSchemeTiles.ps1
#
# Creates live tiles for all configured power schemes on the machine and pins them to
# the start screen.
#
#
# Usage:
# .Create-PowerSchemeTiles.ps1
#
# Written by Tom-Inge Larsen (<a href="http://www.codesalot.com">http://www.codesalot.com</a>)
#
#####################################################################################

Function CreatePowerSchemeTile
{
    Param
    (
        [parameter(Mandatory=$true)][String[]]$SchemeGUID,
        [parameter(Mandatory=$true)][String[]]$SchemeName
    )
Write-Verbose "Creating Windows shutdown tile to Start menu."

#create a new shortcut
$ShortcutPath = "$env:ProgramData\Microsoft\Windows\Start Menu\Programs\" + $SchemeName + ".lnk"
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = "$env:SystemRoot\System32\powercfg.exe"
$arguments = "-s " + $SchemeGUID
$Shortcut.Arguments = $arguments
$Shortcut.Save()

#change the default icon of shortcut
$Lnk = $Desktop.ParseName($ShortcutPath)
$LnkPath = $Lnk.GetLink
$LnkPath.SetIconLocation("$env:SystemRoot\System32\ddores.dll",20)
$LnkPath.Save()

#pin application to windows Start menu
$Verbs = $Lnk.Verbs()
Foreach($Verb in $Verbs) {
    If($Verb.Name.Replace("&","") -match "Pin to Start") {
        $Verb.DoIt()
    }
}

If(Test-Path -Path $ShortcutPath) {
    Write-Host "Create" $SchemeName "tile successfully." -ForegroundColor Green
    } Else {
    Write-Host "Failed to create" $SchemeName "tile." -ForegroundColor Red
   }
}

$Shell = New-Object -ComObject Shell.Application
$Desktop = $Shell.NameSpace(0X0)
$WshShell = New-Object -comObject WScript.Shell
$plans = Get-WmiObject -Class win32_powerplan -Namespace root\cimv2\power
$regex = [regex]"{(.*?)}$"
foreach ($plan in $plans) {
    $planGuid = $regex.Match($plan.instanceID.Tostring()).groups[1].value
    $planName = $plan.ElementName.Tostring()
    Write-Debug $planGuid
    Write-Debug $planName
    CreatePowerSchemeTile -SchemeGUID $planGuid -SchemeName $planName
}

Playing with PS: Script: Start remote Lync management session

I wrote a script to start a remote Lync management shell session based on this post. This of course is a big bloated way to do it (it can be done as a twoliner), but I need the PS training 😛

 ##########################################################################################################################
 # New-RemoteCSPSsession.ps1
 #
 # Opens a remote session to a Lync Management Shell and imports all commands
 #
 # Uses current logged inn credentials, but can optionally supply other credentials.
 #
 # eg.
 # Prompt for fefqdn:
 # .New-RemoteCSPSsession.ps1
 #
 # Use other credential:
 # .New-RemoteCSPSsession.ps1 -othercredential $true
 #
 # Ignore certificates on fe:
 # .New-RemoteCSPSsession.ps1 -notrust $true
 #
 # fefqdn can be passed in arguments as well:
 # .New-RemoteCSPSsession.ps1 -csfe lync-admin.contoso.local
 #
 # Written by Tom-Inge Larsen (www.codesalot.com), based on this blogpost:
 # (http://blogs.technet.com/b/csps/archive/2010/06/16/qsremoteaccess.aspx)
 # this script can also easily be run as a twoliner:
 # (http://blog.powershell.no/2010/12/05/lync-server-2010-remote-administration/)
 #
 # $session = New-PSSession -ConnectionUri https://lync-admin.contoso.local/OcsPowershell -Credential (Get-Credential)
 # Import-PSSession -Session $session
 #
 #
 ##########################################################################################################################
 param($othercredential,$notrust,$csfe)

$env = get-host
 $majorversion = $host.version.major

if ($majorversion -lt 2) {
 write-Host "You need to run at least Powershell 2.0 to run this script. http://support.microsoft.com/kb/968929"
 } else {

if ($csfe -eq $null) {
 $csfe = Read-Host "Please enter the FQDN of the Lync Front End pool you want to connect to"
 }

$connectionURI = "https://"+$csfe+"/OcsPowershell"
 $cmdstring = 'New-PSSession -ConnectionUri $connectionURI'

if ($othercredential -eq $true) {
 $credential = Get-Credential
 $cmdstring += ' -Credential $credential'
 }
 if ($notrust -eq $true) {
 $sessionoption = New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck
 $cmdstring += ' -SessionOption $sessionoption'
 }

$session = & $executioncontext.invokecommand.NewScriptBlock($cmdstring)

Import-PSSession $session
 }

Disable AD disabled CS users powershell script

I’ve made a script to disable AD disabled users from Lync. The script pulls all AD disabled users and checks if they are disabled for Lync as well. If not, they will be. Optional output to screen and/or file.

Download Disable-AdDisabledCsUsers.zip or copy the sourcecode:

 #####################################################################################
 # Disable-AdDisabledCsUsers.ps1
 #
 # Pulls all AD disabled users from AD and disables them for Lync as well
 #
 # Can optionally write logs to file or screen using -verbose and/or -logFile inputs
 #
 # eg.
 #
 # .Disable-AdDisabledCsUsers.ps1 -verbose $true -logFile "c:logfile.log"
 #
 #
 #
 # Written by Tom-Inge Larsen (codesalot.com)
 #
 ####################################################################################
 param($verbose,$logFile)
Import-Module active*</code></code>if ($logFile -ne "") {
 $logoutput = [System.IO.StreamWriter] $logFile
 $logoutput.WriteLine("AD disabled users that was Lync disabled:")
 }

$disabledADusers = Search-ADAccount -AccountDisabled -UsersOnly | Select-Object userprincipalname

$disabledADusers | foreach-object {
 $identity = $_.userprincipalname
 $csuser = Get-CsUser -Identity $identity -ErrorAction SilentlyContinue | Select-Object Enabled

if ($csuser.enabled -eq $true) {
 Disable-CsUser -Identity $identity
 if ($verbose -eq $true) {
 Write-Host "AD disabled user" $identity "is now disabled for Lync as well"
 }
 if ($logFile -ne "") {
 $logoutput.WriteLine($identity)
 }
 }
 }

if ($logFile -ne "") {
 $logoutput.close()
 if ($verbose -eq $true) {
 Write-Host $logFile "written."
 }
 }