Skip to content

Commit 6f415df

Browse files
SamErdeCopilot
andcommitted
🐛 fix(scripts): correct string interpolation, obsolete API, and ErrorActionPreference scope bugs
- Snippets\RemoteServerSessionLoop2.ps1: replace Enter-PSSession (interactive-only) with Invoke-Command -Session; fix string interpolation for \.ComputerName and \.State using \ subexpression syntax - DDI\DNS Reverse Lookup from CSV.ps1: replace obsolete GetHostbyAddress() with GetHostEntry() - Windows\Disable-Poshv2.ps1: fix \.Count not expanding in double-quoted string - AD Users\Set User EmailAddress from PrimaryAddress.ps1: fix \.Name not expanding in Write-Progress -Status string - DDI\Resolve-IPs.ps1: remove \Continue global assignment inside foreach loop; wrap DNS lookup in try/catch to suppress per-call errors without leaking preference scope Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4ce0d6a commit 6f415df

5 files changed

Lines changed: 17 additions & 14 deletions

File tree

Active Directory/AD Users/Set User EmailAddress from PrimaryAddress.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Write-Progress -Id 1 -Activity $MainScriptActivity -Status $MainScriptStatus -Pe
3232
foreach ($user in $users)
3333
{
3434
$i++
35-
Write-Progress -Id 2 -Activity $UserLoopActivity -Status "$i of $userCount - $user.Name" -PercentComplete ($i / $userCount * 100) -ParentId 1
35+
Write-Progress -Id 2 -Activity $UserLoopActivity -Status "$i of $userCount - $($user.Name)" -PercentComplete ($i / $userCount * 100) -ParentId 1
3636

3737
$addresses = $user | Select-Object -ExpandProperty ProxyAddresses
3838
If ($addresses)

DDI/DNS Reverse Lookup from CSV and Add Column with Hostname.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ $file = ""
22
$csv = Import-Csv $file
33
foreach ($row in $csv) {
44
$IP = $row.SourceIP
5-
$row.SourceName = ([System.Net.DNS]::GetHostbyAddress($IP)).Hostname
5+
$row.SourceName = ([System.Net.DNS]::GetHostEntry($IP)).Hostname
66
}
77
$csv | Export-Csv "results.csv" -NoTypeInformation

DDI/Resolve-IPs.ps1

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ $ResultList = @()
1414

1515

1616
foreach ($IP in $ListOfIPs) {
17-
$ErrorActionPreference = 'silentlycontinue'
1817
$Result = $null
1918

2019
Write-Host "Resolving $IP" -ForegroundColor Green
21-
$result = [System.Net.Dns]::gethostentry($IP)
20+
try {
21+
$result = [System.Net.Dns]::gethostentry($IP)
22+
} catch {
23+
$result = $null
24+
}
2225

2326
If ($Result) {
2427
$ResultList += "$IP," + [string]$Result.HostName

Snippets/RemoteServerSessionLoop2.ps1

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@ foreach ($server in $servers) {
1919

2020
Try {
2121
Write-Information "Connecting to $server... " -InformationAction Continue
22-
Enter-PSSession $session
22+
# Enter-PSSession is interactive-only; use Invoke-Command to run code in the remote session.
23+
Invoke-Command -Session $session -ScriptBlock {
24+
<#
25+
Code to be run on each remote server goes here.
26+
#>
27+
Write-Information 'Inner code.' -InformationAction Continue
28+
}
2329
} Catch {
24-
Write-Warning "Failed to enter the PSSession for $server. Skipping." -WarningAction Continue
30+
Write-Warning "Failed to connect to $server. Skipping." -WarningAction Continue
2531
Continue
2632
}
2733
Write-Output $session.State
2834

29-
<#
30-
Code to be run on each remote server go here.
31-
#>
32-
Write-Information 'Inner code.' -InformationAction Continue
33-
3435
#Cleanup and then show the current PSSession state.
35-
Exit-PSSession
3636
Remove-PSSession $session
37-
Write-Information "$session.ComputerName $session.State `n`n" -InformationAction Continue
37+
Write-Information "$($session.ComputerName) $($session.State) `n`n" -InformationAction Continue
3838
}

Windows/Disable-Poshv2.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $exclusions = @('', '', '')
1111
#Filtering out production servers as well as any SQL, Exchange, SharePoint (SPS), Telephony, rinf* servers, and any servers in the "Non Windows" OU.
1212
$servers = (Get-ADComputer -Filter 'OperatingSystem -like "Windows Server*" -and Enabled -eq "True" -and Name -notlike "*sps*"' `
1313
-SearchBase 'DC=DOMAIN,DC=COM' -SearchScope Subtree).Name | Where-Object { $_ -notin $Exclusions } | Sort-Object
14-
Write-Information "$servers.Count servers found."
14+
Write-Information "$($servers.Count) servers found."
1515

1616
foreach ($server in $servers) {
1717
if (Test-WSMan -ComputerName $server -ErrorAction Ignore) {

0 commit comments

Comments
 (0)