Skip to content

Commit 2f86ff1

Browse files
SamErdeCopilot
andcommitted
🐛 fix(scripts): address prior PR Copilot review follow-ups
- Make profile Prompt implementations null-safe when command history is empty - Add ShouldProcess support around New-gMSA Active Directory changes - Update stale remoting catch messages after replacing Enter-PSSession usage Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6f415df commit 2f86ff1

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

Active Directory/Domain Services/DNSZonesRemote.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ foreach ($srv in $servers) {
4848
}
4949
}
5050
} Catch {
51-
Write-Host -ForegroundColor DarkYellow "Failed to enter the PSSession for $server. Skipping."
51+
Write-Host -ForegroundColor DarkYellow "Failed to create or use the PSSession for $server. Skipping."
5252
Continue
5353
} Finally {
5454
if ($session) {

Active Directory/Set DNS Server Zone Settings via Registry.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ foreach ($srv in $servers) {
5555
}
5656
}
5757
} Catch {
58-
Write-Host "Failed to enter the PSSession for $server. Skipping." -ForegroundColor DarkYellow
58+
Write-Host "Failed to create or use the PSSession for $server. Skipping." -ForegroundColor DarkYellow
5959
Continue
6060
} Finally {
6161
if ($session) {

Profile and Prompt/PSProfileBase.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ if ($IsPowerShellISE -or (-not (Get-Command -Name 'oh-my-posh' -ErrorAction Sile
170170
# Custom prompt function that shows admin status, command ID, command duration, and path.
171171
function Prompt {
172172
$LastCommand = Get-History -Count 1 -ErrorAction SilentlyContinue
173+
$CommandId = if ($LastCommand) { $LastCommand.Id + 1 } else { 1 }
174+
$DurationMilliseconds = if ($LastCommand) { [math]::Ceiling($LastCommand.Duration.TotalMilliseconds) } else { 0 }
173175
Write-Host "$AdminStatus " -ForegroundColor "$AdminStatusColor" -NoNewline
174-
Write-Host "[$($LastCommand.Id +1)] $([math]::Ceiling($LastCommand.Duration.TotalMilliseconds))ms " -NoNewline -ForegroundColor White
176+
Write-Host "[$CommandId] ${DurationMilliseconds}ms " -NoNewline -ForegroundColor White
175177
Write-Host "$FolderGlyph $($PWD.ToString() -ireplace [regex]::Escape($HOME),'~')" -ForegroundColor Yellow
176178
Write-Host '>' -NoNewline -ForegroundColor White
177179
return ' '
@@ -190,8 +192,10 @@ if ($IsPowerShellISE -or (-not (Get-Command -Name 'oh-my-posh' -ErrorAction Sile
190192

191193
function Prompt {
192194
$LastCommand = Get-History -Count 1 -ErrorAction SilentlyContinue
195+
$CommandId = if ($LastCommand) { $LastCommand.Id + 1 } else { 1 }
196+
$DurationMilliseconds = if ($LastCommand) { [math]::Ceiling($LastCommand.Duration.TotalMilliseconds) } else { 0 }
193197
Write-Host "$AdminStatus " -ForegroundColor "$AdminStatusColor" -NoNewline
194-
Write-Host "[$($LastCommand.Id +1)] $([math]::Ceiling($LastCommand.Duration.TotalMilliseconds))ms " -NoNewline -ForegroundColor White
198+
Write-Host "[$CommandId] ${DurationMilliseconds}ms " -NoNewline -ForegroundColor White
195199
Write-Host "$FolderGlyph $($PWD.ToString() -ireplace [regex]::Escape($HOME),'~')" -ForegroundColor Yellow
196200
Write-Host '>' -NoNewline -ForegroundColor White
197201
return ' '

Windows/New-gMSA.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Function New-gMSA {
3737
.OUTPUTS
3838
None
3939
#>
40+
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
4041
param (
4142
[Parameter(Mandatory=$true)]
4243
[ValidateLength(1,15)]
@@ -65,7 +66,10 @@ Function New-gMSA {
6566
Import-Module ActiveDirectory
6667
$Group = "MSA " + $gMSA
6768
$DNS = "$gMSA.$DomainDnsName"
68-
New-ADGroup -Name $Group -GroupScope Global -DisplayName $Group -Description "Permission group for $gMSA" -Path $GroupPath -Credential $Credential
69-
Add-ADGroupMember -Identity $Group -Members ($Servers | ForEach-Object {Get-ADComputer -Identity $_ -Credential $Credential}) -Credential $Credential
70-
New-ADServiceAccount -Name $gMSA -DNSHostName $DNS -PrincipalsAllowedToRetrieveManagedPassword $Group -Path $ServiceAccountPath -Credential $Credential
69+
70+
if ($PSCmdlet.ShouldProcess($gMSA, 'Create gMSA, retrieval group, and group membership')) {
71+
New-ADGroup -Name $Group -GroupScope Global -DisplayName $Group -Description "Permission group for $gMSA" -Path $GroupPath -Credential $Credential
72+
Add-ADGroupMember -Identity $Group -Members ($Servers | ForEach-Object {Get-ADComputer -Identity $_ -Credential $Credential}) -Credential $Credential
73+
New-ADServiceAccount -Name $gMSA -DNSHostName $DNS -PrincipalsAllowedToRetrieveManagedPassword $Group -Path $ServiceAccountPath -Credential $Credential
74+
}
7175
}

0 commit comments

Comments
 (0)