Skip to content

Commit 0bb9e70

Browse files
committed
🚨 Fix more style issues
1 parent b0757fc commit 0bb9e70

5 files changed

+198
-194
lines changed

‎Get-PocketArticles.ps1

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,10 @@ By default, the default vault is used.
8080
Set-ParameterDefault.ps1 Invoke-RestMethod Method Post
8181
Set-ParameterDefault.ps1 Invoke-RestMethod ContentType application/json
8282
Set-ParameterDefault.ps1 Invoke-RestMethod Headers @{'X-Accept'='application/json'}
83-
$consumerKey = try{( $Vault ?
83+
$consumerKey = ( $Vault ?
8484
(Get-Secret PocketApiConsumerKey -Vault $Vault -ErrorAction Ignore) :
8585
(Get-Secret PocketApiConsumerKey -ErrorAction Ignore) ) |
86-
ConvertFrom-SecureString -AsPlainText}
87-
catch{}
86+
ConvertFrom-SecureString -AsPlainText -ErrorAction Ignore
8887
if(!$consumerKey)
8988
{
9089
$consumerKey = Get-Credential PocketApiConsumerKey -Message 'Pocket API consumer key'

‎Optimize-Path.ps1

+89-86
Original file line numberDiff line numberDiff line change
@@ -15,111 +15,114 @@ with the newest version ahead of the others.
1515

1616
function Initialize-PathCollection
1717
{
18-
# initialize collections
19-
$Script:app,$Script:user = @{},@()
20-
# get command extensions
21-
$Script:pathext = $env:PATHEXT -split ';'
22-
# get list of directory environment variables
23-
$Script:evmatch = Get-ChildItem env: |
24-
Where-Object {Test-Path $_.Value -PathType Container} |
25-
Where-Object {'windir','TMP','ProgramW6432','CommonProgramW6432','SystemDrive','HomeDrive','HomePath' -inotcontains $_.Name} |
26-
ForEach-Object {$_.Value = '^' + ($_.Value.Trim('\') -replace '(\W)','\$1') + '(?=\\|$)'; $_} |
27-
Sort-Object @{e={$_.Value.Length};asc=$false},@{e={$_.Name.Length};asc=$true}
18+
# initialize collections
19+
$Script:app,$Script:user = @{},@()
20+
# get command extensions
21+
$Script:pathext = $env:PATHEXT -split ';'
22+
# get list of directory environment variables
23+
$Script:evmatch = Get-ChildItem env: |
24+
Where-Object {Test-Path $_.Value -PathType Container} |
25+
Where-Object {'windir','TMP','ProgramW6432','CommonProgramW6432','SystemDrive','HomeDrive','HomePath' -inotcontains $_.Name} |
26+
ForEach-Object {$_.Value = '^' + ($_.Value.Trim('\') -replace '(\W)','\$1') + '(?=\\|$)'; $_} |
27+
Sort-Object @{e={$_.Value.Length};asc=$false},@{e={$_.Name.Length};asc=$true}
2828
}
2929

3030
function Backup-Path([Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target)
3131
{
32-
mkdir $env:LOCALAPPDATA\OptimizePath |Out-Null
33-
"$(Get-Date -Format u)`t$($Target)`t$([Environment]::GetEnvironmentVariable('Path',$Target))" |
34-
Add-Content -Path $env:LOCALAPPDATA\OptimizePath\Backup.tsv
32+
mkdir $env:LOCALAPPDATA\OptimizePath |Out-Null
33+
"$(Get-Date -Format u)`t$($Target)`t$([Environment]::GetEnvironmentVariable('Path',$Target))" |
34+
Add-Content -Path $env:LOCALAPPDATA\OptimizePath\Backup.tsv
3535
}
3636

3737
filter Get-PathDetail([Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target,
38-
[Parameter(Position=1,ValueFromPipeline=$true)][string]$Entry)
38+
[Parameter(Position=1,ValueFromPipeline=$true)][string]$Entry)
3939
{
40-
if([string]::IsNullOrWhiteSpace($Entry)) {return}
41-
$fullpath = [Environment]::ExpandEnvironmentVariables($Entry)
42-
Write-Verbose "$Target Path: $Entry$(if($Entry -ne $fullpath){' ('+$fullpath+')'})"
43-
if(!(Test-Path $fullpath -PathType Container)) {Write-Warning "$Target Path: Entry $Entry not found!"; return}
44-
if($Target -ne 'User' -and $entry.StartsWith("$env:USERPROFILE\")) {Write-Warning "$Target Path: Entry $entry under user profile!"; $user+= $entry; continue}
45-
[IO.FileInfo[]]$cmd = Get-ChildItem $fullpath -File |Where-Object {$pathext -icontains $_.Extension}
46-
if(!$cmd) {Write-Warning "$Target Path: Entry $Entry contains no executables."; return}
47-
elseif($cmd.Count -eq 1) {Write-Warning "$Target Path: Entry $Entry contains only one command! ($($cmd[0].Name))"}
48-
else {Write-Verbose "$Target Path: Entry $Entry contains $($cmd.Count) commands."}
49-
foreach($c in $cmd)
50-
{
51-
if($app[$c.Name]) {$app[$c.Name]+=$Entry}
52-
else {[string[]]$app[$c.Name] = $Entry}
53-
}
54-
$ev = $evmatch |Where-Object {$Entry -match $_.Value} |Select-Object -First 1
55-
if($ev) {Write-Verbose "$Target Path: $entry matches $($ev.Name) /$($ev.Value)/"}
56-
$evpath =
57-
if($Entry -like '%*') {$Entry}
58-
elseif(!$ev) {$fullpath}
59-
else {$Entry -ireplace $ev.Value,"%$($ev.Name)%"}
60-
return [pscustomobject]@{
61-
Name = $Entry
62-
Path = $fullpath
63-
EnvVarPath = $evpath
64-
Commands = $cmd
65-
Precede = [string[]]@()
66-
}
40+
if([string]::IsNullOrWhiteSpace($Entry)) {return}
41+
$fullpath = [Environment]::ExpandEnvironmentVariables($Entry)
42+
Write-Verbose "$Target Path: $Entry$(if($Entry -ne $fullpath){' ('+$fullpath+')'})"
43+
if(!(Test-Path $fullpath -PathType Container)) {Write-Warning "$Target Path: Entry $Entry not found!"; return}
44+
if($Target -ne 'User' -and $entry.StartsWith("$env:USERPROFILE\")) {Write-Warning "$Target Path: Entry $entry under user profile!"; $user+= $entry; continue}
45+
[IO.FileInfo[]]$cmd = Get-ChildItem $fullpath -File |Where-Object {$pathext -icontains $_.Extension}
46+
if(!$cmd) {Write-Warning "$Target Path: Entry $Entry contains no executables."; return}
47+
elseif($cmd.Count -eq 1) {Write-Warning "$Target Path: Entry $Entry contains only one command! ($($cmd[0].Name))"}
48+
else {Write-Verbose "$Target Path: Entry $Entry contains $($cmd.Count) commands."}
49+
foreach($c in $cmd)
50+
{
51+
if($app[$c.Name]) {$app[$c.Name]+=$Entry}
52+
else {[string[]]$app[$c.Name] = $Entry}
53+
}
54+
$ev = $evmatch |Where-Object {$Entry -match $_.Value} |Select-Object -First 1
55+
if($ev) {Write-Verbose "$Target Path: $entry matches $($ev.Name) /$($ev.Value)/"}
56+
$evpath =
57+
if($Entry -like '%*') {$Entry}
58+
elseif(!$ev) {$fullpath}
59+
else {$Entry -ireplace $ev.Value,"%$($ev.Name)%"}
60+
return [pscustomobject]@{
61+
Name = $Entry
62+
Path = $fullpath
63+
EnvVarPath = $evpath
64+
Commands = $cmd
65+
Precede = [string[]]@()
66+
}
6767
}
6868

6969
function Get-PathDetail([Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target)
7070
{
71-
$path = [Environment]::GetEnvironmentVariable('Path',$Target) -split ';'
72-
switch($Target)
73-
{
74-
Machine {$path |Get-PathDetail $Target}
75-
User {($Script:user + $path) |Get-PathDetail $Target}
76-
}
71+
$path = [Environment]::GetEnvironmentVariable('Path',$Target) -split ';'
72+
switch($Target)
73+
{
74+
Machine {$path |Get-PathDetail $Target}
75+
User {($Script:user + $path) |Get-PathDetail $Target}
76+
}
7777
}
7878

7979
function Resolve-PathConflict([Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target,
80-
[Parameter(Position=1,Mandatory=$true)][Collections.Generic.List[psobject]]$PathDetails)
80+
[Parameter(Position=1,Mandatory=$true)][Collections.Generic.List[psobject]]$PathDetails)
8181
{
82-
# examine conflicts
83-
if(!(Get-Command -Verb Test -Noun NewerFile)) {Set-Alias Test-NewerFile "$PSScriptRoot\Test-NewerFile.ps1"}
84-
foreach($c in $app.Keys |Where-Object {$app.$_.Count -gt 1})
85-
{
86-
$newest,$rest = $app[$c]
87-
[string[]]$precede = @()
88-
foreach($r in $rest)
89-
{
90-
if(Test-NewerFile (Join-Path $newest $c) (Join-Path $r $c)) {$precede += $newest; $newest = $r}
91-
}
92-
if($precede)
93-
{
94-
Write-Verbose "Executable conflict ${c}: $newest needs to be moved ahead of $($precede -join ', ')."
95-
$npos,$rpos = 0,0
96-
foreach($i in 0..($precede.Length))
97-
{
98-
if($PathDetails[$i].Name -eq $newest) {$npos = $i}
99-
elseif($rpos -ne 0 -and $precede -contains $PathDetails[$i].Name) {$rpos = $i}
100-
}
101-
if($npos -gt $rpos)
102-
{
103-
$n = $PathDetails[$npos]
104-
$PathDetails.RemoveAt($npos)
105-
$PathDetails.Insert($rpos,$n)
106-
}
107-
}
108-
}
109-
return $PathDetails
82+
# examine conflicts
83+
if(!(Get-Command -Verb Test -Noun NewerFile)) {Set-Alias Test-NewerFile "$PSScriptRoot\Test-NewerFile.ps1"}
84+
foreach($c in $app.Keys |Where-Object {$app.$_.Count -gt 1})
85+
{
86+
$newest,$rest = $app[$c]
87+
[string[]]$precede = @()
88+
foreach($r in $rest)
89+
{
90+
if(Test-NewerFile (Join-Path $newest $c) (Join-Path $r $c)) {$precede += $newest; $newest = $r}
91+
}
92+
if($precede)
93+
{
94+
Write-Verbose "Executable conflict ${c}: $newest needs to be moved ahead of $($precede -join ', ')."
95+
$npos,$rpos = 0,0
96+
foreach($i in 0..($precede.Length))
97+
{
98+
if($PathDetails[$i].Name -eq $newest) {$npos = $i}
99+
elseif($rpos -ne 0 -and $precede -contains $PathDetails[$i].Name) {$rpos = $i}
100+
}
101+
if($npos -gt $rpos)
102+
{
103+
$n = $PathDetails[$npos]
104+
$PathDetails.RemoveAt($npos)
105+
$PathDetails.Insert($rpos,$n)
106+
}
107+
}
108+
}
109+
return $PathDetails
110110
}
111111

112-
function Update-Path([Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target)
112+
function Update-Path
113113
{
114-
[Collections.Generic.List[psobject]]$path = Get-PathDetail $Target
115-
if($ResolveConflicts) {$path = Resolve-PathConflict $path}
116-
if($PSCmdlet.ShouldProcess("$Target Path",'Update'))
117-
{
118-
$newpath = ($path |Select-Object -Unique -ExpandProperty EnvVarPath) -join ';'
119-
Write-Verbose "New $Target Path: $newpath"
120-
Backup-Path $Target
121-
[Environment]::SetEnvironmentVariable('Path',$newpath,$Target)
122-
}
114+
[CmdletBinding(SupportsShouldProcess=$true)] Param(
115+
[Parameter(Position=0,Mandatory=$true)][EnvironmentVariableTarget]$Target
116+
)
117+
[Collections.Generic.List[psobject]]$path = Get-PathDetail $Target
118+
if($ResolveConflicts) {$path = Resolve-PathConflict $path}
119+
if($PSCmdlet.ShouldProcess("$Target Path",'Update'))
120+
{
121+
$newpath = ($path |Select-Object -Unique -ExpandProperty EnvVarPath) -join ';'
122+
Write-Verbose "New $Target Path: $newpath"
123+
Backup-Path $Target
124+
[Environment]::SetEnvironmentVariable('Path',$newpath,$Target)
125+
}
123126
}
124127

125128
Initialize-PathCollection

‎Repair-DatabaseConstraintNames.ps1

+38-37
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ WARNING: Renamed 10 defaults
4040

4141
Use-SqlcmdParams.ps1
4242

43-
function Resolve-SqlcmdResult([string]$Action,[string]$Query)
43+
function Resolve-SqlcmdResult
4444
{
45-
<#
45+
<#
4646
.SYNOPSIS
4747
Executes SQL that generates SQL strings, and optionally executes the generated SQL.
4848
@@ -55,77 +55,78 @@ Descriptive text for the commands produced, with two format arguments:
5555
A SQL query that produces a single-column result set, named "command", containing
5656
executable SQL.
5757
#>
58-
$count,$i = 0,0
59-
[string[]]$commands = Invoke-Sqlcmd $Query |Select-Object -ExpandProperty command
60-
if(!$commands){return}
61-
$max,$act = ($commands.Count/100),($Action -f -1,$commands.Count)
62-
Write-Verbose ($Action -f 1,$commands.Count)
63-
foreach($command in $commands)
64-
{
65-
Write-Progress $act "Execute command #$i" -CurrentOperation $command -PercentComplete ($i++/$max)
66-
if(!$Update) {$command}
67-
elseif($PSCmdlet.ShouldProcess($command,'execute')) {Invoke-Sqlcmd $command; $count++}
68-
}
69-
Write-Progress ($action -f 0,$i) -Completed
70-
if($count) {Write-Warning ($Action -f 0,$count)}
58+
[CmdletBinding(SupportsShouldProcess=$true)] Param([string]$Action,[string]$Query)
59+
$count,$i = 0,0
60+
[string[]]$commands = Invoke-Sqlcmd $Query |Select-Object -ExpandProperty command
61+
if(!$commands){return}
62+
$max,$act = ($commands.Count/100),($Action -f -1,$commands.Count)
63+
Write-Verbose ($Action -f 1,$commands.Count)
64+
foreach($command in $commands)
65+
{
66+
Write-Progress $act "Execute command #$i" -CurrentOperation $command -PercentComplete ($i++/$max)
67+
if(!$Update) {$command}
68+
elseif($PSCmdlet.ShouldProcess($command,'execute')) {Invoke-Sqlcmd $command; $count++}
69+
}
70+
Write-Progress ($action -f 0,$i) -Completed
71+
if($count) {Write-Warning ($Action -f 0,$count)}
7172
}
7273

7374
function Repair-DefaultName
7475
{
75-
@{
76-
Action = 'Renam{0:e;ing;ed} {1} defaults'
77-
Query = @"
76+
@{
77+
Action = 'Renam{0:e;ing;ed} {1} defaults'
78+
Query = @"
7879
select 'if object_id(''' + quotename(schema_name(schema_id)) +'.'+ quotename(name)
79-
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
80-
+''', ''DF_'+object_name(parent_object_id)+'_'+col_name(parent_object_id,parent_column_id)
81-
+''', ''OBJECT'';' [command]
80+
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
81+
+''', ''DF_'+object_name(parent_object_id)+'_'+col_name(parent_object_id,parent_column_id)
82+
+''', ''OBJECT'';' [command]
8283
from sys.default_constraints
8384
where name like 'DF._._%' escape '.'
8485
and name <> 'DF_'+object_name(parent_object_id)+'_'+col_name(parent_object_id,parent_column_id)
8586
and objectproperty(parent_object_id,'IsUserTable') = 1 -- excludes 'sys' schema, &c
8687
and objectproperty(parent_object_id,'IsMsShipped') = 0 -- excludes dtproperties, &c
8788
and parent_object_id not in (select major_id from sys.extended_properties
88-
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
89+
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
8990
"@
90-
} |ForEach-Object {Resolve-SqlcmdResult @_}
91+
} |ForEach-Object {Resolve-SqlcmdResult @_}
9192
}
9293

9394
function Repair-PrimaryKeyName
9495
{
95-
@{
96-
Action = 'Renam{0:e;ing;ed} {1} primary keys'
97-
Query = @"
96+
@{
97+
Action = 'Renam{0:e;ing;ed} {1} primary keys'
98+
Query = @"
9899
select 'if object_id(''' + quotename(schema_name(schema_id)) +'.'+ quotename(name)
99-
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
100-
+''', '''+'PK_'+object_name(parent_object_id)+''', ''OBJECT'';' command
100+
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
101+
+''', '''+'PK_'+object_name(parent_object_id)+''', ''OBJECT'';' command
101102
from sys.key_constraints
102103
where name like 'PK._._%' escape '.'
103104
and name <> 'PK_'+object_name(parent_object_id)
104105
and objectproperty(parent_object_id,'IsUserTable') = 1 -- excludes 'sys' schema, &c
105106
and objectproperty(parent_object_id,'IsMsShipped') = 0 -- excludes dtproperties, &c
106107
and parent_object_id not in (select major_id from sys.extended_properties
107-
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
108+
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
108109
"@
109-
} |ForEach-Object {Resolve-SqlcmdResult @_}
110+
} |ForEach-Object {Resolve-SqlcmdResult @_}
110111
}
111112

112113
function Repair-ForeignKeyName
113114
{ #TODO: Mitigate possible deterministic naming collisions.
114-
@{
115-
Action = 'Renam{0:e;ing;ed} {1} foreign keys'
116-
Query = @"
115+
@{
116+
Action = 'Renam{0:e;ing;ed} {1} foreign keys'
117+
Query = @"
117118
select 'if object_id(''' + quotename(schema_name(schema_id)) +'.'+ quotename(name)
118-
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
119-
+''', '''+'FK_'+object_name(parent_object_id)+'_'+object_name(referenced_object_id)+''', ''OBJECT'';' command
119+
+''') is not null exec sp_rename '''+quotename(schema_name(schema_id))+'.'+quotename(name)
120+
+''', '''+'FK_'+object_name(parent_object_id)+'_'+object_name(referenced_object_id)+''', ''OBJECT'';' command
120121
from sys.foreign_keys
121122
where name like 'FK._._%' escape '.'
122123
and name <> 'FK_'+object_name(parent_object_id)
123124
and objectproperty(parent_object_id,'IsUserTable') = 1 -- excludes 'sys' schema, &c
124125
and objectproperty(parent_object_id,'IsMsShipped') = 0 -- excludes dtproperties, &c
125126
and parent_object_id not in (select major_id from sys.extended_properties
126-
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
127+
where class = 1 and minor_id = 0 and name = 'microsoft_database_tools_support'); -- excludes sysdiagrams, &c
127128
"@
128-
} |ForEach-Object {Resolve-SqlcmdResult @_}
129+
} |ForEach-Object {Resolve-SqlcmdResult @_}
129130
}
130131

131132
Repair-DefaultName

0 commit comments

Comments
 (0)