I am looking for that line from .vbproj:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
here is my script:
$xml = [xml](Get-Content $xmlFile)
$propertyGroup = $xml.SelectSingleNode("//PropertyGroup[@Condition= ""''`$(Configuration)|`$(Platform)'' == ''Debug|AnyCPU''""]")
if ($propertyGroup) { Write-Host 'Element found.'
} else { Write-Host 'Element not found.'
}
$xml.SelectSingleNode("//PropertyGroup[normalize-space(translate(@Condition,`"'$`",`"`"))='(Configuration)|(Platform) == Debug|AnyCPU']")
We escape ” and $ characters and use translate
function to do it only once.
Another option with contains
and no escaping :
$xml.SelectSingleNode("//PropertyGroup[contains(@Condition,'(Configuration)') and contains(@Condition,'(Platform)') and contains(@Condition,'Debug') and contains(@Condition,'AnyCPU')]")
2 gold badges8 silver badges12 bronze badges
Introduction
Website automation health check typically refers to proactively monitoring and testing a website or web application to identify issues before they impact customers. Today I will show you how to monitor the website uptime and auto login the website for checking.
Why Powershell
Why would I use Powershell for auto health checking? Powershell is the native application in Windows, so you don’t need to install other software or environment and just can use it, this will be very convenient for some companies’ servers, maybe for some security reasons that do not allow to installation of any 3 party software in the server, and it also needs to handle the auto health check service on schedule, in this time, the powershell is the best choice!
Are you want to be a good trading in cTrader? >> TRY IT! <<
Preparation
1) Create an empty folder for your project, e.g. AutoHealthCheck
2) Create a libs
folder, we will put the preparation libraries in it
3) Download the Selenium
We bill based on Selenium
for the auto testing, so the first thing we need to download the Selenium
web driver. We just need the WebDriver.dll
, so go to Nuget and download the Selenium.WebDriver
package (the latest version is 4.12.4 this time)
Rename the selenium.webdriver.4.12.4.nupkg
to selenium.webdriver.4.12.4.zip
and unzip it, copy the below file to your project libs
folder
4) Download the latest chrome driver below, it must be based on your chrome version
put the chromedriver.exe
file into your project libs
folder
Start Coding
Even just a simple program and only a single file will be ok, but I think we still need to design the flow and some helper functions:
1. Workflow
1) We will create a PowerShell script file and put it into Windows’s schedule for auto-run
2) There should be a setting file for controlling which website we need to check
3) For security reasons, if we need to check the login page it needs to encrypt the password in the settings file
4) The script will auto-call Chrome to access the URL and check whether the element exists based on the setting value
5) It needs to write a log for checking the result
Okay, we need to accomplish the above function, we can create some helper functions first.
2. Write Log Function
The log is very important and can help us to find the issue, and we can easily write log in PowerShell with below
and define the log file name and path
$LogFileName = (Get-Date).toString("yyyyMMdd-HHmmss")
$Logfile = "$RootFloder\logs\$LogFileName.log"
3. Read INI File Function
We will use the ini
file for a setting file, the structure will be as below
I will explain these later, for now, if we want to read this file, we can create the below function
and usage as below
4. Send Email Function
This is an auto-schedule task so we should send an email for notification after done, we can create the below send email function, and it will attach the log file
5. Use the Selenium in Powershell
There is a Selenium PowerShell Module for help to use Selenium in PowerShell. But I think the module it’s too complex(there are many functions unnecessary to use in this case) and maybe some companies would not allow to installation of any 3-party PowerShell module, so I will write the logic by myself 🙂
1) Import the Selenium
There are 3 options to import the Selenium
library
2) Create the Chrome option
We can create the chrome option to set how the chromedriver
works, for example, set the maximize window or use the chrome extensions
$ChromeOption = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOption.AddExcludedArgument("enable-automation")
$ChromeOption.AddArguments("--start-maximized")
#$ChromeOption.AddArguments('--headless') #don't open the browser
# Ignore the SSL non secure issue
$ChromeOption.AcceptInsecureCertificates = $true
# Create a new ChromeDriver Object instance.
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOption)
If the checking website does not support SSL, it needs to ignore it, so we need to use the Chrome option for that
6. Get the Settings from Ini
We need to get the settings from ini
file as below
$CheckItems = Get-IniFile "$RootFloder\config.ini"
foreach the result
7. Get the Element
We can get the website element with XPath
with Selenium
in PowerShell, so there are some settings in out ini
, we define what element should be found
The about testing site is my previous article’s demo project.
And we can find the XPath
in chrome like below
1) Right click the element and inspect it
2) Right click the HTML code with the element and Copy
=> Copy XPath
, then just copy into your ini
file
We can use the below code to get the element by XPath
in PowerShell
and you also can execute the Javascript
in this element
8. Encrypt and Decrypt the Password
When we want to auto login to a website for testing, we need to put the password in the setting file, but that’s a security issue if we just set the password directly, so we need to encrypt it.
We can put the password into an encrypted file (e.g. login_info.dat) and decrypt it when we need to use it.
Create another PowerShell
script file, put the below codes
8. Auto Login to the Website
I will show you how to automate login to a website. We still use the demo project :
9. Find the Element by Tag Name
We also can find the element by tag name, for example below structure
and then get the element by tag name, because there will be multiple elements with a tag, so if you want to get the first one, just get the index 0 as below
of course, you can find the tag name directly, but there are many tags on a page, so if you find the tag based on the parent will be easy to find it
In the end, we can create a batch file to execute the Powershell script so that it can be easy to use
Conclusion
PowerShell is a powerful script for handling many server-side problems, it very suitable for use in some servers that’s require a lot of security restrictions. You can find the full source code in github.
Stackademic
Thank you for reading until the end. Before you go:
- Please consider clapping and following the writer! 👏
- Follow us on Twitter(X), LinkedIn, and YouTube.
- Visit Stackademic.com to find out more about how we are democratizing free programming education around the world.
After learning about the tool suite, Sysinternals, we are now going to be learning about logs, specifically Windows Event Logs. I’m familiar with it but I haven’t really delved too far in into it. It’s good they mentioned how it can be useful with SIEMs as a few of the Splunk labs in TryHackMe had me looking into logs. I think this is just a good module overall to understand a bit more about what the logs mean and how to extract relevant information from it.
Task 2 Event Viewer
1: What is the Event ID for the first recorded event?
We will be heading over to a specific log for this and the next few questions. Open event viewer in the machine by right clicking the start menu (Windows icon) at the bottom left and click event viewer. There are multiple ways to do this but I did it this way.
Event viewer should open up. On the left side, we will navigate to our folder that we need. Expand “Applications and Services Logs” then “Microsoft” then “Windows” then “Powershell” and finally click on “Operational.” This is where we will be working in.
Once you are here, click on “Date and Time” to rearrange it and scroll either to the bottom or top to look for the earliest date. This specific entry’s Event ID will be the answer.
2: Filter on Event ID 4104. What was the 2nd command executed in the PowerShell session?
I did this by filtering the current log. It is located on the right side of event viewer. Click on “Filter Current Log” and a new window should pop up.
Enter “4104” where <All Event IDs> is at. In the picture below, you will see where I entered “4104.”
Click ok and the PowerShell’s Operational log will only display entries with 4104 for the Event ID. Look for the second earliest entry and look at the General tab to see what the entry says. In this case, logged what command was used.
3: What is the Task Category for Event ID 4104?
Read the “Task Category” column to find the answer.
Answer: Execute a Remote Command
4: Analyze the Windows PowerShell log. What is the Task Category for Event ID 800?
Looks like we are going to be changing locations for this question. Head all the way to the bottom to find “Windows PowerShell.” If you don’t see it, the path should be Event Viewer (Local) > Applications and Services Logs > Microsoft > Windows PowerShell
Now we look for an entry with 800 for their event ID and then look at the task category.
Answer: Pipeline Execution Details
Task 3 wevtutil.exe
1: How many log names are in the machine?
We are using PowerShell now. Open PowerShell by clicking on the blue icon on the bottom left.
2: What event files would be read when using the query-events command?
Typing in wevtutil eq /?
and reading the output helped me find this answer.
Answer: Event log, log file, structured query
3: What option would you use to provide a path to a log file?
Typing in wevtutil eq /?
and reading the output helped me find this answer.
4: What is the VALUE for /q?
Typing in wevtutil eq /?
and reading the output helped me find this answer.
Answer: XPath query
5: What is the log name?
I copy and pasted the command that was given to us from the reading into PowerShell. The code is wevtutil qe Application /c:3 /rd:true /f:text
. Reading the output, we can see the log name.
6: What is the /rd option for?
Typing in wevtutil eq /?
and reading the output helped me find this answer.
Answer: Event read direction
7: What is the /c option for?
Typing in wevtutil eq /?
and reading the output helped me find this answer.
Answer: Maximum number of events to read
Task 4 Get-WinEvent
1: Execute the command from Example 1 (as is). What are the names of the logs related to OpenSSH?
This command is actually located in the reading. Type in Get-WinEvent -ListLog *
in PowerShell and hit enter. The answer is conveniently located near the bottom for us, so no need to scroll a lot!
2: Execute the command from Example 8. Instead of the string *Policy* search for *PowerShell*. What is the name of the 3rd log provider?
Grabbing the command from Example 8 in the online guide, we get Get-WinEvent -ListProvider *Policy*
. We do have to change it a little bit to fit our question so our final command is Get-WinEvent -ListProvider *PowerShell*
. Enter this and then press enter. We will be provided with 3 log providers. The third one is the answer.
3: Execute the command from Example 9. Use Microsoft-Windows-PowerShell as the log provider. How many event ids are displayed for this event provider?
4: How do you specify the number of events to display?
Reading the online guide, I saw that Example 13 had an interesting parameter. It had -MaxEvents
. I read a bit more and it helps display a maximum number of events.
5: When using the FilterHashtable parameter and filtering by level, what is the value for Informational?
After spending some time digging around through the online documentation, I found this page. It provided detailed explanation on how to use FilterHashtable. I also found the values and what they meant.
Task 5 XPath Queries
1: Using the knowledge gained on Get-WinEvent and XPath, what is the query to find WLMS events with a System Time of 2020–12–15T01:09:08.940277500Z?
3: Based on the previous query, how many results are returned?
Press enter when you enter the command if you did not yet. There will be two results.
4: Based on the output from the question #2, what is Message?
All we have to do is read the message. They are both the same!
Answer: 12/17/2020 1:57:14 PM
6: What is the Provider Name?
The provider name will be right above the the first entry that we obtained from the previous question.
Task 7 Putting theory into practice
1: What event ID is to detect a PowerShell downgrade attack?
For this one, I used Google and searched it up.
It’s not the most efficient way, but I found the answer through scrolling through the logs. I found all but one log had a host version of 5.1.
The attack downgraded the version to 2.0.
I started from the earliest date and then scrolled up. I just held the up arrow key as the information never changed spots, which was extremely helpful for me. Once you find this entry, look at the time stamp!
Answer: 12/18/2020 7:50:33 AM
3: A Log clear event was recorded. What is the ‘Event Record ID’?
This took me a long amount of time. I initially searched for Log Clear Event ID, which returned 1104. When I entered it in the filter, it did not produce any result. I had to keep refining my search and I had to check further down the search results where I found out that there is another event ID, 104. I found my answer here under “Event Log Manipulation.” Seems like a useful site to also keep around as a quick cheat sheet for event IDs. I entered 104 the filter and I got one result!
4: What is the name of the computer?
The screenshot above also shows the answer, just slightly below EventRecordID.
5: What is the name of the first variable within the PowerShell command?
Probably not the “right” way to do it but I checked the hint and saw -Oldest
. My assumption is that it was the oldest event. I filtered the event ID to 4104 and then looked for the oldest event.
We can get the information under Date and Time column.
Answer: 8/25/2020 10:09:28 PM
7: What is the Execution Process ID?
Go to the Details tab and click either “Friendly View” or “XML View”
and look for the Execution Process ID. I picked Friendly View as it is easier to see but XML View has it too.
8: What is the Group Security ID of the group she enumerated?
9: What is the event ID?
4799 is the answer here because the intern checked the entire group.
This was a really tough room for me, coupled by the fact I could not filter the way I wanted with XPath. Furthermore, all my searching was done using the GUI. I really did wish I was able to use PowerShell more. I was able to get some good experience out of this still. I found a few sites that were really helpful and highlighted some key event IDs I should be aware of and how to utilize Event Viewer to help find threats.