“Log Off” aus einer Powershell RemoteSession

Kürzlich bin ich auf die Frage gestossen, wie au seiner Powershell RemoteSession angemeldete Benutzer abgemeldet werden können. Per WMI ist dies relativ einfach umsetzbar.

Get-WMIObject -class Win32_OperatingSystem -Computername [MyComputerName]).Win32Shutdown(0)

Win32Shutdown Flags:
0            Log Off
4            Forced Log Off (0+4)
1            Shutdown
5            Forced Shutdown (1+4)
2            Reboot
6            Forced Reboot (2+4)
8            Power Off
12          Forced Power Off (8+4) 

Quelle: https://msdn.microsoft.com/en-us/library/windows/desktop/aa394058?f=255&MSPPError=-2147217396


Oracle Java JRE Einstellungen per Configuration Baselines

Grundsätzlich sollten sicherheitstechnisch die Einstellungen der Java Runtime möglichst hoch eingestellt werden. Während der Installation wird die Grundkonfiguration und somit die Standardsicherheit in der Datei "C:\Windows\Sun\Java\Deployment\deployment.properties" eingestellt. In Spezialfällen können jedoch explizite Einstellungen nötig sein.

In "Microsoft System Center Configuration Manager 2012" kann unter "\Assets and Compliance\Overview\Compliance Settings\" eine "Configuration Baseline" mit einem "Configuration Item" als Lösung erstellt werden. 

"Configuration Item"

Im dazugehörigen "Configuration Item" erfolgt die Prüfung (Discovery Script) und gegebenenfalls Korrektur (Remediation Script) per Powershell.

Discovery.ps1

#############################################################################
<#
 Summary:          JRE Config
 Create Date:      2015-06-08
 Author:           Martin Schneeberger
 License:          Copyright © 2015
#>
#############################################################################
# Main
#############################################################################

try
{
	# Check if file 'deployment.properties' exists
	if(Test-Path -Path "$env:windir\Sun\Java\Deployment\deployment.properties")
	{
	    # Check setting 'deployment.security.revocation.check'
	    $deploymentsecurityrevocationcheck = Get-Content "$env:windir\Sun\Java\Deployment\deployment.properties" | Select-String "deployment.security.revocation.check=NO_CHECK"

	    # Check setting 'deployment.security.revocation.check'
	    $deploymentsecuritytlsrevocationcheck = Get-Content "$env:windir\Sun\Java\Deployment\deployment.properties" | Select-String "deployment.security.tls.revocation.check=NO_CHECK" 

	    # Return Comliance
	    if(($deploymentsecurityrevocationcheck -eq $null) -or ($deploymentsecuritytlsrevocationcheck -eq $null))
	    {
	    	$Compliance = "NonCompliant" 
	    }
	    else
	    {
	    	$Compliance = "Compliant"
	    }
	}
	else
	{
	    $Compliance = "NonCompliant" 
	}

	# Return
	Return $Compliance
}
finally
{
	New-EventLog -LogName Application -Source "Configuration Baseline" -ErrorAction SilentlyContinue
	Write-EventLog -logname Application -source "Configuration Baseline" -eventID 3001 -entrytype Information -message "Configuration Baseline 'JRE Config' Discovery: $Compliance"
}

#############################################################################

Remediation.ps1

#############################################################################
<#
 Summary:          JRE Config
 Create Date:      2015-06-08
 Author:           Martin Schneeberger
 License:          Copyright © 2015
#>
#############################################################################
# Main
#############################################################################

# Config Template
$ConfigTemplate = @"
#deployment.properties
deployment.security.SSLv2Hello=true
deployment.expiration.check.enabled=false
deployment.user.security.exception.sites=C\:\\Windows\\Sun\\Java\\Deployment\\exception.sites
deployment.user.security.exception.sites.locked
deployment.security.level=HIGH
deployment.security.level.locked
deployment.proxy.type=3
deployment.proxy.type.locked
deployment.security.revocation.check=NO_CHECK
deployment.security.revocation.check.locked
deployment.security.tls.revocation.check=NO_CHECK
deployment.security.tls.revocation.check.locked
"@

try
{
	# Check if file 'deployment.properties' exists
	if(Test-Path -Path "$env:windir\Sun\Java\Deployment\deployment.properties")
	{
	    # Check setting 'deployment.security.revocation.check'
	    $deploymentsecurityrevocationcheck = Get-Content "$env:windir\Sun\Java\Deployment\deployment.properties" | Select-String "deployment.security.revocation.check=NO_CHECK"

	    # Check setting 'deployment.security.revocation.check'
	    $deploymentsecuritytlsrevocationcheck = Get-Content "$env:windir\Sun\Java\Deployment\deployment.properties" | Select-String "deployment.security.tls.revocation.check=NO_CHECK" 

	    # Write new 'deployment.properties' file
	    if(($deploymentsecurityrevocationcheck -eq $null) -or ($deploymentsecuritytlsrevocationcheck -eq $null))
	    {
	        $ConfigTemplate | Out-File -FilePath "$env:windir\Sun\Java\Deployment\deployment.properties" -Encoding default -Force
	    }
	}
	else
	{
	    # Write new 'deployment.properties' file
	    $ConfigTemplate | Out-File -FilePath "$env:windir\Sun\Java\Deployment\deployment.properties" -Encoding default -Force
	}
}
finally
{
	New-EventLog -LogName Application -Source "Configuration Baseline" -ErrorAction SilentlyContinue
	Write-EventLog -logname Application -source "Configuration Baseline" -eventID 3002 -entrytype Information -message "Configuration Baseline 'JRE Config' Remediation: Updated!"
}

#############################################################################


In der "Compliance Rule" wird definiert, dass der Rückgabewert des "Discovery Script" dem Wert "Compliant" entsprechen muss. Ist dies nicht der Fall wir das "Remediation Script" ausgeführt.


"Configuration Baseline"

Die "Configuration Baseline" müsste zeitlich relativ oft angewandt werden, damit die Einstellungen für die Endbenutzenden auch nach einem Update der Java Runtime rasch wieder wirken. Eine Evaluation jede Stunde macht Sinn.

Liste installierter OEM Driver - Powershell PNPutil

Folgender Code nimmt die Elemente sämtlicher OEM Drivers, aufgelistet per PNPutil, in ein Array auf und gibt diese in einer “GridView” aus.

# load output of util into memory as array
$PNPutil = pnputil -e
#
# parse the array and create objects
$PNPObjects = for($i = 2;$i -lt $PNPutil.length;$i += 6){
    $Array = @{}
    for($j = 0; $j -lt 5;$j ++){
        $Pair = $PNPutil[$i+$j].Split("\:")
        $Array.Add($Pair[0].Trim().Replace(" ","_"),$Pair[1].Trim().Replace("            ",""))
    }
New-Object PSObject -Property $Array
}
# output in gridview
$PNPObjects | Out-GridView

 

Folgender zusätzlich Code kann verwendet werden um sämtliche Objekte eines bestimmten Herstellers, Klasse und Version auszugeben. Die oem###.inf Werte können für die Entfernung eines OEM Driver Packages genutzt werden.

# search objects for a specific package provider, class and version
$OEM = ($PNPObjects |?{($_.Driver_date_and_version -like "*5303.1600.0.0") -and ($_.Driver_package_provider -match "Xerox") -and ($_.Class -match "Printer")} | Select-Object Published_Name -ExpandProperty Published_Name)