Skip to content
This repository was archived by the owner on Jan 21, 2021. It is now read-only.

Commit 85b374c

Browse files
committed
-Fixes for PSScriptAnalyzer compatibility
-More error handling
1 parent 07ccc07 commit 85b374c

File tree

1 file changed

+114
-18
lines changed

1 file changed

+114
-18
lines changed

Recon/PowerView.ps1

Lines changed: 114 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,7 +3369,13 @@ Outputs custom PSObjects with detailed information about the DNS zone.
33693369
$Out.PSObject.TypeNames.Insert(0, 'PowerView.DNSZone')
33703370
$Out
33713371
}
3372-
try { $Results.dispose() } catch {}
3372+
3373+
if ($Results) {
3374+
try { $Results.dispose() }
3375+
catch {
3376+
Write-Verbose "[Get-DomainDFSShare] Error disposing of the Results object: $_"
3377+
}
3378+
}
33733379
$DNSSearcher1.dispose()
33743380
}
33753381

@@ -3386,7 +3392,12 @@ Outputs custom PSObjects with detailed information about the DNS zone.
33863392
$Out.PSObject.TypeNames.Insert(0, 'PowerView.DNSZone')
33873393
$Out
33883394
}
3389-
try { $Results.dispose() } catch {}
3395+
if ($Results) {
3396+
try { $Results.dispose() }
3397+
catch {
3398+
Write-Verbose "[Get-DomainDNSZone] Error disposing of the Results object: $_"
3399+
}
3400+
}
33903401
}
33913402
catch {
33923403
Write-Verbose "[Get-DomainDNSZone] Error accessing 'CN=MicrosoftDNS,DC=DomainDnsZones'"
@@ -3554,7 +3565,13 @@ Outputs custom PSObjects with detailed information about the DNS record entry.
35543565
$Out
35553566
}
35563567
}
3557-
try { $Results.dispose() } catch {}
3568+
3569+
if ($Results) {
3570+
try { $Results.dispose() }
3571+
catch {
3572+
Write-Verbose "[Get-DomainDNSRecord] Error disposing of the Results object: $_"
3573+
}
3574+
}
35583575
$DNSSearcher.dispose()
35593576
}
35603577
}
@@ -3622,7 +3639,7 @@ http://social.technet.microsoft.com/Forums/scriptcenter/en-US/0c5b3f83-e528-4d49
36223639
PROCESS {
36233640
if ($PSBoundParameters['Credential']) {
36243641

3625-
Write-Verbose "[Get-Domain] Using alternate credentials for Get-Domain"
3642+
Write-Verbose '[Get-Domain] Using alternate credentials for Get-Domain'
36263643

36273644
if ($PSBoundParameters['Domain']) {
36283645
$TargetDomain = $Domain
@@ -3652,7 +3669,12 @@ http://social.technet.microsoft.com/Forums/scriptcenter/en-US/0c5b3f83-e528-4d49
36523669
}
36533670
}
36543671
else {
3655-
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
3672+
try {
3673+
[System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
3674+
}
3675+
catch {
3676+
Write-Verbose "[Get-Domain] Error retrieving the current domain: $_"
3677+
}
36563678
}
36573679
}
36583680
}
@@ -4089,6 +4111,7 @@ the specified alternate credentials.
40894111
An ActiveDirectorySchemaClass returned from the found schema.
40904112
#>
40914113

4114+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
40924115
[OutputType([System.DirectoryServices.ActiveDirectory.ActiveDirectorySchemaClass])]
40934116
[CmdletBinding()]
40944117
Param(
@@ -4555,6 +4578,7 @@ PowerView.User.Raw
45554578
The raw DirectoryServices.SearchResult object, if -Raw is enabled.
45564579
#>
45574580

4581+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
45584582
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
45594583
[OutputType('PowerView.User')]
45604584
[OutputType('PowerView.User.Raw')]
@@ -4733,7 +4757,12 @@ The raw DirectoryServices.SearchResult object, if -Raw is enabled.
47334757
}
47344758
$User
47354759
}
4736-
try { $Results.dispose() } catch {}
4760+
if ($Results) {
4761+
try { $Results.dispose() }
4762+
catch {
4763+
Write-Verbose "[Get-DomainUser] Error disposing of the Results object: $_"
4764+
}
4765+
}
47374766
$UserSearcher.dispose()
47384767
}
47394768
}
@@ -4825,6 +4854,7 @@ DirectoryServices.AccountManagement.UserPrincipal
48254854
http://richardspowershellblog.wordpress.com/2008/05/25/system-directoryservices-accountmanagement/
48264855
#>
48274856

