Here is a basic PowerShell script to set desired state configuration (DSC) for logging off inactive user sessions on a server:
configuration ExampleDSC { node <NodeName> { Script LogoffInactiveSessions { GetScript = { return @{ Result = 'Inactive' } } SetScript = { $IdleTime = (Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty `
LastBootUpTime) $Sessions = Query Session | Where-Object { $_ -notmatch ">console" } Foreach ($Session in $Sessions) { $SessionId = $Session.Split()[0] $UserName = $Session.Split()[2] $IdleTimeSession = (Get-CimInstance -ClassName Win32_ComputerSystem | `
Select-Object -ExpandProperty UserName) -eq $UserName | Get-CimInstance -ClassName Win32_Session | Select-Object -ExpandProperty IdleTime if ($IdleTimeSession -gt $IdleTime) { rwinsta $SessionId } } } TestScript = { $IdleTime = (Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime) $Sessions = Query Session | Where-Object { $_ -notmatch ">console" } $Result = $False Foreach ($Session in $Sessions) { $SessionId = $Session.Split()[0] $UserName = $Session.Split()[2] $IdleTimeSession = (Get-CimInstance -ClassName Win32_ComputerSystem | `
Select-Object -ExpandProperty UserName) -eq $UserName | Get-CimInstance -ClassName Win32_Session | Select-Object -ExpandProperty IdleTime if ($IdleTimeSession -gt $IdleTime) { $Result = $True break } } return $Result } } } } # Compile the configuration into a MOF file ExampleDSC -OutputPath .\ExampleDSC # Apply the configuration to the node Start-DscConfiguration -Path .\ExampleDSC -Wait -Verbose
No comments:
Post a Comment