Crafting an Advanced PowerShell Function with Bing Copilot

In this post, I create an Advanced PowerShell Function that retrieves running services from a specified computer, complete with error handling and network checks, using Bing CoPilot. I’ll include the complete transcript of the conversation

Bing Copilot is one of a number of AI applications based on OpenAI’s Large Language Models (I think GTP 4). The goal of this session was to see Copilot handle conversational context with a technical topic when asked to modify a previous response.


Here is the complete transcript of our conversation regarding the creation of an advanced PowerShell function:

User: create a PowerShell function that takes a computer name as input and returns a list of all running services on that computer


Copilot: Certainly! Below is a PowerShell function that takes a computer name as input and returns a list of all running services on that computer:
function Get-RunningServices {
param (
[Parameter(Mandatory=$true)]
[string]$ComputerName
)
try {
$services = Get-Service -ComputerName $ComputerName | Where-Object { $_.Status -eq ‘Running’ }
return $services
} catch {
Write-Error “An error occurred: $_”
}
}

To use this function, you would call it with the computer name like this:

Get-RunningServices -ComputerName “ComputerNameHere”

Replace “ComputerNameHere” with the actual name of the computer you want to check. This function will list all services that are currently running on the specified computer. Make sure you have the necessary permissions to query services on the remote computer.

User: Have the function return a PowerShell custom object that has the Computer Name and the Service Display Name, and the Name of the service, also include the start type of the service.

Copilot: Certainly! Here’s an updated PowerShell function that returns a custom object with the Computer Name, Service Display Name, Service Name, and the Start Type of each running service:

function Get-RunningServices {
param (
[Parameter(Mandatory=$true)]
[string]$ComputerName
)
try {
$services = Get-Service -ComputerName $ComputerName | Where-Object { $_.Status -eq ‘Running’ }
$customObjects = $services | ForEach-Object {
[PSCustomObject]@{
ComputerName = $ComputerName
DisplayName = $_.DisplayName
ServiceName = $_.Name
StartType = $_.StartType
}
}
return $customObjects
} catch {
Write-Error “An error occurred: $_”
}
}

This function will output a list of custom objects, each containing the Computer Name, Service Display Name, Service Name, and Start Type for all running services on the specified computer. To call this function, use the same syntax as before, replacing “ComputerNameHere” with the actual name of the computer.

User: Thank you. Please include in the function a test to determine if the computer is on the network. If not do not try and collect the services data.

Copilot: You’re welcome! Here’s the updated PowerShell function with a network test included. The function will now check if the computer is reachable on the network before attempting to collect the services data:

function Get-RunningServices {
param (
[Parameter(Mandatory=$true)]
[string]$ComputerName
)
if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
try {
$services = Get-Service -ComputerName $ComputerName | Where-Object { $_.Status -eq ‘Running’ }
$customObjects = $services | ForEach-Object {
[PSCustomObject]@{
ComputerName = $ComputerName
DisplayName = $_.DisplayName
ServiceName = $_.Name
StartType = $_.StartType
}
}
return $customObjects
} catch {
Write-Error “An error occurred while retrieving services: $_”
}
} else {
Write-Host “The computer $ComputerName is not reachable on the network.”
}
}

This function uses Test-Connection to ping the specified computer. If the computer responds to the ping, the function proceeds to gather the services data. If the computer does not respond, it outputs a message stating that the computer is not reachable on the network.

User: Add detailed comments to the function please, but do so in a steampunk style

Copilot: Certainly! Here’s the PowerShell function adorned with steampunk-inspired comments:

