build.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#build module script
[cmdletbinding()]
Param(
    [string]$ModuleFileName = "GPOWMI.psm1"
    ,
    [string]$ProjectUrl = "https://bitbucket.org/torgro/gpowmi/overview"
    ,
    [switch]$Major
    ,
    [switch]$Minor
    ,
    [switch]$LoadModule
)
cd C:\Users\Tore\Dropbox\SourceTreeRepros\gpowmi -ErrorAction SilentlyContinue
$F = $MyInvocation.InvocationName
Write-Verbose -Message "$F - Starting build, getting files"

if(Get-Module -Name GPOWMI)
{
    Write-Verbose "$F - Removing FIMautomation snapIn and Powerfim module"
    Remove-FIMsnapin -Verbose
    Remove-Module PowerFIM -Verbose:$false
}
    
$fileList = Get-ChildItem -Filter .\functions\*.ps1 | where name -NotLike "*Tests*"

$ModuleName = (Get-ChildItem -Path $ModuleFileName -ErrorAction SilentlyContinue).BaseName
if(-not $ModuleName)
{
    $ModuleName = $ModuleFileName.Replace(".psm1","")
}
Write-Verbose -Message "$f - Modulename is $ModuleName"

$ExportedFunctions = New-Object System.Collections.ArrayList
$fileList | foreach {
    Write-Verbose -Message "$F - Function = $($_.BaseName) added"
    [void]$ExportedFunctions.Add($_.BaseName)
}

$ModuleLevelFunctions = $null

foreach($function in $ModuleLevelFunctions)
{
    Write-Verbose -Message "$f - Checking function $function"
    if($ExportedFunctions -contains $function)
    {
        write-verbose -Message "$f - Removing function $function from exportlist"
        $ExportedFunctions.Remove($function)
    }
    else
    {
        Write-Verbose -Message "$f - Exported functions does not contain $function"
    }
}

Write-Verbose -Message "$f - Constructing content of module file"
[string]$ModuleFile = ""
foreach($file in $fileList)
{
    $filecontent = Get-Content -Path $file.FullName -raw
    $filecontent = "$filecontent`n`n"
    $ModuleFile += $filecontent
}
[System.Version]$ver = $null

if((Test-Path -Path $moduleFileName -ErrorAction SilentlyContinue) -eq $true)
{
    Write-Verbose -Message "$f - Getting version info"
    Import-Module -Name ".\$ModuleName.psd1" -Verbose:$false
    $ver = (Get-Module $Modulename).Version
    Remove-Module $ModuleName -Verbose:$false
    #Remove-PSSnapin -Name FIMautomation -ErrorAction SilentlyContinue
    Write-Verbose -Message "$f - Removing previous version of $ModuleFileName"
    Remove-Item -Path $ModuleFileName
}

function Get-NextVersion
{
[cmdletbinding()]
[outputtype([System.Version])]
Param(
    [System.Version]$CurrentVersion
    ,
    [switch]$Major
    ,
    [switch]$Minor
)
    [System.Version]$newVersion = $null
    $f = $MyInvocation.InvocationName
    Write-Verbose -Message "$f - START"

    if($Major)
    {
        Write-Verbose -Message "$F - Bumping Major version"
        $build = $CurrentVersion.Build
        $ma = $CurrentVersion.Major + 1
        $mi = $CurrentVersion.Minor
        $newVersion = New-Object System.Version("$Ma.$Mi.$build.0")
    }

    if($Minor)
    {
        Write-Verbose -Message "$f - Bumping Minor version"
        $build = $CurrentVersion.Build
        $ma = $CurrentVersion.Major
        $mi = $CurrentVersion.Minor + 1
        $newVersion = New-Object System.Version("$Ma.$Mi.$build.0")
    }

    if($Minor -and $Major)
    {
        Write-Verbose -Message "$f - Bumping Major and Minor version"
        $build = $CurrentVersion.Build
        $ma = $CurrentVersion.Major + 1
        $mi = $CurrentVersion.Minor + 1
        $newVersion = New-Object System.Version("$Ma.$Mi.$build.0")
    }

    if(-not $Minor -and -not $Major)
    {
        Write-Verbose -Message "$f - Bumping build version"
        $build = $CurrentVersion.Build + 1
        $ma = $CurrentVersion.Major
        $mi = $CurrentVersion.Minor
        $newVersion = New-Object System.Version("$Ma.$Mi.$build.0")
    }
    return $newVersion
}

