Skip to content

Commit ffa2b33

Browse files
committed
2 parents bf96000 + 3fdaf47 commit ffa2b33

File tree

4 files changed

+191
-4
lines changed

4 files changed

+191
-4
lines changed

O365-GROUP-Get-DistributionGroupRecursive/O365-GROUP-Get-DistributionGroupRecursive.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ function Get-DistributionGroupMemberRecursive
55
.SYNOPSIS
66
This script will list all the members (recursively) of a DistributionGroup
77
.EXAMPLE
8-
Get-DistributionGroupMemberRecursive -Group mtl-wbgames_metallica_all -Verbose
8+
Get-DistributionGroupMemberRecursive -Group TestDG -Verbose
99
.NOTES
1010
Francois-Xavier Cat
11-
2015/02/18
12-
11+
www.lazywinadmin.com
12+
@lazywinadm
1313
#>
1414
[CmdletBinding()]
1515
PARAM ($Group)
@@ -63,4 +63,4 @@ function Get-DistributionGroupMemberRecursive
6363
{
6464
Write-Verbose -message "[END] Done"
6565
}
66-
}
66+
}

O365-Get-O365CalendarEvent/O365-Get-O365CalendarEvent.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
3838
Get the calendar Events Subject, StartTimeZone,Start, End, Attendees for the last 7 days
3939
.NOTES
40+
Francois-Xavier Cat
41+
www.lazywinadmin.com
42+
@lazywinadm
43+
4044
https://msdn.microsoft.com/office/office365/api/calendar-rest-operations
4145
#>
4246
[CmdletBinding()]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
function Expand-ScriptAlias
2+
{
3+
<#
4+
.SYNOPSIS
5+
Function to replace Aliases used in a script by their fullname
6+
7+
.DESCRIPTION
8+
Function to replace Aliases used in a script by their fullname.
9+
Using PowerShell AST we are able to retrieve the functions and cmdlets used in a script.
10+
11+
.PARAMETER Path
12+
Specifies the Path to the file.
13+
Alias: FullName
14+
15+
.EXAMPLE
16+
"C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias
17+
18+
.EXAMPLE
19+
gci C:\LazyWinAdmin -File | Expand-ScriptAlias
20+
21+
.EXAMPLE
22+
Expand-ScriptAlias -Path "C:\LazyWinAdmin\testscript.ps1"
23+
24+
.EXAMPLE
25+
"C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias -Confirm
26+
27+
.EXAMPLE
28+
"C:\LazyWinAdmin\testscript.ps1", "C:\LazyWinAdmin\testscript2.ps1" | Expand-ScriptAlias -WhatIf
29+
30+
What if: Performing the operation "Expand Alias: select to Select-Object (startoffset: 15)" on target "C:\LazyWinAdmin\testscript2.ps1".
31+
What if: Performing the operation "Expand Alias: sort to Sort-Object (startoffset: 10)" on target "C:\LazyWinAdmin\testscript2.ps1".
32+
What if: Performing the operation "Expand Alias: group to Group-Object (startoffset: 4)" on target "C:\LazyWinAdmin\testscript2.ps1".
33+
What if: Performing the operation "Expand Alias: gci to Get-ChildItem (startoffset: 0)" on target "C:\LazyWinAdmin\testscript2.ps1".
34+
35+
.NOTES
36+
Francois-Xavier Cat
37+
www.lazywinadmin.com
38+
@lazywinadm
39+
#>
40+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')]
41+
PARAM (
42+
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
43+
[ValidateScript({ Test-Path -Path $_ })]
44+
[Alias('FullName')]
45+
[System.String]$Path
46+
)
47+
PROCESS
48+
{
49+
FOREACH ($File in $Path)
50+
{
51+
Write-Verbose -Message '[PROCESS] $File'
52+
53+
TRY
54+
{
55+
# Retrieve file content
56+
$ScriptContent = (Get-Content $File -Delimiter $([char]0))
57+
58+
# AST Parsing
59+
$AbstractSyntaxTree = [System.Management.Automation.Language.Parser]::
60+
ParseInput($ScriptContent, [ref]$null, [ref]$null)
61+
62+
# Find Aliases
63+
$Aliases = $AbstractSyntaxTree.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true) |
64+
ForEach-Object -Process {
65+
$Command = $_.CommandElements[0]
66+
if ($Alias = Get-Alias | Where-Object { $_.Name -eq $Command })
67+
{
68+
69+
# Output information
70+
[PSCustomObject]@{
71+
File = $File
72+
Alias = $Alias.Name
73+
Definition = $Alias.Definition
74+
StartLineNumber = $Command.Extent.StartLineNumber
75+
EndLineNumber = $Command.Extent.EndLineNumber
76+
StartColumnNumber = $Command.Extent.StartColumnNumber
77+
EndColumnNumber = $Command.Extent.EndColumnNumber
78+
StartOffset = $Command.Extent.StartOffset
79+
EndOffset = $Command.Extent.EndOffset
80+
81+
}#[PSCustomObject]
82+
}#if ($Alias)
83+
} | Sort-Object -Property EndOffset -Descending
84+
85+
# The sort-object is important, we change the values from the end first to not lose the positions of every aliases.
86+
Foreach ($Alias in $Aliases)
87+
{
88+
# whatif and confirm support
89+
if ($psCmdlet.ShouldProcess($file, "Expand Alias: $($Alias.alias) to $($Alias.definition) (startoffset: $($alias.StartOffset))"))
90+
{
91+
# Remove alias and insert full cmldet name
92+
$ScriptContent = $ScriptContent.Remove($Alias.StartOffset, ($Alias.EndOffset - $Alias.StartOffset)).Insert($Alias.StartOffset, $Alias.Definition)
93+
# Apply to the file
94+
Set-Content -Path $File -Value $ScriptContent -Confirm:$false
95+
}
96+
}#ForEach Alias in Aliases
97+
98+
}#TRY
99+
CATCH
100+
{
101+
Write-Error -Message $($Error[0].Exception.Message)
102+
}
103+
}#FOREACH File in Path
104+
}#PROCESS
105+
}#Expand-ScriptAlias
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
function Get-ScriptAlias
2+
{
3+
<#
4+
.SYNOPSIS
5+
Function to retrieve the aliases inside a Powershell script file.
6+
7+
.DESCRIPTION
8+
Function to retrieve the aliases inside a Powershell script file.
9+
Using PowerShell AST Parser we are able to retrieve the functions and cmdlets used in the script.
10+
11+
.PARAMETER Path
12+
Specifies the path of the script
13+
14+
.EXAMPLE
15+
Get-ScriptAlias -Path "C:\LazyWinAdmin\testscript.ps1"
16+
17+
.EXAMPLE
18+
"C:\LazyWinAdmin\testscript.ps1" | Get-ScriptAlias
19+
20+
.EXAMPLE
21+
gci C:\LazyWinAdmin -file | Get-ScriptAlias
22+
23+
.NOTES
24+
Francois-Xavier Cat
25+
www.lazywinadmin.com
26+
@lazywinadm
27+
#>
28+
[CmdletBinding()]
29+
PARAM
30+
(
31+
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
32+
[ValidateScript({ Test-Path -Path $_ })]
33+
[Alias("FullName")]
34+
[System.String[]]$Path
35+
)
36+
PROCESS
37+
{
38+
FOREACH ($File in $Path)
39+
{
40+
TRY
41+
{
42+
# Retrieve file content
43+
$ScriptContent = (Get-Content $File -Delimiter $([char]0))
44+
45+
# AST Parsing
46+
$AbstractSyntaxTree = [System.Management.Automation.Language.Parser]::
47+
ParseInput($ScriptContent, [ref]$null, [ref]$null)
48+
49+
# Find Aliases
50+
$AbstractSyntaxTree.FindAll({ $args[0] -is [System.Management.Automation.Language.CommandAst] }, $true) |
51+
ForEach-Object -Process {
52+
$Command = $_.CommandElements[0]
53+
if ($Alias = Get-Alias | Where-Object { $_.Name -eq $Command })
54+
{
55+
56+
# Output information
57+
[PSCustomObject]@{
58+
File = $File
59+
Alias = $Alias.Name
60+
Definition = $Alias.Definition
61+
StartLineNumber = $Command.Extent.StartLineNumber
62+
EndLineNumber = $Command.Extent.EndLineNumber
63+
StartColumnNumber = $Command.Extent.StartColumnNumber
64+
EndColumnNumber = $Command.Extent.EndColumnNumber
65+
StartOffset = $Command.Extent.StartOffset
66+
EndOffset = $Command.Extent.EndOffset
67+
68+
}#[PSCustomObject]
69+
}#if ($Alias)
70+
}#ForEach-Object
71+
}#TRY
72+
CATCH
73+
{
74+
Write-Error -Message $($Error[0].Exception.Message)
75+
} #CATCH
76+
}#FOREACH ($File in $Path)
77+
} #PROCESS
78+
}

0 commit comments

Comments
 (0)