I am trying to access Jtrac webpage using powershell. i am able to login into but i am unable to access SEARCH button which href link.
$Url = “http://kbserver/workflow/app/login”
enter code here`$Username=”XXXXX”
enter code here`$Password=”XXXXX”
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true;
$IE.navigate($Url);
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000;
}
$Login = $IE.document.getElementById("loginName3").value = "$Username"
$Login = $IE.Document.getElementById(“password12”).value= "$Password"
$Login = $IE.Document.getElementsByTagName("input") | where-object {$_.type -eq "submit"}
$Login.click();
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 5000;
}
$Login = $IE.Document.getElementsByTagName("a") | where {$_.href -eq "'?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListen er::'"}
$Login.click();
Error which i am getting is
You cannot call a method on a null-valued expression.
At C:\Users\Dinesh\Webbb.ps1:20 char:13
+ $Login.click <<<< ();
+ CategoryInfo : InvalidOperation: (click:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
<a href="?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::">
<img title="SEARCH" src="../resources/search.gif"> </a>
It looks like you have a typo in line 19, ILinkListen er:: at the end of your where instead of ILinkListener::. Where isn't finding anything to match, so it returns null, and that's how you get your null-valued $Login object.
Edit: Your problem is still line 19, you should print out $IE.Document.getElementsByTagName("a") and verify that it's actually returning what you expect, because your where filter is not matching anything and returning $null to $Login.
Edit2: went to a website and grabbed all the hrefs and took a look, powershell removes the "" from the hrefs.
you should have $IE.Document.getElementsByTagName("a") | where {$_.href -eq "?wicket:interface=:2:table:dashboardRows:3:dashboardRow:search::ILinkListener::"} instead.
Edit 3: Your second issue can be solved with regex using the -match operator instead of -eq (-eq does not really work if the item you are checking is dyanmic ).
Like this:
$IE.Document.getElementsByTagName("a") | where {$_.href -match "\?wicket:interface=:\d"}
This regex will return anything that containts "?wicket:interface=:digit".
One digit is the best option here since it will keep growing on refresh, assuming nothing else on the page matches that this should be good.
Related
I've been trying to create a powershell script to automate something in my router's website but I can't really navigate through the sites frames and framesets, any help would be greatly appreciated!
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("http://192.168.1.1")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('userName')
$usernamefield.value = "admin"
$passwordfield = $ie.document.getElementByID('pcPassword')
$passwordfield.value = "admin"
$Link = $ie.document.getElementByID('loginBtn')
$Link.click()
$Frame1a = $ie.document.getElementByID("topFrame")
$Frame2a = $Frame1a.contentWindow
$Frame3a = $Frame2a.document
$Frame1b = $Frame3a.getElementByID('bottomLeftFrame')
$Frame2b = $Frame1b.contentWindow
$Frame3b = $Frame2b.document
$thing = $Frame3b.getElementsByTagName('a') | where-object {$_.innerText -eq 'Wireless'}
This is the code I have so far, but when it gets to $Frame1b = $Frame3a.getElementByID('bottomLeftFrame') I get an error message saying: "You cannot call a method on a null-valued expression."
After I get this frame thing to work, I'd need it to click a hyperlink, if anyone knows how to do that please tell me, I've tried before but it didn't work. I've also tried different languages before, python, VBS, but I got the furthest with powershell.
Thanks for the attention!
I had similar issue in the past. I solved it like this:
$ie = New-Object -ComObject 'internetExplorer.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("http://192.168.1.1")
While ($ie.Busy -eq $true) {Start-Sleep -Seconds 3;}
$usernamefield = $ie.document.getElementByID('userName')
$usernamefield.value = "admin"
$passwordfield = $ie.document.getElementByID('pcPassword')
$passwordfield.value = "admin"
$Link = $ie.document.getElementByID('loginBtn')
$Link.click()
start-Sleep -Milliseconds 500
#Click Wireless
$oFrameLeft=$ie.document.parentwindow.frames['bottomLeftFrame'].document
$oWireless = $oFrameLeft.IHTMLDocument3_getElementsByTagName('a') | where-object {$_.innerText -eq 'Wireless'}
$oWireless.click()
I want to automate a task at my work where i need to fill a form in one of our intranet sites. Basically i need to call the site, fill a specific textbox and click the save button. I am trying first to test the code on Google website but it is not working, although i am copying most of it from various sources.
The code mostly works fine. I am able to open Internet explorer, navigate to google, fill the search textbox, but i am stuck when clicking the button.
$Url
$Textbox
$Url = “www.google.com”
$Textbox=”test”
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true
$IE.navigate($url)
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000
}
$IE.Document.getElementsByname(“q”)[0].value = $Textbox
$IE.Document.getElementsByname(“btnk”)[0].Click()
while ($IE.Busy -eq $true)
{
Start-Sleep -Milliseconds 2000
}
The error message i keep getting is this:
You cannot call a method on a null-valued expression. At line:17
char:1
+ $IE.Document.getElementsByname(“btnk”)[0].Click()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
enter image description here
getElementsByname name parameter is case sensitive.
The name of the Search element is btnK and not btnk.
Change to:
$IE.Document.getElementsByname("btnK")[0].Click()
I have reproduced the problem on my machine, it seems that this behavior is related to the IE browser protected mode. Before turning on the protected mode, it will show this behavior.
To solve this issue, please turn on the Protected Mode (in my machine, I enabled the protected mode in different zones. The screenshot like this).
Besides, you could also run the PowerShell as admin.
The PowerShell script as below:
$Url
$Textbox
$Url = "www.google.com"
$Textbox="test"
$IE = New-Object -com internetexplorer.application;
$IE.visible = $true
$IE.navigate($url)
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("q")[0].value = $Textbox
Start-Sleep -Seconds 1
$IE.Document.getElementsByName("btnK")[0].Click()
while ($IE.Busy -eq $true) { Start-Sleep -Milliseconds 2000 }
I've always used:
$Button_Name.RaiseEvent((New-Object -TypeName System.Windows.RoutedEventArgs $([System.Windows.Controls.Button]::ClickEvent)))
Because it works for me and I'm too lazy to look for anything else.
I am still new to Powershell and haven't been able to find anything on this. I am running a REST GET request to a URI which I know for a fact returns a 404 from the server since the resource is not found.
I would like to be able to run a conditional that checks if it's a 404 and skip over it for further processing if this is the case however when I assign the request to a variable, then calling on that later, it just gives me the contents of what my request was. I have never seen anything like this before in other languages...
My basic premise is the following. I first fetch all group names, then loop through that array of names, include the current one in a new URL and make an additional request for that specific group which looks for a SHIFT which will always have the same name. If the group doesn't have that shift by name I want to skip to the next group, otherwise alter some attributes of that newly found shift object.
Here is what my code looks like, as you can see it's not acting correctly
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$user = '******'
$pass = ConvertTo-SecureString '*******' -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $pass
$req = Invoke-WebRequest -Credential $cred -Uri https://********-np.xmatters.com/api/xm/1/groups
$res = ConvertFrom-Json $req.Content
$content = $res.data
$base = "https://********-np.xmatters.com/api/xm/1/groups"
$group_name = $content[0].targetName
$path = "$base/$group_name/shifts/MAX-Default Shift"
$shift = Invoke-RestMethod -Credential $cred -Uri $path
Write-Host '-----------------------'
Write-Host $shift
Write-Host '-----------------------'
... RESPONSE BELOW ....
Invoke-RestMethod : The remote server returned an error: (404) Not Found.
At \\MMFILE\********$\MyDocuments\Group Supers PReliminary.ps1:16 char:10
+ $shift = Invoke-RestMethod -Credential $cred -Uri $path
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
-----------------------
#{id=***********; group=; name=MAX-Default Shift; description="; start=2018-08-21T04:00:00.000Z; end=2018-08-22T04:00:00.000Z; timezone=America/New_York; recurrence=;
links=}
-----------------------
PS C:\WINDOWS\system32>
What I would like to do is something like, in shorthand code, if $shift.code == 404 ... skip ... else ... run additional query
You need to use a try ... catch.
$code = ""
try{
$shift = Invoke-RestMethod -Credential $cred -Uri $path
}
catch{
$code = $_.Exception.Response.StatusCode.value__
}
if($code -eq "404")
{
continue
# Other things
}
else
{
Write-Host '-----------------------'
Write-Host $shift
Write-Host '-----------------------'
}
You could suppress the error message via Try..Catch and in so doing allow the script to continue:
Try {
$Shift = Invoke-RestMethod http://www.google.com/fakeurl -ErrorAction Stop
#Do other things here if the URL exists..
} Catch {
if ($_.Exception -eq 'The remote server returned an error: (404) Not Found.') {
#Do other things here that you want to happen if the URL does not exist..
}
}
Note this will hide all terminating errors from Invoke-ResetMethod. You could then use an if statement to see if the exception was a 404 and then perform further actions accordingly.
I've created a function "Query-ComDomElements.ps1" to query HTML objects.
This works quite well when querying only one object and querying that again.
When I try calling it in recursion it however fails and I don't understand why. The code/objects is/are the very same.
Could anyone please enlighten me why the query .container>img is not working, but querying .container and with that img is?
The error I get when querying both (and thus calling the function recursively) is:
Exception calling "InvokeMember" with "5" argument(s): "Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"
At C:\path\to\Query-ComDomElements.ps1:31 char:5
+ ... $result = [System.__ComObject].InvokeMember("getElementsB ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
Here my sample script (function Query-ComDomElements.ps1 not included but on github):
. C:\path\to\Query-ComDomElements.ps1
$ie = New-Object -ComObject "InternetExplorer.Application"
$ie.Navigate2("https://www.gpunktschmitz.de/")
while($ie.Busy) {
Start-Sleep -Seconds 1
}
#this works
$imgContainer = Query-ComDomElements -Query '.container' -Dom $ie.Document
$image = Query-ComDomElements -Query 'img' -Dom $imgContainer -Property 'src'
#this fails
$image = Query-ComDomElements -Query '.container>img' -Dom $ie.Document -Property 'src'
$ie.quit()
I think the problem is occurring because $dom ends up being an array with two elements when it is passed in on the second iteration. One (dirty) fix for this would be to use Select-Object to just get the first element (suggest using Select rather than [0] so that if its not an array it doesn't error):
if($SecondQuery -eq $false) {
if($Property -ne $false -and $Property -ne '*') {
return $result.$Property
} else {
return $result
}
} else {
return Query-ComDomElements -Query $SecondQuery -Dom ($result | select -first 1) -Property $Property
}
I've got a function which works fine.
It pulls the first character of the firstname and the whole lastname from a text box in a PowerShell GUI and then it creates a sAMAccountName from both.
Now I need only the first 8 characters from the generated sAMAccountName.
Here is the function
Function Set-sAMAccountName {
Param([Switch]$Csv=$false)
if(!$Csv)
{
$GivenName = $txtFirstName.text
$Surname = $txtLastName.text
}
else{}
Switch($XML.Options.Settings.sAMAccountName.Style | Where{$_.Enabled -eq $True} | Select -ExpandProperty Format)
{
"FirstName.LastName" {"{0}.{1}" -f $GivenName,$Surname}
"FirstInitialLastName" {"{0}{1}" -f ($GivenName)[0],$Surname}
"LastNameFirstInitial" {"{0}{1}" -f $Surname,($GivenName)[0]}
Default {"{0}.{1}" -f ($GivenName)[0],$Surname}
}
}
Any ideas?
Thx a lot in advance
Substring works like that:
you pass the index of where you want to start
you pass the index of where you want to end the reading of the substring (if your not passing anything it will go until the end of the string's length)
so in your case it will be start reading at index 0 end reading at index 8:
$str = "a simple string"
$newString = $str.Substring(0,8)
I really recommend to read about string manipulation here
Okay I got it now!
I've added the -and condition to check the length of the sAMAccountName and said -lt 8 and it's working now. The sAMAccountName is now 8 characters long.
This was the code before:
$txtName_TextChanged={
Write-Verbose "Creating required account fields"
if ($XML.Options.Settings.DisplayName.Generate -eq $True) {$txtDN.Text = Set-DisplayName}
if ($XML.Options.Settings.sAMAccountName.Generate -eq $True) {$txtsAM.Text = (Set-sAMAccountName)}
if ($XML.Options.Settings.UPN.Generate -eq $True) {$txtUPN.Text = Set-UPN}
}
And after the change:
$txtName_TextChanged={
Write-Verbose "Creating required account fields"
if ($XML.Options.Settings.DisplayName.Generate -eq $True) {$txtDN.Text = Set-DisplayName}
if ($XML.Options.Settings.sAMAccountName.Generate -eq $True -and $txtsAM.Text.Length -lt 8) {$txtsAM.Text = (Set-sAMAccountName)}
if ($XML.Options.Settings.UPN.Generate -eq $True) {$txtUPN.Text = Set-UPN}
}