forked from seansp-zz/Framework-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_session_to_machine.ps1
More file actions
71 lines (55 loc) · 2 KB
/
Copy pathcreate_session_to_machine.ps1
File metadata and controls
71 lines (55 loc) · 2 KB
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
param (
[Parameter(Mandatory=$false)] [string] $requestedName="Unset",
[Parameter(Mandatory=$false)] [string] $destSA="smokesrc",
[Parameter(Mandatory=$false)] [string] $destRG="smoke_source_resource_group",
[Parameter(Mandatory=$false)] [string] $suffix="-Runonce-Primed.vhd",
[Parameter(Mandatory=$false)] [string] $location="westus",
[Parameter(Mandatory=$false)] [int] $retryCount=2
)
$destSA = $destSA.Trim()
$destRG = $destRG.Trim()
$suffix = $suffix.Trim()
$location = $location.Trim()
. c:\Framework-Scripts\common_functions.ps1
. c:\Framework-Scripts\secrets.ps1
if ($requestedNames -like "*,*") {
Write-Error "create_session_to_machine does not take more than one machine parameter."
exit 1
} else {
$vm_name += $requestedName
}
$suffix = $suffix -replace "_","-"
. C:\Framework-Scripts\common_functions.ps1
. C:\Framework-Scripts\secrets.ps1
$logName = "C:\temp\transcripts\create_session_to_machine-" + $requestedName + "-" + (Get-Date -Format s).replace(":","-")
Start-Transcript -path $logName -force
login_azure $DestRG $DestSA $location > $null
#
# Session stuff
#
$o = New-PSSessionOption -SkipCACheck -SkipRevocationCheck -SkipCNCheck
$cred = make_cred
$suffix = $suffix.Replace(".vhd","")
[int]$timesTried = 0
[bool]$success = $false
while ($timesTried -lt $retryCount) {
write-verbose "Creating PSRP session to remote machine $vm_name, resource group $destRG"
$timesTried = $timesTried + 1
[System.Management.Automation.Runspaces.PSSession]$session = create_psrp_session $vm_name $destRG $destSA $location $cred $o
if ($? -eq $true -and $session -ne $null) {
Enter-PSSession -Session $session
$success = $true
break
} else {
if ($timesTried -lt $retryCount) {
Write-Host " Try $timesTried of $retryCount -- FAILED to establish PSRP connection to machine $vm_name."
}
}
start-sleep -Seconds 10
}
# Stop-Transcript > $null
if ($success -eq $true) {
exit 0
} else {
exit 1
}