Wakeup SharePoint with PowerShell as Scheduled Task - powershell

I have this integration to SharePoint which only works when the SharePoint site has been visited. I've made a PowerShell script to open Internet Explorer, pass credentials and close IE again. Works great when I'm logged on to the server. BUT when I set up the job as a schedule it doesn't work. Found out that the task does not have access to the UI, and therefore does not access the site as if the user is logged on. I've tried a script running thru files and sites to see if that wakes the site, but no luck. I need to get pass the "Working on it..." screen on the site before the integration works. Need to do this every morning
Working on it
$wshell = New-Object -ComObject wscript.shell;
$ie = Start-Process -file iexplore -arg 'http://[websiteurl]' -PassThru
$wshell = New-Object -ComObject wscript.shell;
Sleep 1
$wshell.AppActivate('Windows Security')
Sleep 1
$wshell.SendKeys('[Domain]\[service_account]')
$wshell.SendKeys('{tab}')
$wshell.SendKeys('[service_account_password]')
$wshell.SendKeys('{enter}')
sleep 25
$ie.Kill()
Any ideas on how to wake my sharepoint automatic? How to visit the page on the UI on a scheduled task?

Related

PowerShell script to log in to Linkedin

Fairly new to PowerShell and exploring its capabilities. I have created the following script to automatically log in to LinkedIn, but it opens the web page and does nothing else, can some please assist? I wish to use the script to log in to a web status page and search for issues for alerting purposes, thank you.
PowerShell Script
$username = "Username"
$password = "Password"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://www.linkedin.com")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$usernameElement = $ie.document.getElementById("login-email").value= "$username"
$ie.document.getElementById("login-password").value = "$password"
$ie.document.getElementById("login-submit").submit()
start-sleep 20
OK, took me a while, but I figured out the issue. On LinkedIn specifically, the login-submit button has two notable properties: isDisabled and disabled. Those need to be both changed to $true before it can be clicked. Also, change the function called on it from .submit() to .click(). Also, a quick tip: After the script is done (I'm not sure if this is still true, because you seem to manually close it afterwards), $ie is kept under your script's management. To release it, call this command (after you manually close it, so it might be hard to get in there):
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($ie) #>
Not really sure why, I just know it keeps it out of the way and is apparently equivalent to killing it from the task manager.

System hangs in between running a powershell script.

I am trying to automate to extract data from a webpage after clicking on a button. But when I try to execute, the system freezes and it requires hard reboot. Please refer the code below.
$ie = New-Object -ComObject Internetexplorer.application
$ie.visible = $true
$ie.navigate("http://beta.speedtest.net")
while($ie.readystate -ne 4) {
sleep -Milliseconds 100
}
$link = $ie.document.getElementsByTagName("span") |Where-Object {$_.innertext -like "Begin test"}
$link.click()
Reference: Powershell website automating button click on loginenter code here
There is nothing in there which would cause a system freeze.
As you are starting an Internet Explorer instance, I guess IE is crashing your system and it is unreleated to your script or powershell.
You can verify by running the script on another machine.
Then I would try reinstalling IE on the crashing machine.

Remote Powershell Popup message to all users not working

I'm having some issues creating a remote powershell popup message.
I've got a working script that displays the popup message that I want to send to a remote user. However, whenever I bundle it up in a .ps1 script and run it remotely it does not send a popup message to the logged in user. I know the script is running correctly, as I have other parts of the script that execute correctly. I was able to run the popup message on a local machine, so it is not a script error.
The script is:
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object system.Windows.Forms.Form
$Form.Text = 'ALERT!'
$form.ControlBox = $false;
$Image = [system.drawing.image]::FromFile('\\filepath')
$Form.BackgroundImage = $Image
$Form.BackgroundImageLayout = 'Stretch'
$Form.Width = (680)
$Form.Height = (550)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(500,445)
$OKButton.Size = New-Object System.Drawing.Size(100,50)
$OKButton.Text = 'Accept'
$OKButton.Font = New-Object System.Drawing.Font('Times New Roman',18)
$OKButton.Add_Click({$Form.Close()})
$Form.Controls.Add($OKButton)
$Form.Add_Shown({$Form.Activate()})
[void] $Form.ShowDialog()
I'm running this as part of a .ps1 that is called in the following fashion:
Invoke-WmiMethod -Class Win32_Process -ComputerName $computer -Name Create -ArgumentList "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe \\$computer\C$\folder\script.ps1
I'm running the script as a service account from a server that's executing the commands on a Windows 7 enterprise desktop machine. The target machine execution policy is set to unrestricted so I know it is not a script execution policy issue.
I have a hunch that the reason that it is not popping up on the target machine is because the account that is running the script and the account that is logged into the target machine are different, however I could be incorrect.
Your hunch is correct. The PowerShell script is running in the context of the service account, and won't show up for any of the logged in users. (for ex. if your script launched notepad.exe, it would run only for the service account).
You will see it pop up when you run the command locally under the same account.
#Adrian R is correct you can use msg.exe to send a message, or the more commandline friendly:
net send /users message
If you want to show something more complex (i.e. a full-on Windows form) instead of the classic message box, then you need to be running something in the user context. One way to do this is to use PsExec with the -i (interactive option) instead of using PowerShell remoting.

Powershell start IE on remote machine with specific URL

Is there a way to start IE on a remote machine pointing it to a specific URL using powershell.
This script will only open on my local machine:
$PC = Read-Host "Name of machine to run cookie creator on"
$URL = "http://www.google.co.uk"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate($URL)
Any ideas? Thanks
You can't.
When you use PowerShell remoting, it creates a session on the remote computer which cannot be interacted with and has no UI. So while you can start the IE process, the UI will never be visible to any user.

Powershell Web Page Automation works on Internet, not Intranet

I'm trying to do some simple automation with Powershell, pulling link URLs from one of our company's local intranet pages, and then doing some work with those URLs. Eventually I'll use the script to open each link and click a button on the page. I'm using Internet Explorer 9 in Windows 7 x64.
Here's an example of a simple working powershell script that displays all the links on a page:
$ie = new-object -com "InternetExplorer.Application"
$ie.Visible = $true
$ie.Navigate( "http://www.reddit.com" )
While ($ie.Busy) {
Sleep 1
}
$links = $ie.Document.getElementsByTagName("a")
$links | foreach {
write-host $_.href
}
This script works fine until I replace the URL with a local intranet site. It follows the normal URL scheme ( http://internaldomain.com/etc ), but it's recognized as an intranet site. Once I'm trying to scrape a page in the intranet zone, the $ie.Document value suddenly becomes NULL and the script fails.
I'm guessing it's related to some obscure setting for that zone... I'm not sure. I found some suggestions online such as adding it to your trusted sites, but that has not worked. This is my first time using Powershell for web automation, so any help or insight would be appreciated.
Maybe the solution is here: http://blogs.msdn.com/b/ieinternals/archive/2011/08/03/internet-explorer-automation-protected-mode-lcie-default-integrity-level-medium.aspx
It explained the different levels of tabs, in ie. You have to use the "medium tab" to navigate in local zone.
Basically, the best way to keep your ie settings and use your script is to create a registry key, as explained in the link above.
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\InternetExplorer.ApplicationMedium]
[HKEY_CLASSES_ROOT\InternetExplorer.ApplicationMedium\CLSID]
#="{D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E}"
And in your script, use this new com object:
$ie = new-object -Com InternetExplorer.ApplicationMedium
...
Due to policy restrictions on my computer, I was not able to access the registry to create the key mentioned in another answer. However, I did find a way to do it indirectly using PowerShell in case this is helpful to anyone else:
$type = [Type]::GetTypeFromCLSID('D5E8041D-920F-45e9-B8FB-B1DEB82C6E5E')
$ie = [System.Activator]::CreateInstance($Type)
$ie.Visible = $true
$URL = "http://my.intranet.com"
$ie.Navigate($URL)
Write-Host "`$ie.Busy:" $ie.Busy
Write-Host "`$ie.ReadyState:" $ie.ReadyState
while($ie.Busy -or ($ie.ReadyState -ne 4) ) {
Start-Sleep -s 1
}
Write-Host "IE is ready"
Use
$ie.Document.documentElement.getElementsByClassName("underline")
and enjoy .....