4857+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
48284858
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
48294859
[OutputType('DirectoryServices.AccountManagement.UserPrincipal')]
48304860
Param(
@@ -4968,6 +4998,7 @@ DirectoryServices.AccountManagement.UserPrincipal
49684998
http://richardspowershellblog.wordpress.com/2008/05/25/system-directoryservices-accountmanagement/
49694999
#>
49705000

5001+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
49715002
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
49725003
[OutputType('DirectoryServices.AccountManagement.UserPrincipal')]
49735004
Param(
@@ -5387,7 +5418,12 @@ http://blogs.technet.com/b/ashleymcglone/archive/2013/03/25/active-directory-ou-
53875418
$Results | Where-Object {$_} | ForEach-Object {
53885419
$GUIDs[(New-Object Guid (,$_.properties.schemaidguid[0])).Guid] = $_.properties.name[0]
53895420
}
5390-
try { $Results.dispose() } catch {}
5421+
if ($Results) {
5422+
try { $Results.dispose() }
5423+
catch {
5424+
Write-Verbose "[Get-DomainGUIDMap] Error disposing of the Results object: $_"
5425+
}
5426+
}
53915427
$SchemaSearcher.dispose()
53925428
}
53935429
catch {
@@ -5405,7 +5441,12 @@ http://blogs.technet.com/b/ashleymcglone/archive/2013/03/25/active-directory-ou-
54055441
$Results | Where-Object {$_} | ForEach-Object {
54065442
$GUIDs[$_.properties.rightsguid[0].toString()] = $_.properties.name[0]
54075443
}
5408-
try { $Results.dispose() } catch {}
5444+
if ($Results) {
5445+
try { $Results.dispose() }
5446+
catch {
5447+
Write-Verbose "[Get-DomainGUIDMap] Error disposing of the Results object: $_"
5448+
}
5449+
}
54095450
$RightsSearcher.dispose()
54105451
}
54115452
catch {
@@ -5761,7 +5802,12 @@ The raw DirectoryServices.SearchResult object, if -Raw is enabled.
57615802
$Computer
57625803
}
57635804
}
5764-
try { $Results.dispose() } catch {}
5805+
if ($Results) {
5806+
try { $Results.dispose() }
5807+
catch {
5808+
Write-Verbose "[Get-DomainComputer] Error disposing of the Results object: $_"
5809+
}
5810+
}
57655811
$CompSearcher.dispose()
57665812
}
57675813
}
@@ -5900,6 +5946,7 @@ PowerView.ADObject.Raw
59005946
The raw DirectoryServices.SearchResult object, if -Raw is enabled.
59015947
#>
59025948

5949+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
59035950
[OutputType('PowerView.ADObject')]
59045951
[OutputType('PowerView.ADObject.Raw')]
59055952
[CmdletBinding()]
@@ -6044,7 +6091,12 @@ The raw DirectoryServices.SearchResult object, if -Raw is enabled.
60446091
}
60456092
$Object
60466093
}
6047-
try { $Results.dispose() } catch {}
6094+
if ($Results) {
6095+
try { $Results.dispose() }
6096+
catch {
6097+
Write-Verbose "[Get-DomainObject] Error disposing of the Results object: $_"
6098+
}
6099+
}
60486100
$ObjectSearcher.dispose()
60496101
}
60506102
}
@@ -7622,7 +7674,12 @@ Custom PSObject with translated OU property fields.
76227674
$OU.PSObject.TypeNames.Insert(0, 'PowerView.OU')
76237675
$OU
76247676
}
7625-
try { $Results.dispose() } catch {}
7677+
if ($Results) {
7678+
try { $Results.dispose() }
7679+
catch {
7680+
Write-Verbose "[Get-DomainOU] Error disposing of the Results object: $_"
7681+
}
7682+
}
76267683
$OUSearcher.dispose()
76277684
}
76287685
}
@@ -7876,7 +7933,12 @@ Custom PSObject with translated site property fields.
78767933
$Site.PSObject.TypeNames.Insert(0, 'PowerView.Site')
78777934
$Site
78787935
}
7879-
try { $Results.dispose() } catch {}
7936+
if ($Results) {
7937+
try { $Results.dispose() }
7938+
catch {
7939+
Write-Verbose "[Get-DomainSite] Error disposing of the Results object"
7940+
}
7941+
}
78807942
$SiteSearcher.dispose()
78817943
}
78827944
}
@@ -8137,7 +8199,12 @@ Custom PSObject with translated subnet property fields.
81378199
$Subnet
81388200
}
81398201
}
8140-
try { $Results.dispose() } catch {}
8202+
if ($Results) {
8203+
try { $Results.dispose() }
8204+
catch {
8205+
Write-Verbose "[Get-DomainSubnet] Error disposing of the Results object: $_"
8206+
}
8207+
}
81418208
$SubnetSearcher.dispose()
81428209
}
81438210
}
@@ -8411,6 +8478,7 @@ Custom PSObject with translated group property fields.
84118478
#>
84128479

