System hangs in between running a powershell script. - powershell

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.

Related

Powershell to click on 'ok' to delete an SP site

I am trying to create a script to delete old MOSS 2007 personal sites. I have most of the script working, but where I am having trouble is once a box pops up, to have it click 'Ok'. The script locates the URL, and clicks on 'delete', but then a box pops to confirm deletion and the options are 'Ok' and 'cancel'. I want the script to click on 'ok'. I've done some research on the sendkey methods, but since I'm not well versed in PS, I'm unable to get it to work. Also, please don't suggest using the SP cmds. We have MOSS 2007 running on 2003 servers, so any SP cmds or trying to run the script from the server is a moot point.
Please take a look at my script below.
[void]
[System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
[void]
[System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$user = "username"
$mysiteURL = "http://SharePointSite/personal/$user/_layouts/deleteweb.aspx"
Invoke-WebRequest -UseDefaultCredentials -uri $mysiteURL | Select-Object statusdescription
#Creates an Internet Explorer object
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true
$ie.Navigate($mysiteURL)
while ($ie.Busy -eq $true){Start-Sleep 4;}
$ie.Document.getElementByID('ctl00_PlaceHolderMain_ctl08_RptControls_BtnDelete').click()
#give the focus to ie
[Microsoft.VisualBasic.Interaction]::AppActivate("Message from webpage")
#send keys
start-sleep 1
[System.Windows.Forms.SendKeys]::Sendwait("{ENTER}");
Any help would be appreciated. Thank you!

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.

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 .....

COMAdmin - force refresh of applications after install of proxy

I'm programmatically installing a COM+ proxy component via Powershell, using msiexec on the msi, and then using the COMAdmin.COMAdminCatalog object to set the remote server on the proxy.
The problem is that it takes a while for the newly installed proxy to be available in the "Applications" collection of COMAdminCatalog. Is there some way to force a refresh of the catalog before getting the application list?
Essentially, what I do is this:
msiexec /q /i $appName.msi
use the COMAdmin.COMAdminCatalog to enumerate the apps.
function Set-Remote-Server-For-Complus-Application($appName, $remoteServer) {
$comAdmin = New-Object -comobject COMAdmin.COMAdminCatalog
$apps = $comAdmin.GetCollection("Applications")
$apps.Populate();
$app = $apps | Where-Object {$_.Name -eq $appName}
if ($app -eq $null) {
Write-Warning "Unable to find COM+ app ""$appName""."
Return
}
$app.Value("ApplicationProxyServerName") = $remoteServer
$result = $apps.SaveChanges()
$apps = $null
if ($result -eq 1) {
Write-Output "Successfully set complus remote server ""$remoteServer"" on ""$appName"""
}
}
The problem is that the application is not Found. If I add a Start-Sleep -Seconds 2 between the calls, it works. But, sleeping is not good, because sometimes it might take longer than 2 seconds, sometimes it might only take 200 milliseconds, so the wait is unnecessarily long.
Is there any way to make sure that the COMAdmin.COMAdminCatalog is actually updated before I try to enuerate the applications, without resorting to sleeping and just hoping for the best?