I'm working currently on a project to automate some stuff. However I'm quited blocked with a problem in reading an excel file. I have an Excel file (related to MS Form so basically it does contain the results of a form ans it is shared on MS Sharepoint), I tried importing it using Powershell like this :
$value=Import-Excel "C:\Users\sth\sth1\MyFile.xlsx"
The problem is that $value gives me this as a result instead of the table:
TXzkw9Vz0Own80zEzjQhphDvtf05gBAi4P some other stuff
--------------------------------------------------------------------------------
Form1
{d23c5acf-c4d2-47d5-9784-another random string}
FYI I did another test with another .xlsx file not linked to MS Forms and it worked.
EDIT:
$value | Get-Member gives this:
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
cTXzkwEzjQhphDvtf05gBAi4PdtR-BB_JUOU5WSVFVTlFQNFZONERJQ1hGWDg1N0xOOS4u NoteProperty string cTXzkw9Vz0Own80zEzjQhphDvtf05gBAi4PdtR-BB_JUOU5WONERJQ1hGWDg1N0xOOS4u=Form1
EDIT 2:
The table in the excel should be like this
ID, Email, Question1,Questin 2
----------------------------------
1 Email1 answer 1 Answer3
2 Email2 answer2 Answer 4
...
Does anyone has an clue about this ?
Thank you very much !!
Related
I have the following code, which is returning the following information but I only need the name of the clinic and not the rest of the information. How would I remove them?
Intent secondIntent = getIntent();
final String message = secondIntent.getStringExtra("Add a Review for");
This is producing the following information: Ringwood Family Medical Centre : Shop 19A/59-65 :8842 6200 : 3134
but I only need the name of the clinic and I tried using the following code but there is some kind of error:
message = message.split(":", Integer.parseInt("10"));
Actually, I think String#split might be want you want here:
final String message = secondIntent.getStringExtra("Add a Review for")
.split("\\s*:\\s*")[0];
I need to use powershell to resolve IP addresses via whois. My company filters port 43 and WHOIS queries so the workaround I have to use here is to ask powershell to use a website such as https://who.is, read the http stream and look for the Organisation Name matching the IP address.
So far I have managed to get the webpage read into powershell (example here with a WHOIS on yahoo.com) which is https://who.is/whois-ip/ip-address/206.190.36.45
So here is my snippet:
$url=Invoke-WebRequest https://who.is/whois-ip/ip-address/206.190.36.45
now if I do :
$url.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False HtmlWebResponseObject Microsoft.PowerShell.Commands.WebResponseObject
I see this object has several properties:
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
AllElements Property Microsoft.PowerShell.Commands.WebCmdletElementCollection AllElements {get;}
BaseResponse Property System.Net.WebResponse BaseResponse {get;set;}
Content Property string Content {get;}
Forms Property Microsoft.PowerShell.Commands.FormObjectCollection Forms {get;}
Headers Property System.Collections.Generic.Dictionary[string,string] Headers {get;}
Images Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Images {get;}
InputFields Property Microsoft.PowerShell.Commands.WebCmdletElementCollection InputFields {get;}
Links Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Links {get;}
ParsedHtml Property mshtml.IHTMLDocument2 ParsedHtml {get;}
RawContent Property string RawContent {get;}
RawContentLength Property long RawContentLength {get;}
RawContentStream Property System.IO.MemoryStream RawContentStream {get;}
Scripts Property Microsoft.PowerShell.Commands.WebCmdletElementCollection Scripts {get;}
StatusCode Property int StatusCode {get;}
StatusDescription Property string StatusDescription {get;}
but every time I try commands like
$url.ToString() | select-string "OrgName"
Powershell returns the whole HTML code because it interprets the text string as a whole. I found a workaround dumping the output into a file and then read the file through an object (so every line is an element of an array) but I have hundreds of IPs to check so that's not very optimal to create a file all the time.
I would like to know how I could read the content of the web page https://who.is/whois-ip/ip-address/206.190.36.45 and get the line that says :
OrgName: Yahoo! Broadcast Services, Inc.
and just that line only.
Thanks very much for your help! :)
There are most likely better ways to parse this but you were on the right track with you current logic.
$web = Invoke-WebRequest https://who.is/whois-ip/ip-address/206.190.36.45
$web.tostring() -split "[`r`n]" | select-string "OrgName"
Select-String was returning the match as it, previously, was one long string. Using -split we can break it up to just get the return you expected.
OrgName: Yahoo! Broadcast Services, Inc.
Some string manipulation after that will get a cleaner answer. Again, many ways to approach this as well
(($web.tostring() -split "[`r`n]" | select-string "OrgName" | Select -First 1) -split ":")[1].Trim()
I used Select -First 1 as select-string could return more than one object. It would just ensure we are working with 1 when we manipulate the string. The string is just split on a colon and trimmed to remove the spaces that are left behind.
Since you are pulling HTML data we could also walk through those properties to get more specific results. The intention of this was to get 1RedOne answer
$web = Invoke-WebRequest https://who.is/whois-ip/ip-address/206.190.36.45
$data = $web.AllElements | Where{$_.TagName -eq "Pre"} | Select-Object -Expand InnerText
$whois = ($data -split "`r`n`r`n" | select -index 1) -replace ":\s","=" | ConvertFrom-StringData
$whois.OrgName
All that data is stored in the text of the PRE tag in this example. What I do is split up the data into its sections (Sections are defined with blank lines separating them. I look for consecutive newlines). The second group of data contains the org name. Store that in a variable and pull the OrgName as a property: $whois.OrgName. Here is what $whois looks like
Name Value
---- -----
Updated 2013-04-02
City Sunnyvale
Address 701 First Ave
OrgName Yahoo! Broadcast Services, Inc.
StateProv CA
Country US
Ref http://whois.arin.net/rest/org/YAHO
PostalCode 94089
RegDate 1999-11-17
OrgId YAHO
You can also make that hashtable into a custom object if you prefer dealing with those.
[pscustomobject]$whois
Updated : 2017-01-28
City : Sunnyvale
Address : 701 First Ave
OrgName : Yahoo! Broadcast Services, Inc.
StateProv : CA
Country : US
Ref : https://whois.arin.net/rest/org/YAHO
PostalCode : 94089
RegDate : 1999-11-17
OrgId : YAHO
it it very simple to use whois app this is for microsoft put app in System32 or windir and in powershell use whois command then get-string get "orgname" like this
PS C:\> whois.exe -v 206.190.36.45 | Select-String "Registrant Organization"
Registrant Organization: Yahoo! Inc.
I advise you this app because has more information for your work
Here you go, the way to do this is in fact to do an Invoke-WebRequest. If we take a look at some of the properties of the object we get from Invoke-WebRequest, we can see that PowerShell has already parsed some of the HTML and text for us.
All that we have to do is pick out some of the values we'd like to work with. For instance, taking a peek at the ParsedText field, we see these results.
These fields begin on about line 30 or so. In my approach to solving this problem we know that we'll find good data like this mid-way down the page, so if we could scrape the values from these lines, we'd be on our way to working with the data. The code to accomplish this first part is this:
$url = "https://who.is/whois-ip/ip-address/$ipaddress"
$Results = Invoke-WebRequest $url
$ParsedResults = $Results.ParsedHtml.body.outerText.Split("`n")[30..50]
Now, PowerShell has a number of very powerful commands to import and convert data into various formats. For instance, if we could only replace the ':' colon character with an equals sign '=', we could send the whole mess over to ConverFrom-StringData and have rich PowerShell objects to work with. It turns out that we can easily do that using the universal -Replace operator, like this
$Results.ParsedHtml.body.outerText.Split("`n")[30..50] -replace ":","="
I figured you might want to do this again in the future, so I took the entire thing and made it into a simple five line function for you. Throw this into your $Profile and enjoy.
So the finished result looks like this:
Function Get-WhoIsData {
param($ipaddress='206.190.36.45')
$url = "https://who.is/whois-ip/ip-address/$ipaddress"
$Results = Invoke-WebRequest $url
$ParsedResults = $Results.ParsedHtml.body.outerText.Split("`n")[30..50] -replace ":","=" | ConvertFrom-StringData
$ParsedResults }
and using it works this way:
PS C:\windows\system32> Get-WhoIsData -ipaddress 206.190.36.45
Name Value
---- -----
NetRange 206.190.32.0 - 206.190.63.255
CIDR 206.190.32.0/19
NetName NETBLK1-YAHOOBS
NetHandle NET-206-190-32-0-1
Parent NET206 (NET-206-0-0-0-0)
NetType Direct Allocation
OriginAS
Organization Yahoo! Broadcast Services, Inc. (YAHO)
RegDate 1995-12-15
Updated 2012-03-02
Ref http=//whois.arin.net/rest/net/NET-206-190-32-0-1
OrgName Yahoo! Broadcast Services, Inc.
OrgId YAHO
Address 701 First Ave
City Sunnyvale
StateProv CA
PostalCode 94089
You can then select any of the properties you'd like using normal Select-Object or Where-Object commands. For example, to pull out just the orgName property, you'd use this command:
(Get-WhoIsData).OrgName
>Yahoo! Broadcast Services, Inc.
In AfterScenario method, I want to get the rows from table "Examples" in Scenario Outline, to get the values in it, and search that specific values in the database
I know that this can be achieved by using Context.Scenario.Current...
Context.Scenario.Current[key]=value;
...but for some reason I'd like to be able to get it in a simpler way
like this:
ScenarioContext.Current.Examples();
----------- SCENARIO --------------------------------
Scenario Outline: Create a Matter
Given I create matter "< matterName >"
Examples:
| matterName |
| TAXABLE |
----------AFTER SCENARIO -----------------------------------
[AfterScenario()]
public void After()
{
string table = ScenarioContext.Current.Examples();
}
So if you look at the code for ScenarioContext you can see it inherits from SpecflowContext which is itself a Dictionary<string, object>. This means that you can simply use Values to get the collection of values, but I have no idea if they are Examples or not.
The best solution I came up with was to infer the examples by keeping my own static singleton object, then counting how many times the same scenario ran.
MyContext.Current.Counts[ScenarioContext.Current.ScenarioInfo.Title]++;
Of course, this doesn't work very well if you don't run all the tests at the same time or run them in random order. Having a table with the examples themselves would be more ideal, but if you combine my technique along with using ScenarioStepContext you could extract the parameters of the Examples table out of the rendered step definition text itself.
Feature
Scenario Outline: The system shall do something!
Given some input <input>
When something happens
Then something should have happened
Examples:
| input |
| 1 |
| 2 |
| 3 |
SpecFlow Hook
[BeforeStep]
public void BeforeStep()
{
var text = ScenarioStepContext.Current.StepInfo.Text;
var stepType = ScenarioStepContext.Current.StepInfo.StepDefinitionType;
if (text.StartsWith("some input ") && stepType == StepDefinitionType.Given)
{
var input = text.Split(' ').Last();
}
}
I'm using cucumber-jvm. I have the following in a .feature file:
Background:
Given the following account file line:
| First Name | Lance |
| Last Name | Fisher |
| Corporate Name | |
This is a vertically-oriented table. The following generated step definition uses First Name and Lance as the headers, where First Name, Last Name, and Corporate Name should be the headers.
#Given("^the following account file line:$")
public void the_following_account_file_line(DataTable arg1) throws Throwable {
// Express the Regexp above with the code you wish you had
// For automatic conversion, change DataTable to List<YourType>
throw new PendingException();
}
How would I implement a step definition to handle a vertical table rather than a horizontal table?
Just receive the value on parameter as Map<String, String> instead of DataTable. Cucumber will convert the first columns as map keys and the second column as map values.
It's simple enough, as the Datatable can return an array of arrays of strings, and you can step through this in whatever manner you wish.
However, I'd agree with Prakash Murthy that the horizontal table is more usual and useable.
Hi I am developing using the SharePoint namespace and I ran into the following error when I try to retrieve a URL column from one of my lsits.
"Value does not fall within the expected range"
All I am doing is:
item["URL"]
Can someone tell me what I can do about this?
The error definitely means that the field can't be found.
Debug the process and look at the ListItem.Fields.SchemaXML property to find its internal name, it may be saved internally as something other than URL. You can also use the following method to get a list item value.
SPField l_field = l_item.Fields.GetField("URL");
string l_fieldValue = l_item[l_field.Id].ToString();
The GetField method looks for a field by both DisplayName & InternalName.
To get the URL of an SPListItem, use Item.Url.
public static string GetItemURLValue(SPListItem item, string fieldName)
{
string exit = "";
SPFieldUrlValue link = new SPFieldUrlValue(item[fieldName].ToString());
exit = link.Url;
return exit;
}
This usually means "URL" is not a field in the list.
If it's a promoted InfoPath column, try deactivating and re-activating the form template to the site. I have noticed that I have to do this whenever I add a new promoted field to an infopath template.
There is a special method for retrieving URLs. Try this:
SPListItem li = ...
SPFieldUrlValue fuv = new SPFieldUrlValue(li[strFieldName].ToString());
return fuv.Url;
Mine is a Windows application. I used to get this exception after I created the set up and tried deploying.
My application needed to write in Excel and then save it. I used reference of COM component 'Microsoft Excel 11.0 Object'. I noticed when I add this reference actually 3 dll's appear in my reference list.
Microsoft.office.core
Excel
VBIDE
I removed the 'VBIDE' reference and my problem is solved.
If it's just a column name and in "Single line of Text" format, what about:
item["URL"] != null ? item["URL"].ToString() : "Not Found";