Powershell start IE in private and log in to page - powershell

I'm trying to automate a process of activating accounts by logging in to email, I have it working when I am not on the business network with the following code.
$username = "user"
$password = "pass"
$url = "url"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate($url)
while ($ie.Busy -eq $true){Start-Sleep -seconds 1;}
$usernamefield = $ie.Document.getElementByID('UsernameTextBox')
$usernamefield.value = $username
$passwordfield = $ie.Document.getElementByID('PasswordTextBox')
$passwordfield.value = $password
$ie.document.getElementById("ctl00_ContentPlaceHolder1_SubmitButton").click()`
My issue is that once connected to the business network it then uses SSO to log in so I do not get the option to put in a username and password. To be able to put in a username and password I need to start IE in private mode. I have been able to start IE in private with the start-process command but I cannot find a way to select the window to type in the username and password.
Is there a way I can use powershell to log in to the website in a private browser?

You can get a handle to the private IE by starting it with start-process, as you referred to.
Start-Process -FilePath "C:\Program Files (x86)\Internet Explorer\iexplore.exe" -ArgumentList ' -private http://bogus.bogus'
Then you can connect to it by finding it -
$Shell = New-Object -Com Shell.Application
$apps = $shell.windows()
$ie = $apps | where { $_.locationname -eq 'http://bogus.bogus/' }
Happy scripting. :)

Related

Auto login script

I am trying to fix a script that automatically opens a page in edge and logs in. i can get it to open the page but the script errors out without entering the login info or directing to the desired final destination. the internal server page uses j_username and j_password to id the location where the credentials are entered. Im fairly new to coding and would like help to understand what im doing wrong and what i could be doing better.
Here is the code:
$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible
start microsoft-edge:http://internal URL to company
$usernmae="user"
$password="password"
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 5;}
$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = $username
$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = $password
$Link = $ie.document.getElementByID('login-submit')
$Link.click()
{Start-Sleep -Seconds 10;}
start microsoft-edge:http://final destination internal URL to company
$ie.Quit()
Your problem was that the $ie object is not connected to the browser when you use start microsoft-edge:http://internal URL to company. Use the Navigate method:
$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("https://internal-login-page")
Do {sleep 1} While ($ie.Busy)
$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = "user"
$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = "password"
$Link = $ie.document.getElementByID('login-submit')
$Link.click()
Do {sleep 1} While ($ie.Busy)
$ie.Navigate("https://final-page")

Insert Sharepoint Credentials using Powershell

When trying to open or sometimes close, via powershell, a word document in a sharepoint directory hosted in my company's network, the windows security box popup.
How can I authenticate this ? Here is part of my script:
$docpath = "\\sharepoint.[Domain].com\[...]\mydoc.docx"
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open("$docpath")
{...process...}
$doc.Close([ref]$true)
$word.Quit()
$word = $null
[gc]::collect()
[gc]::WaitForPendingFinalizers()
Here is a visual example of what happens.
I´ve found something that works, but only if if have an admin user. Still would like to know if there is a way to do this without this kind of permission.
Here is the code:
$User = "domain\useradmin"
$Cred = Get-Credential -Credential $User
$srv = "sharepoint.[Domain].com"
Invoke-Command -ComputerName $srv -Credential $Cred -ScriptBlock{
$docpath = "\\sharepoint.[Domain].com\[...]\mydoc.docx"
$word = New-Object -ComObject Word.Application
$word.Visible = $true
$doc = $word.Documents.Open("$docpath")
{...process...}
$doc.Close([ref]$true)
$word.Quit()
$word = $null
}

Powershell - Masking a line with secure string causing

To log into something, I have to enter a username on page1, password on page2, then click submit on page3. To make it easier I wrote a PS script. I double checked it by hard-coding the password, and it was successful.
But not wanting to hard-code the password I used the added $pass and how to read it. But now I get invalid password when I run the script.
I wanted to use Get-Credentials, but didn't know how to just pass in the username or password on their specific pages.
I would appreciate any advice to help me in the right direction towards my goal.
$pass = Read-Host 'd\P What is your password?' -AsSecureString
[Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))
$IE = New-Object -ComObject InternetExplorer.Application
$URL = 'https://website/'
$IE.Visible = $true
$IE.Navigate($URL)
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$ie.Document.getElementById('DATA').value = "P0523586"
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$ie.Document.getElementById('DATA').value = "$pass"
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()
While ($IE.Busy -eq $true) {Start-Sleep -Milliseconds 1000}
$Submit = $ie.Document.getElementsByTagName('Input') | ? {$_.Type -eq "Submit"}
$Submit.click()
Get-Credential is a good way to go about this as you aren't storing anything that should be kept secure in the script.
It's easiest to assign the Object returned from Get-Credentials to a variable:
$credentials = Get-Credential
You can then use $credentials.UserName to retrieve the username:
PS> $credentials.UserName
myusername
And $credentials.Password to retrieve Secure Password Object
PS> $credentials.Password
System.Security.SecureString
This can be used by other PowerShell commands, but needs to be decoded (made un-secure) before it can be sent to a process that cannot accept secure password objects.
To retrieve the password in plain-text, you can use the GetNetworkCredential method like this:
PS> $credentials.GetNetworkCredential().Password
mypassword
In your script you would add $credentials = Get-Credential at the start of your script, and submit the username/password to the form like this:
$ie.Document.getElementById('DATA').value = $credentials.UserName
$ie.Document.getElementById('DATA').value = $credentials.GetNetworkCredential().Password

Powershell Connect to VSO

I'm trying to use Powershell to connect to VSO. Here is my code:
$tfsServer = New-Object System.Uri("the server is here")
$creds = [System.Net.CredentialCache]::DefaultNetworkCredentials
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$creds)
$tfsCollection.Authenticate()
When it reaches the Authenticate line, it pops up a box for me to enter my credentials. I need it to not pop up this box, as this script will be scheduled, and I can't keep entering the credentials. How can I pass the current user's credentials to the TFS object?
Try this:
First, run this command which will prompt you once for your password, and then save it in an encrypted format.
read-host -prompt Password -assecurestring | convertfrom-securestring | out-file .\ps-password.pwd -ErrorAction Stop
Change the $username variable
$Username = 'jdoe'
$Password = Get-Content ".\ps-password.pwd" | ConvertTo-SecureString
$creds = New-Object -typename System.Management.Automation.PSCredential -ArgumentList $Username,$Password
$tfsServer = New-Object System.Uri("the server is here")
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$creds)
$tfsCollection.Authenticate()
Use the constructor that just takes a URI. It will default to using the credentials of the current user.
To connect to Visual Studio Online, you have to follow the instructions at Buck's post. Shortly:
enable alternate credentials on the VSO account
set alternate user and password
use code similar to the following
$tfsServer = New-Object System.Uri("the server is here")
$netCred = New-Object NetworkCredential("alternate_user","alternate_password")
$basicCred = New-Object Microsoft.TeamFoundation.Client.BasicAuthCredential($netCred)
$tfsCred = New-Object Microsoft.TeamFoundation.Client.TfsClientCredentials($basicCred)
$tfsCred.AllowInteractive = $false
$tfsCollection = New-Object Microsoft.TeamFoundation.Client.TfsTeamProjectCollection($tfsServer,$tfsCred)
$tfsCollection.EnsureAuthenticated()
I know no way of using current process credentials with VSO, but you must explicitly pass them.
Use EnsureAuthenticated and do not specify credentials.
$tfsCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection("the server is here")
$tfsCollection.EnsureAuthenticated()
This way it will use the account running the process.

PS script to login into salesforce.com

I'm trying to write a powershell script to simply log into salesforce.com. From there I'll just find some resulting HTML to find out if it was successful or not. This is what I have so far.
$username = "myusername"
$password = "mypassword"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://login.salesforce.com")
$ie.document.getElementById("username").value= "$username"
$ie.document.getElementById("pw").value = "$password"
$ie.document.getElementById("Login").Click()
The problem I have is that when I execute the click method nothing happens. Since the form is visible I can see that the username and password fields are set correctly. Just nothing happens when I execute Click.
I didn't past the code from the site but you can view source at https://login.salesforce.com
Anyone have any ideas why click isn't working. Wasn't sure if it's because of the type of button, or the onclick method using javascript, etc.
Welcome to stack overflow. Your script is very similar to the one found here : http://gallery.technet.microsoft.com/scriptcenter/69eaa0bc-c0fc-4948-bd4c-6dab8d85179e
$username = "myusername"
$password = "mypassword"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://login.salesforce.com")
while ($ie.Busy -eq $true)
{
Start-Sleep -Milliseconds 1000;
}
$ie.document.getElementById("username").value= "$username"
$ie.document.getElementById("pw").value = "$password"
$login = $ie.document.getElementsByTagName("button") | ? {$_.id -eq "Login"}
$login.click()
The way to get it working is almost identical, but unlike googles page, saleforce uses the id=Login in many places, and if you run $ie.document.getElementById("Login") you can see that tagName : DIV... In order to get the login button, I just started by getting all the buttons, and then finding the one with the Login ID