84138480
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
8481+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
84148482
[OutputType('PowerView.Group')]
84158483
[CmdletBinding(DefaultParameterSetName = 'AllowDelegation')]
84168484
Param(
@@ -8595,7 +8663,12 @@ Custom PSObject with translated group property fields.
85958663
$Group.PSObject.TypeNames.Insert(0, 'PowerView.Group')
85968664
$Group
85978665
}
8598-
try { $Results.dispose() } catch {}
8666+
if ($Results) {
8667+
try { $Results.dispose() }
8668+
catch {
8669+
Write-Verbose "[Get-DomainGroup] Error disposing of the Results object"
8670+
}
8671+
}
85998672
$GroupSearcher.dispose()
86008673
}
86018674
}
@@ -8667,6 +8740,7 @@ Creates the 'TestGroup' group with the specified description using the specified
86678740
DirectoryServices.AccountManagement.GroupPrincipal
86688741
#>
86698742

8743+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '')]
86708744
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
86718745
[OutputType('DirectoryServices.AccountManagement.GroupPrincipal')]
86728746
Param(
@@ -9137,6 +9211,7 @@ http://www.powershellmagazine.com/2013/05/23/pstip-retrieve-group-membership-of-
91379211
#>
91389212

91399213
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
9214+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
91409215
[OutputType('PowerView.GroupMember')]
91419216
[CmdletBinding(DefaultParameterSetName = 'None')]
91429217
Param(
@@ -10117,7 +10192,12 @@ A custom PSObject describing the distributed file systems.
1011710192
}
1011810193
}
1011910194
}
10120-
try { $Results.dispose() } catch {}
10195+
if ($Results) {
10196+
try { $Results.dispose() }
10197+
catch {
10198+
Write-Verbose "[Get-DomainDFSShare] Get-DomainDFSShareV1 error disposing of the Results object: $_"
10199+
}
10200+
}
1012110201
$DFSSearcher.dispose()
1012210202

1012310203
if ($pkt -and $pkt[0]) {
@@ -10194,7 +10274,12 @@ A custom PSObject describing the distributed file systems.
1019410274
}
1019510275
}
1019610276
}
10197-
try { $Results.dispose() } catch {}
10277+
if ($Results) {
10278+
try { $Results.dispose() }
10279+
catch {
10280+
Write-Verbose "[Get-DomainDFSShare] Error disposing of the Results object: $_"
10281+
}
10282+
}
1019810283
$DFSSearcher.dispose()
1019910284
}
1020010285
catch {
@@ -10601,6 +10686,7 @@ The raw DirectoryServices.SearchResult object, if -Raw is enabled.
1060110686
#>
1060210687

1060310688
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSShouldProcess', '')]
10689+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '')]
1060410690
[OutputType('PowerView.GPO')]
1060510691
[OutputType('PowerView.GPO.Raw')]
1060610692
[CmdletBinding(DefaultParameterSetName = 'None')]
@@ -10870,7 +10956,12 @@ The raw DirectoryServices.SearchResult object, if -Raw is enabled.
1087010956
}
1087110957
$GPO
1087210958
}
10873-
try { $Results.dispose() } catch {}
10959+
if ($Results) {
10960+
try { $Results.dispose() }
10961+
catch {
10962+
Write-Verbose "[Get-DomainGPO] Error disposing of the Results object: $_"
10963+
}
10964+
}
1087410965
$GPOSearcher.dispose()
1087510966
}
1087610967
}
@@ -17457,7 +17548,12 @@ Custom PSObject with translated domain API trust result fields.
1745717548
$DomainTrust.PSObject.TypeNames.Insert(0, 'PowerView.DomainTrust.LDAP')
1745817549
$DomainTrust
1745917550
}
17460-
try { $Results.dispose() } catch {}
17551+
if ($Results) {
17552+
try { $Results.dispose() }
17553+
catch {
17554+
Write-Verbose "[Get-DomainTrust] Error disposing of the Results object: $_"
17555+
}
17556+
}
1746117557
$TrustSearcher.dispose()
1746217558
}
1746317559
}

0 commit comments

Comments
 (0)