I already know Powershell is not really meant for GUI automation, but just the same I have a pretty large Powershell script that needs to perform a single GUI operation, that being a left mouse click. I have searched for hours and have not found a satisfactory solution. The closest I have found is an add on called Wasp, but it was written in 2009 and appears to be incomplete anyway.
Is there another add in available to solve this problem?
I cannot condone using this as a first resort, but it should work. Generally when dealing with web pages you want to use URL parameters as much as possible since webpage elements are volatile.
With that said you should be able to create an IE comobject and then select the element that way. For example:
First create an IE object:
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.navigate("YourPageURLHere.com")
$ie.visible = $true
You can manipulate certain elements by ID (ex: name="username") or even type. These can be easily found be viewing the source code of a webpage. Here are a few examples:
$Username = $ie.document.getElementByID("Username")
$Username.value = $UsernameValue
$SearchButton = $ie.Document.getElementsByTagName("button")
$SearchButton.click()
Even if the webpage changes, since we have it all attached to the IE object you can then grab the output elements that generate after the submit button is clicked and feed them back into your script.
Related
I’m writing a script automating useless tasks at work.
Usually, everything is ok but sometimes a JavaScript is run in order to load specific pages and then all my documents are empty.
Exemple:
$Page = New-Object -ComObject «InternetExplorer.Application»
...
$frame = $Page.document.frames
...
# The script navigates well, set some values get others everything is fine and then:
$frame.item(3).IHTMLDocument3_getElementById(«oAff1»).children[0].(...).click()
# It does the click, the browser loads the new page and:
$frame.item(3).document
# Returns nothing, it seems to load in IE but not for Powershell who still see a white page.
Any Ideas?
I'm trying to scrape this webpage: https://portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV990001, specifically looking to grab the latest Article # from the table for Windows Server 2016 (or alternatively, the smaller table, grabbing the corresponding SSU Package number), which today, would be 4465659.
I'm working on a script to automate grabbing this latest Servicing Stack Update, and also the latest Cumulative Update for Windows Server 2016. I've got the Cumulative Update section working, but the same approach doesn't yield a result on the above link.
For reference, my Cumulative Update approach is below. It parses the page, performs a few adjustments, and ends up with the KBID that I can feed into a download script, which I know works. The problem with using this against the above link, is that the page content appears to be dynamically populated from elsewhere, so I can't seem to get any actual table content returned in PowerShell, from which I would be able to interrogate further.
Also, I need to -UseBasicParsing as this is executing on a Windows Server, potentially with IE out of the picture.
Thanks!
Matt
$buildVersion = "14393"
$kbID = (Invoke-WebRequest -Uri 'https://support.microsoft.com/en-us/help/4000825' -UseBasicParsing).RawContent -split "`n"
$kbID = ($kbID | Where-Object { $_ -like "*heading*$buildVersion*" } | Select-Object -First 1)
$kbID = ((($kbID -split "KB", 2)[1]) -split "\s", 2)[0]
You can't do it with simple http client, because that page runs some javascript and more over it first redirects you to eula page that you need to check/accept. You may google for some rest api returning same data or some other source with static html.
If not - you still can automate it, but you'd need to use actual browser. I guess the most popular way to do it is to use Selenium (it will use FireFox, Chrome or IE). There are probably some other headless browser.
Below is the solution with firefox (install it if not yet), but I believe you can do it with other browsers too. You will also need a C# driver (WebDriver.dll, it's inside Selenium.WebDriver.3.14.0.nupkg) and Mozilla GeckoDriver (geckodriver.exe). You can download it from here: https://www.seleniumhq.org/download/
Put all files in the same folder with you powershell script. The script below:
Add-Type -Path "C:\stack\selenium\WebDriver.dll"
$driver = [OpenQA.Selenium.Firefox.FirefoxDriver]::new()
$driver.manage().timeouts().ImplicitWait = [timespan]::FromSeconds(5)
$driver.Navigate().GoToUrl("https://portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV990001")
$driver.FindElementByCssSelector(".ng-untouched").Click() # check eula box
$driver.FindElementByCssSelector(".btn-primary").Click() # click accept button
#select rows of the seconds table
$data = $driver.FindElementsByTagName("table")[1].FindElementsByTagName("tr").text
$driver.quit()
$data | Select-String "Windows Server 2016"
You can't do this without invoking IE or using some other UI automation.
If you have not hit the page before you get prompted to accept the first page.
Once you hit this URL:
https://support.microsoft.com/en-us/help/4000825
.. you'll find yourself here:
https://portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV990001
Once there the main source of this page does not directly contain the table, if you inspect the table element, then you see the href's, and even those will take you off to the MS Catalog page as a search, that you'd then have to interact with (and if you've neve been to that page, it will force you to accept that one as well). So, this stuff only gets render using the a browser.
So, that means, just getting to the table (excluding all the accepts steps), you'd have something like the below using IE. I am not going to act on that first URL, since you are already dealing with that.
# Get all tables on a web page.
$Url1 = 'https://support.microsoft.com/en-us/help/4000825'
$Url2 = 'https://portal.msrc.microsoft.com/en-us/security-guidance/advisory/ADV990001'
$ie = New-Object -com InternetExplorer.Application
while ($ie.Busy) {Sleep 1}
$ie.navigate($Url2)
while ($ie.Busy) {Sleep 1}
$KBTable = ForEach ($table in $ie.Document.getElementsByTagName('table')){ $table }
$KBPattern = 'https.*KB\d{7}'
[regex]::Matches(($KBTable | ConvertTo-Xml).Objects.Object.Property.'#text',$KBPattern).Value
# Results
https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4093430
https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4093430
https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4465659
… then of course do as you wish with those links.
I want to write a powershell script that will open an Internet Explorer instance as a com object so i can manipulate it. The problem is I need to run it with a parameter to make sure IE is run as a new session.
The parameter I need to pass is -noframemerging I need to pass this so each instance is a new session. Here is the code I've been using to get the instance started.
$ie = new-object -com "InternetExplorer.Application"
If someone could give me a hand figuring out how to be able to create multiple objects as a new session in IE that would be fantastic!
Here is my solution:
$IEProcess = [System.Diagnostics.Process]::Start("iexplore", "-noframemerging")
Sleep -Seconds 1
$ie = $(New-Object -ComObject "Shell.Application").Windows() | ? {$_.HWND -eq $IEProcess.MainWindowHandle}
I launch an instance with the System.Diagnostic.Process library, I was using Start-Process but getting some weird outcomes, this fixed that right up. At this point I have an instance of IE open with the noframemerging switch and a variable pointing the process.
I threw a sleep in there to give the com object a chance to construct before trying to reference it.
Then I grab all the windows com objects and select the one that has a handle (HWND) that is the same as the one from the process I just started and assign it to my $ie variable.
The end result is I have a variable that references a com object of Internet Explorer that is independent of all other sessions. The reason I needed this in particular is I'm opening many instances of IE and logging into a website with different user accounts, if they share a session the login page is skipped and they will already be logged in as the first user that logged in.
Thanks for all the help guys!
Hello I'm looking for a way to search for a word in a word doc and add an endnote(special type of footnote) with a definition of the word as the endnote text. This would allow me to hover over that word and then the definition would pop up like a tool tip.
I know i need to use reflection, but i'm new to the whole reflection thing and all my attempts have fallen flat.
I've found the reference for endnotes here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.endnotes.add%28office.11%29.aspx
I've tried loading C:\WINDOWS\Assembly\Gac\Microsoft.Office.Interop.Word\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll using reflection, but i don't know what to do once i've loaded it. When i try to create an new-object, it still asks me if i've loaded the appropriate dll.
Additionally i tried to fix the problem with a diff method by loading the MS word application as a comobject, but i wasn't able to figure out how to select the text i wanted and then set and endnote.
Any suggestions for this would be greatly appreciated!
-Skyler
I am not too familiar with the Word object model, but if you can handle that part I can tell you how to get an instance of Word running and automated. It's quite simple actually.
$Application = New-Object -ComObject Word.Application
$Application.Visible = $true
$Document = $Application.Documents.Add()
The key is Visible = $true otherwise it will be running but hidden. Now you can use all the methods of the Word Application object to create a new doc and automate it. Now if you're using Word 2007's docx format, you can investigate ZIP file extraction cmdlets and access the xml directly in the word doc. But dealing with namespaces in XML is a hassle and may not be as straightforward.
Word Object Model Stuff
ScriptingGuy recently posted a solution to this: http://blogs.technet.com/heyscriptingguy/archive/2009/10/14/hey-scripting-guy-october-14-2009.aspx
I've been a SharePoint admin for a while, and now have been tasked with a bit more of a developer role - which I'm still very much learning. Most things I've been able to figure out on my own or through Google, but this one has me stumped.
For one particular task I need to use PowerShell to script adding items to a list. Normally - not a difficult task. These steps are all over the web. However, I have yet to find anywhere that will tell you how to add a "Hyperlink" type of item to a list.
I can add one using the following code:
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = $($url.url)
$NewItem.Update()
But I want to set the name/title of the link as well and that's what stumps me. I don't want to have to create a separate column in the list and populate that with the link name, and use code similar to above to populate the url/link.
Does this work for you? I don't have a Sharepoint install available to test on, this is from memory:
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = "$($url.url), <Title>"
$NewItem.Update()
james
Thanks James! That was very close and I'm thinking would work if I was specifying a single item?
Here's my full solution (with some extra bits):
$enumsite = new-object microsoft.sharepoint.spsite($SubWebUrl)
foreach ($url in $enumsite.allwebs)
{
$NewItem = $MyList.Items.Add()
$NewItem["My Hyperlink Column"] = "$($url.url), $(url.title)"
$NewItem.Update()
}
$enumsite.Dispose()
Perhaps this will help someone else out in the future.