Powershell check service status and eventually start if stopped

I mostly use this script for checking all XenDesktop app hosts if print spooler is running, and if its not it will try to start it.

First it finds all registered hosts from XenDesktops, and then query the spooler service and eventually try to start it if it is stopped.

To check/start other services, just enter servicename in $service variable.

$xendesktopController = "deliverycontroller.domain.local"
$onlineHosts = @()
$service = "spooler"
## This function gets all online hosts with status as registered in XenDesktop. This way we dont have to wait for hosts thats not accepting users anyways.
## Result is output to a CSV on c:\scripts\
function getOnlineHosts {
Write-host -ForegroundColor Yellow -BackgroundColor Black "*********************************************"
Write-Host -ForegroundColor Yellow -BackgroundColor Black "** Getting online hosts.....be patient.... **"
Write-Host -ForegroundColor Yellow -BackgroundColor Black "*********************************************"
$psremote = New-PSSession -ComputerName $xendesktopController
Invoke-Command -Session $psremote { asnp citrix* }
Invoke-Command -Session $psremote -scriptblock { Get-BrokerMachine -Filter { RegistrationState -eq 'Registered' } | Select-Object dnsName} | Export-Csv c:\scripts\onlinehosts.csv
Exit-PSSession

}

## Reads CSV and checks spooler status on each host, if not running, service is started.
function checkAndStartSpooler
{
$onlineHosts = Import-Csv c:\scripts\onlinehosts.csv
foreach ($h in $onlineHosts)
{
$pshost = $h.DNSName
Write-Host -BackgroundColor Black "Checking spooler status on: " $h.DNSName
Invoke-Command -ComputerName $pshost {
$serviceStatus = Get-Service -Name Spooler
if ($serviceStatus.Status -ne "Running")
{
Start-Service -Name Spooler
Write-host -ForegroundColor Red -BackgroundColor Black "Started Spooler on: " $env:COMPUTERNAME
}
else
{
Write-host -ForegroundColor Green -BackgroundColor Black "Spooler checked out fine on: " $env:COMPUTERNAME
}
}
}

}
## Calling functions.
getOnlineHosts
checkAndStartSpooler

 

XenDesktop monitor registration state

These days, we have had a lot of trouble with a XenDesktop 7.1 farm that have VM’s unregister with controller and in many times leaving the VM “half dead”. So i spent a few days with refreshing Studio to watch for VM’s going in to unregistered state. Today I spent a few minutes and created a script that does the same thing in PoSh. Refreshing every 30 seconds.

This probably could be better written on a one-liner, but I really dont like one-liners they are messy :)

asnp citrix*
$i = 1
while ($i -eq 1) {
Clear-Host
Get-BrokerDesktop|select MachineName,RegistrationState|sort RegistrationState|ft
Start-Sleep -Seconds 30
}

Remote initialize and format newly added harddrive

Handy little script that initializes your newly added harddisk and then format it.


function initalizeDisk ($dnsname)
{
$psremote = New-PSSession $dnsname
Invoke-Command -Session $psremote { Get-Disk |

Where partitionstyle -eq 'raw' |

Initialize-Disk -PartitionStyle MBR -PassThru |

New-Partition -AssignDriveLetter -UseMaximumSize |

Format-Volume -FileSystem NTFS -NewFileSystemLabel "WriteCache" -Confirm:$false

Exit-PSSession
}

}

Print test page to all your printers from Powershell

I recently had a task, requiring me to print a testpage to all printer queues on a printserver. With somewhere around 150 queues, this is certainly something i won’t do manually. So i created a little snippet to perform my task.

I know this could be done in one line, but I really dislike that way of writing code.


$printers = Get-Printer | select Name

foreach ($printer in $printers) {

Write-Host "Spooling Test Page to Printer: " $printer.name

rundll32 printui.dll,PrintUIEntry /k /n $printer.name

}

Duplicate all applications in XenDesktop 7 to new delivery group

This little powershell script will get all current applications from your current delivery group, and duplicate it to a new. 

asnp citrix*

$apps = Get-BrokerApplication | select BrowserName
$apps

foreach ($app in $apps)
{
$in = Get-BrokerApplication -BrowserName $app.BrowserName
Write-Host “Duplicating app to new deliverygroup: ” $in.BrowserName
#$in | Add-BrokerApplication -DesktopGroup “Server 2012v2″

}

 

Unable to launch as the application is not currently available but is working via StorefrontWeb

So this was also really crappy, systems have been working and tested for a while. After some kind of SQL issues last night, we had troubles logging in through Receiver, but from Web working ok.

Found the resolution BUT no root cause, as this was working and suddenly stopped working.

But here is what fixed it.

 

On your XenDesktop controller run this.

PS C:\Users\> asnp citrix*
PS C:\Users\> Set-BrokerSite -TrustRequestsSentToTheXmlServicePort $true