function Get-RunningServices {
<#
.SYNOPSIS
A fantastical apparatus to procure the list of diligent automatons, those services that toil tirelessly, aboard the grand vessel known as the Computer.

.DESCRIPTION
By invoking this contraption with a ComputerName, one shall unveil the list of running services, each a cog in the grand machination of the digital realm.

.PARAMETER ComputerName
The nomenclature of the digital leviathan whose services you wish to audit.

.EXAMPLE
Get-RunningServices -ComputerName “ZeppelinStation”
A simple incantation to reveal the inner workings of the “ZeppelinStation”.
#>

param (
[Parameter(Mandatory=$true)]
[string]$ComputerName # The moniker of the computational behemoth.
)

# First, we must ascertain if the digital leviathan is within our ethereal reach.
if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
try {
# With the leviathan in our sights, we proceed to summon the list of services.
$services = Get-Service -ComputerName $ComputerName | Where-Object { $_.Status -eq ‘Running’ }

# For each service, we shall craft a custom object, a testament to human ingenuity.
$customObjects = $services | ForEach-Object {
[PSCustomObject]@{
ComputerName = $ComputerName # The vessel’s name, emblazoned upon the object.
DisplayName = $_.DisplayName # The service’s grand title for all to see.
ServiceName = $_.Name # The service’s secret name, whispered in the digital winds.
StartType = $_.StartType # The manner in which the service is summoned at dawn.
}
}
return $customObjects # The fruits of our labor, presented with pride.
} catch {
Write-Error “An error occurred while retrieving services: $_” # Alas, even the best-laid plans may go awry.
}
} else {
Write-Host “The computer $ComputerName is not reachable on the network.” # A somber note, the leviathan is beyond our grasp.
}

}

This function now includes whimsical, steampunk-themed comments that describe its purpose and usage in a more imaginative and narrative style. Enjoy your journey through the gears and sprockets of the digital age! 🎩⚙️

User: Splendid! Can you add more steampunk flair to the error messages?

Copilot: Absolutely! Here’s the function with enhanced steampunk flair in the error messages:

function Get-RunningServices {
<#
.SYNOPSIS
A fantastical apparatus to procure the list of diligent automatons, those services that toil tirelessly, aboard the grand vessel known as the Computer.

.DESCRIPTION
By invoking this contraption with a ComputerName, one shall unveil the list of running services, each a cog in the grand machination of the digital realm.

.PARAMETER ComputerName
The nomenclature of the digital leviathan whose services you wish to audit.

.EXAMPLE
Get-RunningServices -ComputerName “ZeppelinStation”
A simple incantation to reveal the inner workings of the “ZeppelinStation”.
#>

param (
[Parameter(Mandatory=$true)]
[string]$ComputerName # The moniker of the computational behemoth.
)

# First, we must ascertain if the digital leviathan is within our ethereal reach.
if (Test-Connection -ComputerName $ComputerName -Count 2 -Quiet) {
try {
# With the leviathan in our sights, we proceed to summon the list of services.
$services = Get-Service -ComputerName $ComputerName | Where-Object { $_.Status -eq ‘Running’ }

# For each service, we shall craft a custom object, a testament to human ingenuity.
$customObjects = $services | ForEach-Object {
[PSCustomObject]@{
ComputerName = $ComputerName # The vessel’s name, emblazoned upon the object.
DisplayName = $_.DisplayName # The service’s grand title for all to see.
ServiceName = $_.Name # The service’s secret name, whispered in the digital winds.
StartType = $_.StartType # The manner in which the service is summoned at dawn.
}
}
return $customObjects # The fruits of our labor, presented with pride.
} catch {
Write-Error “Blast! An aberration has befallen our quest: $_. The gears have jammed, and the steam has ceased!” # Alas, even the best-laid plans may go awry.
}
} else {
Write-Host “Drat! The computer $ComputerName appears to have vanished into the aether. No puffs of smoke nor hums of electricity; it’s simply not reachable on the network.” # A somber note, the leviathan is beyond our grasp.
}

}

The error messages now reflect a more vivid steampunk essence, painting a picture of a world where technology intertwines with Victorian elegance and industrial machinery. May your coding adventures be as grand as the airships of yore!