Posted on Leave a comment

Powershell handle Invoke-WebRequest 404 error

In some cases you may need to use Invoke-WebRequest of powershell as a correct result with status code of 404. A use case for this scenario would be a smoke test for a particular service or URL. In newer powershell versions you can use SkipHttpErrorCheck in order to stop powershell from failing the script.

The below example is a simple Web request that will fail on Powershell <7 with an exception if a 404 response is returned from the webpage.

$req = Invoke-WebRequest -Uri $url -Method GET

The parameter that you could use with Powershell 7++ and you would not have the powershell task failed would be.

$req = Invoke-WebRequest -Uri $url -Method GET -SkipHttpErrorCheck

In order to bypass this issue you could handle the exception on Powershell <7 and add your logic inside the catch block. For example

try {
    $resultApi = MakeWebRequest("http://localhost:1234/api/console") 
catch {
    if( $_.Exception.Response.StatusCode.Value__ -eq 404 ) 
        Write-Host "API console Test 404 , proceeding ..."
    else {
        Write-Host "False response..."
Posted on Leave a comment

Automatically update your GitHub repositories with a powershell script

Developers often have a lot of repositories stored on their local machines. These repositories get updated from other developers and they stay outdated. In many cases developers forget to fetch and pull the latest changes on those repositories and when they commit code, the IDE will notify of the new changes. When this is the case, the commit will get an non explanatory message as the latest of the commit and you will have to navigate on the actual commit to verify the changes and commit message.

Commit message
Merge branch test/v3.0.0 of

In order to resolve this issue, you can create a powershell script that can automatically fetch the latest changes of your local repositories. You will need to change your repositories base location.

#change your github location
$github_directory = "C:\Users\galexiou\Documents\GitHub"
Get-ChildItem $github_directory | ForEach-Object {
if($_.Attributes -eq "Directory")
Write-Host $_.FullName
Set-Location $_.FullName
git fetch
git pull

As you can see from the output below this script will go and fetch the latest changes on the repositories that have been updated.

You can also create a cron job or an automated windows task in order to run this job automatically on computer startup or on your work schedule start. For example

On task scheduler press create task

and select your triggers (when this task will run) along with the action. This will be the run of the powershell script. On the argument you must specify the -File location (where you stored your powershell script).

Posted on Leave a comment

Ansible Privilege Escalation win_command – refresh Windows Update Service

When you have ansible deployed it is easy to perform massively actions on your inventory hosts. On a previous article I have explained in detail how you can easily manage Windows machines with Ansible.

In order to refresh Windows update service and make it connect to WSUS reporting console one should execute the below commands on a range of machines.

wuauclt /resetauthorization
wuauclt /reportnow
wuauclt /detectnow

In order to perform this task automated and avoid logging in on each machine, you can execute the below playbook.

- name: deploy commands on servers
 hosts: test
 become: true
 name: win_command resetauth
 win_command: wuauclt /resetauthorization
 name: win_command report
 win_command: wuauclt /reportnow
 name: win_command detect
 win_command: wuauclt /detectnow 

If you run this playbook without elevated privileges, it will fail. The necessary variables that must be included in your inventory or another appropriate location are listed below (folder variables, etc). The become keyword is a must but the escalation method must be changed instead of sudo as we handle windows machines.

 ansible_connection = winrm
 ansible_user = administrator
Posted on Leave a comment

Get old events from Vcenter using powerCLI

If there is a reason that you want to retrieve older events from Vcenter than the 1000 that are available, you can use powerCLI in order to get specific date ranges.

After you login in the vCenter using

Connect-VIServer SERVER_IP

you can use Get-VIEvent with Start or Finish attributes. Be careful also to use Maxsamples attribute in order to get more than 10 that are returned by default.

For example if I wanted all events that have been captured the weekend I could use something like:

Get-VIEvent -maxsamples 10000 -start "28/07/2018"

P.S Date is a DateTime and should be used as above