1+ function Get-DataFromWebDriver {
2+ [CmdletBinding ()]
3+ param (
4+ [string ]$EdgeProfile ,
5+ [switch ]$InPrivate
6+ )
7+
8+ " {0} - Invoking data from WebDriver" -f $MyInvocation.MyCommand | Write-Verbose
9+
10+ $AuthCookie = $null
11+
12+ " Opening Edge to retrieve authentication cookie" | Write-Host
13+ try {
14+ $Script :LoginRetryCount ++
15+ " `n {0} - Login try {1} of max {2}" -f $MyInvocation.MyCommand , $Script :LoginRetryCount , $Script :MaxLoginRetries | Write-Verbose
16+
17+ $EdgeDriver = Start-EdgeDriver - InPrivate:$InPrivate.IsPresent - EdgeProfile $EdgeProfile
18+
19+ Start-EdgeDriverLogin
20+
21+ $AgentString = $EdgeDriver.ExecuteScript (" return navigator.userAgent" )
22+
23+ Start-Sleep - Seconds 1
24+ $LoginMessageShown = $false
25+ $MfaRequestDisplayed = $false
26+ $PhoneLinkActive = $false
27+ if ((Get-Process | Where-Object { $_.ProcessName -eq " PhoneExperienceHost" } | Measure-Object ).Count -gt 0 ) {
28+ $PhoneLinkActive = $true
29+ }
30+ do {
31+ if (-not $LoginMessageShown ) {
32+ Write-Host " `r`n Browser opened, please login! Waiting for login." - NoNewline - ForegroundColor Yellow
33+ if ($Script :Credential -and ! [string ]::IsNullOrWhiteSpace($Script :Credential.UserName )) {
34+ " Execute automated login steps for user: {0}" -f $Script :Credential.UserName.Trim () | Write-Host - ForegroundColor Yellow - NoNewline
35+ }
36+ $LoginMessageShown = $true
37+ }
38+
39+ Write-Host " ." - NoNewline - ForegroundColor Yellow
40+ Start-Sleep - Milliseconds 500
41+
42+ if ($Script :Credential -and ! [string ]::IsNullOrWhiteSpace($Script :Credential.UserName.Trim ())) {
43+
44+ if ($EdgeDriver.url -like " https://login.microsoftonline.com/*" ) {
45+ try {
46+ $UserNameElementId = " i0116"
47+ $PasswordElementId = " i0118"
48+ $SubmitButton = " idSIButton9"
49+ # $DisplayNameElement = "displayName"
50+ $CantAccessAccountId = " cantAccessAccount"
51+ $MfaElementId = " idRichContext_DisplaySign"
52+ $MfaRetryId1 = " idA_SAASTO_Resend"
53+ $MfaRetryId2 = " idA_SAASDS_Resend"
54+ $ButtonBackId = " idBtn_Back"
55+ $ButtonSubmitId = " idSIButton9"
56+
57+ $Elements = $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::XPath(" //*[@id]" ))
58+ $IdAttributes = $Elements.GetAttribute (" id" )
59+
60+ if ($IdAttributes -contains $UserNameElementId `
61+ -and $IdAttributes -contains $PasswordElementId `
62+ -and $IdAttributes -notcontains $ButtonBackId `
63+ -and $IdAttributes -contains $ButtonSubmitId `
64+ -and $IdAttributes -contains $CantAccessAccountId `
65+ -and $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($ButtonSubmitId )).ComputedAccessibleLabel -eq " Next" `
66+ ) {
67+ Start-Sleep - Milliseconds 500
68+ " Enter username" | Write-Verbose
69+ $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($UserNameElementId ))[0 ].SendKeys($Script :Credential.UserName.Trim ())
70+ $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($SubmitButton )).Click()
71+ }
72+
73+ if ($IdAttributes -notcontains $UserNameElementId `
74+ -and $IdAttributes -contains $PasswordElementId `
75+ -and $IdAttributes -contains $ButtonSubmitId `
76+ -and $IdAttributes -notcontains $CantAccessAccountId `
77+ -and $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($ButtonSubmitId )).ComputedAccessibleLabel -eq " Sign in"
78+ ) {
79+ " Enter password" | Write-Verbose
80+ $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($PasswordElementId ))[0 ].SendKeys($Script :Credential.GetNetworkCredential ().Password)
81+ $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($SubmitButton )).Click()
82+ }
83+
84+ if ($IdAttributes -notcontains $UserNameElementId `
85+ -and $IdAttributes -notcontains $PasswordElementId `
86+ -and $IdAttributes -contains $SelectUserElementId
87+ ) {
88+ " Select logged-in account" | Write-Verbose
89+ if ($AccountElement.GetAttribute (" data-test-id" ) -eq $Script :Credential.UserName.Trim ()) {
90+ $AccountElement.Click ()
91+ }
92+ }
93+
94+ if ($IdAttributes -notcontains $UserNameElementId `
95+ -and $IdAttributes -notcontains $PasswordElementId `
96+ -and $IdAttributes -notcontains $SelectUserElementId `
97+ -and $IdAttributes -contains $ButtonBackId `
98+ -and $IdAttributes -contains $ButtonSubmitId `
99+ -and $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($ButtonBackId )).ComputedAccessibleLabel -eq " No" `
100+ -and $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($ButtonSubmitId )).ComputedAccessibleLabel -eq " Yes" `
101+ ) {
102+ " Decline Stay signed in? " | Write-Verbose
103+ $EdgeDriver.FindElement ([OpenQA.Selenium.By ]::Id($ButtonBackId )).Click()
104+ }
105+
106+ if ($IdAttributes -notcontains $UserNameElementId `
107+ -and $IdAttributes -notcontains $PasswordElementId `
108+ -and $IdAttributes -notcontains $ButtonBackId `
109+ -and ($EdgeDriver.FindElements ([OpenQA.Selenium.By ]::XPath(" //*[@data-test-id]" )) | Measure-Object ).Count -gt 0 ) {
110+
111+ " Select logged-in user " | Write-Verbose
112+ $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::XPath(" //*[@data-test-id]" )) | ForEach-Object {
113+ if ($_.GetAttribute (" data-test-id" ) -eq $Script :Credential.UserName.Trim ()) {
114+ $_.Click ()
115+ }
116+ }
117+ }
118+
119+ if ($MfaRequestDisplayed -ne $true `
120+ -and $IdAttributes -notcontains $UserNameElementId `
121+ -and $IdAttributes -notcontains $PasswordElementId `
122+ -and $IdAttributes -contains $MFAElementId `
123+ -and $IdAttributes -notcontains $MfaRetryId `
124+ -and ($EdgeDriver.FindElements ([OpenQA.Selenium.By ]::XPath(" //*[@data-test-id]" )) | Measure-Object ).Count -eq 0
125+ ) {
126+ $Message = " `n Waiting for your approve this sign-in request."
127+ if ($null -ne $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($MfaElementId )).Text) {
128+ if ($PhoneLinkActive ) {
129+ $Message = " {0}. {1} (This value is now in your clipboard so you can paste it into your Authenticator app using PhoneLink)." -f $Message.TrimEnd (" ." ), $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($MfaElementId )).Text
130+ $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($MfaElementId )).Text | Clip
131+ }
132+ else {
133+ $Message = " {0}. {1}" -f $Message.TrimEnd (" ." ), $EdgeDriver.FindElements ([OpenQA.Selenium.By ]::Id($MfaElementId )).Text
134+ }
135+ }
136+ $Message | Write-Host - ForegroundColor Yellow
137+ $MfaRequestDisplayed = $true
138+ }
139+
140+ if ($MfaRequestDisplayed `
141+ -and $IdAttributes -notcontains $UserNameElementId `
142+ -and $IdAttributes -notcontains $PasswordElementId `
143+ -and $IdAttributes -notcontains $MFAElementId `
144+ -and (
145+ $IdAttributes -contains $MfaRetryId1 `
146+ -or $IdAttributes -contains $MfaRetryId2 )`
147+ -and ($EdgeDriver.FindElements ([OpenQA.Selenium.By ]::XPath(" //*[@data-test-id]" )) | Measure-Object ).Count -eq 0
148+ ) {
149+ " `n MFA failed! Please retry!" | Write-Warning
150+ $MfaRequestDisplayed = $false
151+ $LoginMessageShown = $false
152+ }
153+ }
154+ catch {}
155+ }
156+ }
157+
158+ $EdgeDriverHost = " -1"
159+ $OmadaWebBaseHost = " -2"
160+ $EdgeDriverAbsolutePath = $null
161+ if ($null -ne $EdgeDriver.url ) {
162+ $EdgeDriverHost = [System.Uri ]::new($EdgeDriver.url ).Host
163+ $EdgeDriverAbsolutePath = [System.Uri ]::new($EdgeDriver.url ).AbsolutePath
164+ $OmadaWebBaseHost = [System.Uri ]::new($Script :OmadaWebBaseUrl ).Host
165+ }
166+
167+ if ($OmadaWebBaseHost -ne $EdgeDriverHost -and $EdgeDriverAbsolutePath -ne " /home" ) {
168+ if ($null -eq $EdgeDriver -or ($EdgeDriver.WindowHandles | Measure-Object ).Count -le 0 ) {
169+ throw " EdgeDriver window is closed"
170+ }
171+ }
172+ else {
173+ " {0} - Logged in to Omada Web" -f $MyInvocation.MyCommand | Write-Verbose
174+ $AuthCookie = $EdgeDriver.Manage ().Cookies.AllCookies | Where-Object { $_.Name -eq ' oisauthtoken' }
175+ break
176+ }
177+ }
178+ while ($Script :LoginRetryCount -le $Script :MaxLoginRetries )
179+ " {0} (Line {1}): {2}" -f $MyInvocation.MyCommand , $MyInvocation.ScriptLineNumber , $$ | Write-Verbose
180+
181+ # $CredentialsEntered = $false
182+ Close-EdgeDriver
183+ if ($null -ne $AuthCookie ) {
184+ $Script :LoginRetryCount = 0
185+ return $AuthCookie , $AgentString
186+ }
187+ else {
188+ " Could not authenticate to '{0}" -f $Script :OmadaWebBaseUrl | Write-Error - ErrorAction " Stop"
189+ }
190+ }
191+ catch {
192+ " invalid session id: session deleted as the browser has closed the connection" -match " as the browser has closed the connection"
193+
194+ if (
195+ $_.Exception.Message -like ' *Exception calling "ExecuteScript" with "*" argument(s): "no such window: target window already closed*"' -or
196+ $_.Exception.Message -like ' *Exception calling "Window" with "*" argument(s): "no such window*' -or
197+ $_.Exception.Message -like " *EdgeDriver window is closed*" -or
198+ $_.Exception.Message -like ' Exception calling "Window" with "*" argument(s): *invalid session id*'
199+ ) {
200+ if ($Script :LoginRetryCount -ge $Script :MaxLoginRetries ) {
201+ Close-EdgeDriver
202+ " `n Login try count ({0}) exceeded! Cannot continue!" -f $Script :MaxLoginRetries | Write-Error - ErrorAction " Stop" - Category AuthenticationError
203+ break
204+ }
205+
206+ " " | Write-Host
207+ " Edge WebDriver was unable to complete the process to retrieve a cookie. Re-open Edge WebDriver in 2 seconds!" | Write-Host - ForegroundColor Yellow
208+ Start-Sleep - Seconds 2
209+ $LoginMessageShown = $false
210+ try {
211+ Close-EdgeDriver
212+ }
213+ catch {}
214+ Get-DataFromWebDriver - InPrivate:$InPrivate.IsPresent - EdgeProfile $EdgeProfile
215+ }
216+ else {
217+ $PSCmdlet.ThrowTerminatingError ($PSItem )
218+ }
219+ Close-EdgeDriver
220+ $PSCmdlet.ThrowTerminatingError ($PSItem )
221+ }
222+ }
0 commit comments