if(-not $ver)
{
    Write-Verbose -Message "$f - No previous version found, creating new version"
    $ver = New-Object System.Version("1.0.0.0")
}

if($Major)
{    
    $ver = Get-NextVersion -CurrentVersion $ver -Major
}

if($Minor)
{
    $ver = Get-NextVersion -CurrentVersion $ver -Minor
}

if($Minor -and $Major)
{
     $ver = Get-NextVersion -CurrentVersion $ver -Minor -Major
}

if(-not $Minor -and -not $Major)
{
    Write-Verbose -Message "$f - Defaults to bump build version"
    $ver = Get-NextVersion -CurrentVersion $ver
}

Write-Verbose -Message "$f - New version is $($ver.ToString())"

Write-Verbose -Message "$f - Writing contents to modulefile"
Set-Content -Path $ModuleFileName -Value $ModuleFile -Encoding UTF8

$ManifestName = "$((Get-ChildItem -Path $ModuleFileName -ErrorAction SilentlyContinue).BaseName).psd1"
Write-Verbose -Message "$f - ManifestfileName is $ManifestName"

if((Test-Path -Path $ManifestName -ErrorAction SilentlyContinue) -eq $true)
{
    Write-Verbose -Message "$f - Removing previous version of $ManifestName"
    Remove-Item -Path $ManifestName
}

Write-Verbose -Message "$f - Creating manifestfile"

$privateData = @{
    PSdata = @{
        Tags = @("Group Policy","ActiveDirectory","WMI","WMIFilter")
        LicenseUri = ""
        ProjectUri = "https://bitbucket.org/torgro/gpowmi/overview"
        IconUri = ""
        ReleaseNotes = ""
    }
}

$New_moduleManifest = @{
    Path        = $ManifestName
    Author      = "Tore Groneng @toregroneng tore@firstpoint.no"
    CompanyName = "Firstpoint Bergen AS"
    Description = "Manage Group Policy WMIfilters with Powershell"
    ModuleVersion = $ver.ToString()
    FunctionsToExport = $ExportedFunctions
    RootModule = $ModuleFileName
    ProjectUri = $ProjectUrl
    Tags = @("Group Policy","ActiveDirectory","WMI","WMIFilter")
    Copyright = '(c) 2015 Tore Groneng @toregroneng tore@firstpoint.no. All rights reserved.'
    PowerShellVersion = '4.0'
}
#New-ModuleManifest -Path $ManifestName -Author "Tore Grøneng @toregroneng tore@firstpoint.no" -CompanyName "Sparebanken Vest AS" -ModuleVersion $ver.ToString() -FunctionsToExport $ExportedFunctions -RootModule $ModuleFileName
New-ModuleManifest @New_moduleManifest
Write-Verbose -Message "$f - Reading back content to contert to UTF8 (content management tracking)"
Set-Content -Path $ManifestName -Value (Get-Content -Path $ManifestName -Raw) -Encoding UTF8

$Answer = "n"

if(-not $LoadModule)
{
    $Answer = Read-Host -Prompt "Load module $ModuleName? (Yes/No)"
}

if($Answer -eq "y" -or $Answer -eq "yes" -or $LoadModule)
{
    Write-Verbose -Message "$f - Loading module"
    if(Test-Path -Path $ManifestName)
    {
        Import-Module $PSScriptRoot\$ManifestName
    }
    else
    {
        Write-Warning -Message "Modulefile $ManifestName not found, module not imported"
    }
}
else
{
    Write-Verbose -Message "$f - Module not loaded"
}

Write-Verbose -Message "$f - END"