Category: PSOL

PowerShell OneLiner

Your Online/Offline state for a offline files enabled share

Want to know the online/offline state of a DFS-share that is in your offline files?

There is a WMI class called Win32_OfflineFilesConnectionInfo that you can use. Of course it’s not as easy as just querying that one…

You can do it one one compressed line…

((Get-WmiObject Win32_OfflineFilesItem -Filter "ItemType = 2 AND ItemPath = '\\\\SNOWLAND.SE\\MyRoot'").ConnectionInfo).ConnectState

Or a bit more elegant with a function…

Function Get-DfsRootConnectionState {
	PARAM (
		[string] $rootName,
		[switch] $asText
	)
	$ConnectStates = @("Unknown", "Offline", "Online")
	$OfflineReasons = @("Unknown", "Not applicable", "Working offline", "Slow connection", "Net disconnected", "Need to sync item", "Item suspended")

	$rootName = $rootName.Replace("\", "\\")
	$root = Get-WmiObject Win32_OfflineFilesItem -Filter "ItemType = 2 AND ItemPath = '$($rootName)'"

	$stateAsText = "$($ConnectStates[$root.ConnectionInfo.ConnectState]) - $($OfflineReasons[$root.ConnectionInfo.OfflineReason])"
	Write-Verbose $stateAsText
	
	If ($asText) {
		Return $stateAsText
	} else {
		Return $root.ConnectionInfo.ConnectState
	}
}

Get-DfsRootConnectionState -rootName "\\SNOWLAND.SE\MyRoot" -asText

PSOL: Get-SNMPDevices

Quick and easy way to get a list of all OpsMgr monitored SNMP devices

Get-MonitoringClass -name 'System.NetworkDevice' | Get-MonitoringObject | Format-Table PathName, DisplayName

PSOL: Read web page

If you need to output something from a webpage via Powershell, then .NET is a easy way to do it.

Write-Host ([String] (New-Object Net.WebClient).DownloadString('http://snowland.se/demo/'))

PSOL: Resolve informational alerts without repeatcount

To get rid of a large number of alerts in SCOM you can of course use PowerShell…

This script will take informational alerts without any repeats that are 24 hours old and simply resolve them with a small comment.

get-alert | where { $_.Severity -eq 0 -and $_.RepeatCount -eq 0 -and $_.ResolutionState -eq '0' -and $_.LastModified -lt (get-date).AddHours(-24)} | Resolve-Alert -comment "Alert resovled via script (24 hours old informational alert without repeatcount)" | Format-Table MonitoringObjectPath, Name, TimeRaised

(Change “$_.RepeatCount -eq 0” to “$_.RepeatCount -lt 3” to get alerts with less then 3 repeats)

PSOL: List size of all files matching a pattern

Just recently started to play around with PowerShell… so here is my first PSOL-post (PowerShell One Liner), expect more.

get-Childitem C:\ -recurse | where{$_.Extension -match "pst"} | Format-Table FullName, Length