-
Notifications
You must be signed in to change notification settings - Fork 6
/
create-pull-client.ps1
153 lines (114 loc) · 6.26 KB
/
create-pull-client.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
<#
Copyright (c) Opsgility. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
<#
.SYNOPSIS
This Cmdlet creates a virtual machine and bootstraps a DSC Pull Server Client.
It is currently only supported to deploy the client into the same cloud service as the pull server.
.DESCRIPTION
This Cmdlet creates a virtual machine and bootstraps a DSC Pull Server Client.
It is currently only supported to deploy the client into the same cloud service as the pull server.
.EXAMPLE
# No VNET
.\create-pull-client.ps1 -SubscriptionName opsgilitytraining -ServiceName pullsvc -Name pullclient -Size Small -ConfigurationName "WebServer" -CertificatePath .\PSDSCPullServerCert.pfx -PullServer pullsrv
# Create domain joined
.\create-pull-client.ps1 -SubscriptionName opsgilitytraining -ServiceName pullsvc -Name pullclient -Size Small -ConfigurationName "WebServer" -CertificatePath .\PSDSCPullServerCert.pfx -PullServer pullsrv -JoinDomain -Subnet "MYSUBNET" -DomainFQDN "mydomain.com"
#>
[CmdletBinding(DefaultParameterSetName="default")]
param(
[parameter(Mandatory)]
[string]$SubscriptionName,
[parameter(Mandatory)]
[string]$ServiceName,
[parameter(Mandatory)]
[string]$Name,
[parameter(Mandatory)]
[string]$Size,
[parameter(Mandatory)]
$CertificatePath,
[parameter(Mandatory)]
$PullServer,
[parameter(Mandatory)]
$ConfigurationName,
[parameter(Mandatory, ParameterSetName="JoinDomain")]
[switch]$JoinDomain,
[parameter(Mandatory, ParameterSetName="JoinDomain")]
[string]$Subnet,
[parameter(Mandatory, ParameterSetName="JoinDomain")]
[string]$DomainFQDN,
[parameter(ParameterSetName="JoinDomain")]
[String]$DomainOU
)
Set-StrictMode -Version Latest
function IsAdmin
{
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()`
).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
return $IsAdmin
}
if((IsAdmin) -eq $false)
{
Write-Error "This script generates certificates and installs them in the local machine container and must be elevated to run."
return
}
$Credential = Get-Credential
$ImageName = (Get-AzureVMImage | Where { $_.ImageFamily -eq "Windows Server 2012 R2 Datacenter" } | sort PublishedDate -Descending | Select-Object -First 1).ImageName
# Existing certificate must exist
$pwd = $credential.GetNetworkCredential().password
$pfxName = Join-Path $PSScriptRoot "PSDSCPullServerCert.pfx"
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$cert.Import($pfxName,$pwd,'Exportable')
Write-Host "Creating DSC pull client machine $name in cloud service $ServiceName" -ForegroundColor Green
# Create virtual machine with PSDSCPullServer certificate deployed
$vmConfig = New-AzureVMConfig -Name $Name -ImageName $ImageName -InstanceSize Small
if($PSCmdlet.ParameterSetName -eq "JoinDomain")
{
$domain = $DomainFQDN.Split(".")[0]
$domainCredential = Get-Credential -Message "Enter Domain User Name and Password to Join Domain $domain"
if($DomainOU -eq $null -or $DomainOU.Length -eq 0)
{
$vmConfig | Add-AzureProvisioningConfig -WindowsDomain -AdminUsername $Credential.UserName -Password $credential.GetNetworkCredential().password -EnableWinRMHttp -X509Certificates $cert -JoinDomain $DomainFQDN -Domain $Domain -DomainUserName $domainCredential.UserName -DomainPassword $domainCredential.GetNetworkCredential().password
}
else
{
$vmConfig | Add-AzureProvisioningConfig -WindowsDomain -AdminUsername $Credential.UserName -Password $credential.GetNetworkCredential().password -EnableWinRMHttp -X509Certificates $cert -JoinDomain $DomainFQDN -Domain $Domain -DomainUserName $domainCredential.UserName -DomainPassword $domainCredential.GetNetworkCredential().password -MachineObjectOU $DomainOU
}
}
else
{
$vmConfig | Add-AzureProvisioningConfig -Windows -AdminUsername $Credential.UserName -Password $credential.GetNetworkCredential().password -EnableWinRMHttp -X509Certificates $cert
}
if($PSCmdlet.ParameterSetName -eq "default")
{
Write-Host "Deploying Virtual Machine $Name" -ForegroundColor Green
New-AzureVM -ServiceName $ServiceName -VMs $vmConfig -WaitForBoot
}
else
{
Write-Host "Deploying Virtual Machine $Name into subnet $Subnet" -ForegroundColor Green
$vmConfig | Set-AzureSubnet -SubnetName $Subnet
New-AzureVM -ServiceName $ServiceName -VMs $vmConfig -WaitForBoot
}
Write-Host "Downloading and installing WinRM Certificate for secure communications" -ForegroundColor Green
& $PSScriptRoot\Helper\InstallWinRMCertAzureVM.ps1 -SubscriptionName $SubscriptionName -ServiceName $ServiceName -Name $Name
$DSCLocalConfigScript = Join-Path $PSScriptRoot "\Scripts\PullClientScripts.zip"
$DSCLocaLConfigScriptDestinationPath = "C:\DSCScript"
$DSClOCALConfigScriptFullPath = Join-Path $DSCLocalConfigScriptDestinationPath "Assert_DSCLocalConfig.ps1"
Write-Host "Uploading DSC Configuration Script to configure DSC Client Machine" -ForegroundColor Green
& $PSScriptRoot\Helper\UploadAndExtractFile.ps1 -SubscriptionName $SubscriptionName -ServiceName $ServiceName -Name $Name -FileUpload $DSCLocalConfigScript -LocalPath $DSCLocalConfigScriptDestinationPath -Credential $Credential
Write-Host "Setting DSC Local Configuration to use Pull Server" -ForegroundColor Green
$uri = Get-AzureWinRMUri -ServiceName $ServiceName -Name $Name
Invoke-Command -ConnectionUri $uri -Credential $Credential -ArgumentList $DSCLocalConfigScriptFullPath, $PullServer, $ConfigurationName -ScriptBlock {
param($DSCLocalConfigScriptFullPath, $PullServer, $ConfigurationName)
Write-Host "Configuring the Pull Server Client using Script $DSCLocalConfigScriptFullPath, Pull Server $PullServer and Configuration $ConfigurationName" -ForegroundColor Green
& $DSCLocalConfigScriptFullPath $PullServer $ConfigurationName
}