Benutzer abfragen, der den Wartungsmodus für einen Server oder Client festgelegt hat

Mein erstes Skript kann jetzt in meinem Blog veröffentlicht werden. Mit der nachfolgenden Funktion kannst du  Informationen über Server und Clients und dessen Wartungsmodus sammeln.

<#
	.SYNOPSIS
		Get information about user that set maintenance mode.
	
	.DESCRIPTION
		Over the Citrix XenDeesktop or XenApp log database, you can finde the user that
		set the maintenance mode of an worker.
		This is version 1.0.
	
	.PARAMETER AdminAddress
		Specifies the address of the Delivery Controller to which the PowerShell module will connect. This can be provided as a host name or an IP address.
	
	.PARAMETER Credential
		Specifies a user account that has permission to perform this action. The default is the current user.
	
	.EXAMPLE
		Get-CitrixMaintenanceInfo
		Get the informations on an delivery controller with nedded credentials.
	
	.EXAMPLE
		Get-CitrixMaintenanceInfo -AdminAddress server.domain.tld -Credential (Get-Credential)
		Use sever.domain.tld to get the log informations and use credentials.
	
	.NOTES
		Additional information about the function.

	.LINK
		http://www.beckmann.ch/blog/2016/11/01/get-user-who-set-maintenance-mode-for-a-server-or-client/
#>
function Get-CitrixMaintenanceInfo {
	[CmdletBinding()]
	[OutputType([System.Management.Automation.PSCustomObject])]
	param
	(
		[Parameter(Mandatory = $false,
				   ValueFromPipeline = $true,
				   Position = 0)]
		[System.String[]]$AdminAddress = 'localhost',
		[Parameter(Mandatory = $false,
				   ValueFromPipeline = $true,
				   Position = 1)]
		[System.Management.Automation.PSCredential]$Credential
	) # Param
	
	Try {
		$PSSessionParam = @{ }
		If ($null -ne $Credential) { $PSSessionParam['Credential'] = $Credential } #Splatting
		If ($null -ne $AdminAddress) { $PSSessionParam['ComputerName'] = $AdminAddress } #Splatting
		
		# Create Session
		$Session = New-PSSession -ErrorAction Stop @PSSessionParam
		
		# Create script block for invoke command
		$ScriptBlock = {
			if ((Get-PSSnapin "Get-PSSnapin Citrix.ConfigurationLogging.Admin.*" -ErrorAction silentlycontinue) -eq $null) {
				try { Add-PSSnapin Citrix.ConfigurationLogging.Admin.* -ErrorAction Stop } catch { write-error "Error Get-PSSnapin Citrix.ConfigurationLogging.Admin.* Powershell snapin"; Return }
			} #If
			
			$Date = Get-Date
			$StartDate = $Date.AddDays(-7) # Hard coded value for how many days back
			$EndDate = $Date
			
			# Command to get the informations from log
			$LogEntrys = Get-LogLowLevelOperation -MaxRecordCount 1000000 -Filter { StartTime -ge $StartDate -and EndTime -le $EndDate } | Where { $_.Details.PropertyName -eq 'MAINTENANCEMODE' } | Sort EndTime -Descending
			
			# Build an object with the data for the output
			[array]$arrMaintenance = @()
			ForEach ($LogEntry in $LogEntrys) {
				$TempObj = New-Object -TypeName psobject -Property @{
					User = $LogEntry.User
					TargetName = $LogEntry.Details.TargetName
					NewValue = $LogEntry.Details.NewValue
					PreviousValue = $LogEntry.Details.PreviousValue
					StartTime = $LogEntry.Details.StartTime
					EndTime = $LogEntry.Details.EndTime
				} #TempObj
				$arrMaintenance += $TempObj
			} #ForEach				
			$arrMaintenance
		} # ScriptBlock
		
		# Run the script block with invoke-command, return the values and close the session
		$MaintLogs = Invoke-Command -Session $Session -ScriptBlock $ScriptBlock -ErrorAction Stop
		Write-Output $MaintLogs
		Remove-PSSession -Session $Session -ErrorAction SilentlyContinue
		
	} Catch {
		Write-Warning "Error occurs: $_"
	} # Try/Catch
} # Get-CitrixMaintenanceInfo

Du kannst die folgende Funktion ausführen, um Informationen für einen oder mehrere Client/Server zu erhalten. Dies ist nur ein Beispiel. Oft wirst du ein anderes Skript haben, das den Wartungsstatus überprüft, und nun kannst du die Informationen abrufen, die welcher Benutzer diesen aktiviert hat.

Function Get-CitrixComputerMaintenanceInfo {
	[CmdletBinding()]
	Param (
		[Parameter(
				   Mandatory = $True,
				   Position = 0,
				   ValueFromPipeline = $True
				   )]
		[System.String[]]$ComputerName = 'localhost',
		[Parameter(
				   Mandatory = $True,
				   Position = 0,
				   ValueFromPipeline = $True
				   )]
		$Log
	) # Param
	
	process {
		ForEach ($Computer in $ComputerName) {
			$objMaintenance = $Log | Where { $_.TargetName.ToUpper() -match $Computer } | Select -First 1
			Write-Output $objMaintenance
		} # ForEach
	} # Process
} # Get-CitrixComputerMaintenanceInfo

$Server = @('Server1', 'Server2')
$AdminAddress = 'Server.domain.tld'
$Cred = Get-Credential

$Maintenance = Get-CitrixMaintenanceInfo -AdminAddress $AdminAddress -Credential $Cred

Get-CitrixComputerMaintenanceInfo -ComputerName $Server -Log $Maintenance

2 Gedanken zu „Benutzer abfragen, der den Wartungsmodus für einen Server oder Client festgelegt hat“

Kommentare sind geschlossen.