Skip to content

Instantly share code, notes, and snippets.

@nick050183
Created June 21, 2020 09:26
Show Gist options
  • Save nick050183/b9c6ecbc753f2eaba2373b5c24ac5c81 to your computer and use it in GitHub Desktop.
Save nick050183/b9c6ecbc753f2eaba2373b5c24ac5c81 to your computer and use it in GitHub Desktop.
Powershell Office 365 login (doesn't work)
$login = "CANTSHOW"
$pass = "CANTSHOW"
$Drive = "0","0","0","0","0","0","0","0","0","0"
$Location = "0","0","0","0","0","0","0","0","0","0"
$Label = "0","0","0","0","0","0","0","0","0","0"
$Webdavloc = "0","0","0","0","0","0","0","0","0","0"
$Drive[0]="I:"
$Drive[1]="J:"
$location[0] = "https://CANTSHOW.sharepoint.com/sites/itgisdepartment/Shared%20Documents"
$location[1] = "https://CANTSHOW.sharepoint.com/sites/itgisdepartment"
$label[0] = "This is a label there are many like it but this one is mine"
$label[1] = "I too am a very important label"
for ($i=0; $Location[$i] -ne "0"; $i++) {
$Webdavloc[$i] = $Location[$i].Replace("https://","\\").Replace("/_layouts/15/start.aspx#","").Replace("sharepoint.com/","sharepoint.com@SSL\DavWWWRoot\").Replace("/Forms/AllItems.aspx","").Replace("/","\").Replace("%20"," ")
}
#Add domains to trusted site
New-Item "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\sharepoint.com" -ErrorAction SilentlyContinue
New-Item "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\microsoftonline.com" -ErrorAction SilentlyContinue
New-Item "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\microsoftonline.com\login" -ErrorAction SilentlyContinue
New-Item "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\sharepoint.com\CANTSHOW" -ErrorAction SilentlyContinue
New-Item "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\sharepoint.com\CANTSHOW-my" -ErrorAction SilentlyContinue
New-ItemProperty "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\sharepoint.com\CANTSHOW" -Name "https" -Value 2 -Type DWORD -ErrorAction SilentlyContinue
New-ItemProperty "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\sharepoint.com\CANTSHOW-my" -Name "https" -Value 2 -Type DWORD -ErrorAction SilentlyContinue
New-ItemProperty "hkcu:\software\microsoft\windows\currentversion\internet settings\zonemap\domains\microsoftonline.com\login" -Name "https" -Value 2 -Type DWORD -ErrorAction SilentlyContinue
#Disable first run wizard
New-ItemProperty "hkcu:\software\microsoft\Internet Explorer\Main" -Name "DisableFirstRunCustomize" -value 1 -ErrorAction SilentlyContinue
function getElementById{
Param(
[Parameter(Mandatory=$true)]$id
)
$localObject = $Null
try{
$localObject = $script:ie.document.getElementById($id)
if($localObject.tagName -eq $Null){Throw "The element $id was not found (1) or had no tagName"}
return $localObject
}catch{$localObject = $Null}
try{
$localObject = $script:ie.document.IHTMLDocument3_getElementById($id)
if($localObject.tagName -eq $Null){Throw "The element $id was not found (2) or had no tagName"}
return $localObject
}catch{
Throw
}
}
function waitForIE{
$waited = 0
do {Start-Sleep -m 100;$waited+=0.1} until (-not ($script:ie.Busy) -or $waited -gt 15)
}
function labelDrive{
Param(
[String]$lD_DriveLetter,
[String]$lD_MapURL,
[String]$lD_DriveLabel
)
#try to set the drive label
if($lD_DriveLabel.Length -gt 0){
Write-Host -text "A drive label has been specified, attempting to set the label for $($lD_DriveLetter)"
try{
$regURL = $lD_MapURL.TrimEnd("\") -Replace [regex]::escape("\"),"#"
$path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\$($regURL)"
$Null = New-Item -Path $path -Force -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_CommentFromDesktopINI" -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_LabelFromDesktopINI" -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_LabelFromReg" -Value $lD_DriveLabel -ErrorAction SilentlyContinue
$regURL = $regURL -Replace [regex]::escape("DavWWWRoot#"),""
$path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\$($regURL)"
$Null = New-Item -Path $path -Force -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_CommentFromDesktopINI" -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_LabelFromDesktopINI" -ErrorAction SilentlyContinue
$Null = New-ItemProperty -Path $path -Name "_LabelFromReg" -Value $lD_DriveLabel -ErrorAction SilentlyContinue
Write-Host -text "Label has been set to $($lD_DriveLabel)"
}catch{
Write-Host -text "Failed to set the drive label registry keys: $($Error[0]) " -fout
}
}
}
<#
taskkill /f /IM iexplore.exe
$web = "https://login.microsoftonline.com/logout.srf"
$ie = new-object -com InternetExplorer.Application
$ie.visible = $true;
$ie.Navigate($web)
Start-Sleep -Seconds 3
try{
$logout = $ie.Document.IHTMLDocument3_getElementsByTagName("small") | ? {$_.innerText -eq $login}
$logout.click()
waitForIE
Write-Host "found multiple logouts. Clicking first one"
}catch{
Write-Host "no multiple logouts found."
}
Start-Sleep -Seconds 5
waitForIE
$ie.quit()
#>
#try{
#$ie.quit()
#}catch{
#taskkill /f /IM iexplore.exe
#}
#[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie)
$ie = new-object -com InternetExplorer.Application
$ie.visible = $true;
$web = "https://CANTSHOW.sharepoint.com/sites/itgisdepartment/"
#$web = "https://login.microsoftonline.com/login.srf"
$ie.Navigate($web)
$global:turner = 1
Function StepOne {
try{
(getElementById -id "otherTileText").Click()
waitForIE
Write-Host "Found sign in elements new method on Office 365 login page, trying to redirect to new method"
}catch{
Write-Host "No redirect to new sign in method detected"
}
$global:turner = 2
#Write-Host $global:turner
}
Function StepTwo {
$errcheck = 0
While ($errcheck -eq 0) {
try{
(getElementById -id "i0116").value = $login
(getElementById -id "i0116").innerText = $login
}catch{
Write-Host "Ie went stupid refreshing 2"
$ie.Refresh()
#Start-Sleep -Seconds 3
$global:turner = 1
return;
}
$errcheck = 1
}
Write-Host "Am i Skipping the break?"
waitForIE
(getElementById -id "idSIButton9").click()
$global:turner = 3
}
Function StepThree {
try{
(getElementById -id "aadTileTitle").click()
}catch{
#Write-Host "Ie went stupid refreshing"
#$ie.Refresh()
#Start-Sleep -Seconds 3
#$global:turner = 1
#return;
}
$global:turner = 4
}
Function StepFour {
$errcheck = 0
While ($errcheck -eq 0) {
try{
(getElementById -id "i0118").value = $pass
(getElementById -id "i0118").innerText = $pass
}catch{
Write-Host "Ie went stupid refreshing"
$ie.Refresh()
#Start-Sleep -Seconds 3
$global:turner = 1
return;
}
#Write-Host "I havent clicked yet"
waitForIE
(getElementById -id "idSIButton9").click()
$global:turner = 5
#Write-Host "now I have"
$errcheck = 1
}
}
Function StepFive {
try{
Start-Sleep -Seconds 2
(getElementById -id "idSIButton9").click()
}catch{
Write-Host "Ie went stupid refreshing"
$ie.Refresh()
#Start-Sleep -Seconds 3
$global:turner = 1
return;
}
$global:turner = 0
}
while ($turner -ne "0"){
Write-Host "Running turner while"
waitForIE
if ($turner -eq "1") {
Write-Host "Running stepone"
Start-Sleep -Seconds 3
StepOne
#break
}
if ($turner -eq "2") {
Write-Host "Running steptwo"
Start-Sleep -Seconds 3
StepTwo
#break
}
if ($turner -eq "3") {
Write-Host "Running stepthree"
Start-Sleep -Seconds 3
StepThree
#break
}
if ($turner -eq "4") {
Write-Host "Running stepfour"
Start-Sleep -Seconds 3
StepFour
#break
}
if ($turner -eq "5") {
Write-host "Running stepfive"
Start-Sleep -Seconds 3
StepFive
#break
}
}
#$web = "https://CANTSHOW.sharepoint.com/sites/itgisdepartment/"
#$ie.Navigate($web)
#Start-Sleep -Seconds 3
$web = "https://CANTSHOW.sharepoint.com/sites/itgisdepartment/"
$ie.Navigate($web)
Start-Sleep -Seconds 3
#taskkill /f /IM explorer.exe
#explorer.exe
Start-Sleep -Seconds 3
for ($i=0; $Drive[$i] -ne "0"; $i++) {
#$ehh = $Drive[$i]
#$ehh2 = $Location[$i]
Write-Host NET USE $Drive[$i] $Location[$i] /PERSISTENT:YES
NET USE $Drive[$i] $Location[$i] /PERSISTENT:YES
labelDrive $Drive[$i] $Webdavloc[$i] $Label[$i]
}
#$ie.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment