Share This Post

PowerShell

ConfigMgr Device Right Click Tool via CompName or IP

A while back, I wrote a quick post regarding a console extension tool (more commonly known as right click tool) I wrote for Microsoft System Center Configuration Manager 2012 R2. https://myitforum.com/myitforumwp/2014/12/24/configuration-manager-remote-control-via-ip/ I wrote this for our people doing remote support in instances where you cannot connect via computer name. In the case of my organization, this is most often due to our VPN virtual adapters not registering in DNS. At any rate, I decided to re-write all of my existing device tools written for our organization so that they will try to connect via computer name, but then if that fails switch over to try to connect via an IP pulled from the Configuration Manager Database. The workflow follows this Visio diagram:

RCT_Workflow

 

I created a template based on this that could then be applied generally to all of my existing tools. Remember to pass the correct arguments to the script from your XML file. If you would like to learn more about how to create Right Click Tools, feel free to ask. Otherwise Ryan Ephgrave of Now Micro has some amazing blog posts on this over at http://ephingadmin.com/. The tool template is below. There are an abnormal number of Write-Host lines in there which I used to follow the workflow while testing. I ended up just leaving them, but you could of course remove them or make any other modifications. If you copy and paste, also remember to double check the code for spacing as copying and pasting from the ISE into the blog post editor removes spaces for a lot of the code. I quickly browsed through to try to correct for this, but I still recommend double-checking it.

 

# ConfigMgr Right Click Tool – Device – ComputerName/IP Connect Template

# Variables

$compname = $args[0]

$SiteServer = $args[1]

$Namespace = $args[2]

# Test Connection via Computer Name

Write-Host “Testing connection via Computer Name…”

$nameconnect = Test-Connection $compname -Count 1 -Quiet

# Computer Name Connection Successful

If ($nameconnect -eq $true){

Write-Host “Computer Name Connection Successful”

# Verify ConfigMgr Computer Name Matches Computer Name in WMI of Host

Write-Host “Verifying ConfigMgr Computer Name matches Computer Name in WMI of Host…”

$dnscompname = Get-WmiObject win32_computersystem -computername $compname | select -expandproperty Name

If ($dnscompname -eq $compname){

Write-Host “Computer Identity Confirmed – Running Command(s)…”

# Command(s)

##### Insert Command(s) to be executed #####

Exit

}

# Computer Name Mismatch

Else{

Write-Host “Computer Identity Not Confirmed”

Write-Host “Please ensure correct DNS entry for this computer”

}

}

# Cannot Connect via Computer Name or Computer Name Mismatch

If ($nameconnect -eq $false -or $dnscompname -ne $compname){

Write-Host “Connection via Computer Name failed”

# Pull IPs from CMDB

Write-Host “Pulling Recent IPAddresses from CMDB…”

$IPs = Get-CimInstance -ComputerName $SiteServer -Namespace $Namespace -Query “select * from sms_r_system where name like ‘%$compname%'” | select -ExpandProperty IPAddresses

# Test Connection via IP

Write-Host “Testing Connection via IPAddress(es)…”

ForEach ($IP in $IPs){

$ipconnect = Test-Connection $IP -Count 1 -Quiet

# IP Connection Successful

If ($ipconnect -eq $True){

Write-Host “Connection successful using $IP

# Make sure IP Computer Name matches ConfigMgr Computer Name

Write-Host “Verifying ConfigMgr Computer Name matches Computer Name in WMI of Host”

$ipcompname = Get-WmiObject win32_computersystem -computername $IP | select -expandproperty Name

If ($ipcompname = $compname){

# Command via IP

##### Insert Command(s) to be executed #####

Pause

Exit

}

}

}

Write-Host “No IPs in the CMDB are currently accessible”

Write-Host “This computer is not currently connected to the network”

Pause

Exit

}

 

 

Share This Post

The myITforum Admin is a regular member and author of myITforum. In addition, he is constantly watching and monitoring the community for potential IT superheroes.

Leave a Reply