Public/Test-CCMIsClientAlwaysOnInternet.ps1
function Test-CCMIsClientAlwaysOnInternet { <# .SYNOPSIS Return the status of the MEMCM client having AlwaysOnInternet set .DESCRIPTION This function will invoke the IsClientAlwaysOnInternet of the MEMCM Client. This is done using the Microsoft.SMS.Client COM Object. .PARAMETER CimSession Provides CimSessions to return AlwaysOnInternet setting info from .PARAMETER ComputerName Provides computer names to return AlwaysOnInternet setting info from .EXAMPLE C:\PS> Test-CCMIsClientAlwaysOnInternet Returns the status of the local computer having IsAlwaysOnInternet set .EXAMPLE C:\PS> Test-CCMIsClientAlwaysOnInternet -ComputerName 'Workstation1234','Workstation4321' Returns the status of 'Workstation1234','Workstation4321' having IsAlwaysOnInternet set .NOTES FileName: Test-CCMIsClientAlwaysOnInternet.ps1 Author: Cody Mathis Contact: @CodyMathis123 Created: 2020-01-29 Updated: 2020-01-29 #> [CmdletBinding(DefaultParameterSetName = 'ComputerName')] param( [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'CimSession')] [Microsoft.Management.Infrastructure.CimSession[]]$CimSession, [Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true, ParameterSetName = 'ComputerName')] [Alias('Connection', 'PSComputerName', 'PSConnectionName', 'IPAddress', 'ServerName', 'HostName', 'DNSHostName')] [string[]]$ComputerName = $env:ComputerName ) begin { $connectionSplat = @{ } $invokeCIMPowerShellSplat = @{ FunctionsToLoad = 'Test-CCMIsClientAlwaysOnInternet' } } process { foreach ($Connection in (Get-Variable -Name $PSCmdlet.ParameterSetName -ValueOnly)) { $Computer = switch ($PSCmdlet.ParameterSetName) { 'ComputerName' { Write-Output -InputObject $Connection switch ($Connection -eq $env:ComputerName) { $false { if ($ExistingCimSession = Get-CimSession -ComputerName $Connection -ErrorAction Ignore) { Write-Verbose "Active CimSession found for $Connection - Passing CimSession to CIM cmdlets" $connectionSplat.Remove('ComputerName') $connectionSplat['CimSession'] = $ExistingCimSession } else { Write-Verbose "No active CimSession found for $Connection - falling back to -ComputerName parameter for CIM cmdlets" $connectionSplat.Remove('CimSession') $connectionSplat['ComputerName'] = $Connection } } $true { $connectionSplat.Remove('CimSession') $connectionSplat.Remove('ComputerName') Write-Verbose 'Local computer is being queried - skipping computername, and cimsession parameter' } } } 'CimSession' { Write-Verbose "Active CimSession found for $Connection - Passing CimSession to CIM cmdlets" Write-Output -InputObject $Connection.ComputerName $connectionSplat.Remove('ComputerName') $connectionSplat['CimSession'] = $Connection } } $Result = [System.Collections.Specialized.OrderedDictionary]::new() $Result['ComputerName'] = $Computer try { switch ($Computer -eq $env:ComputerName) { $true { $Client = New-Object -ComObject Microsoft.SMS.Client $Result['IsClientAlwaysOnInternet'] = [bool]$Client.IsClientAlwaysOnInternet() [pscustomobject]$Result } $false { $ScriptBlock = 'Test-CCMIsClientAlwaysOnInternet' $invokeCIMPowerShellSplat['ScriptBlock'] = [scriptblock]::Create($ScriptBlock) Invoke-CIMPowerShell @invokeCIMPowerShellSplat @connectionSplat } } } catch { Write-Error "Failure to determine if the MEMCM client is set to always be on the internet for $Computer - $($_.Exception.Message)" } } } } |