Search This Blog

Tuesday, November 13, 2018

Run TimeStamped Pings against large Computer list

Had to run long term ping test against a number of devices. As such I created the following script that will ping the devices, with a timestamp, logging to a file.

computer list is in a file, should be in the same folder as the script. In this case file called ips.txt.

param ([ Int32 ] $ PingCount = 14400 , [ string ] $ file = '.\ips.txt' )

 

function Get-ScriptDirectory

{

    #Determine the folder in which the script lives.

    $ Invocation = ( Get-Variable MyInvocation -Scope 1 ).Value

    Split-Path $ Invocation .MyCommand.Path

}

 

$ scriptPath = Get-ScriptDirectory

 

$ TimeStampPing = {

    param ( $ comp , $ count , $ scriptPathBlock )

    filter timestamp { " $ ( Get-Date - Format o): $ _ " } ;

    ping $ comp -n $ count | timestamp > " $ ( $ scriptPathBlock )\pingtest- $ ( $ comp ).txt"

  }

 

foreach ( $ line in Get-Content $ file ) {

    Start-Job -ScriptBlock $ TimeStampPing -ArgumentList $ line , $ PingCount , $ scriptPath

}


Share/Bookmark

Monday, November 12, 2018

Powershell: Post code to blogger from Visual Studio Code

Using the blogger web tool, have been able to paste from VSC to web tool and it will colour code.

Just need to jump to HTML view and change the style paramters of the div's from

 white-space: pre;

to

 white-space: pre-wrap;

which will allow for the code to wrap.


OLD: Notes for myself, as I forget by the time I write something new.Smile

Using Visual Studio Code to create Powershell code.

You can copy and paste to word and the formatting is maintained.

Save the file as an rtf file.

Use an rtf converter to convert to HTML. I use the MultiDOC Converter (freeware).

I then paste this HTML code into Windows Live Writer.

Note: If you go from the edit tab, then live writer will change the style class to lower case, and then they wont match the uppercase class names in the code.

So to publish just paste the style block into the code page, just prior to publish.

Reference:

http://www.multidoc-converter.com/en/index.html


Share/Bookmark

Powershell: Cloudflare List all domains and search for ip

Quick Note: This code is quickly put together to prompt for the Cloudflare API key and email, then will list all the domains in cloudflare and also prompt for an ip to search for within all domains. Please ignore the debugging and extra code

$ CloudflareAPIKey = Read-Host -Prompt 'Enter the API Key' ;

$ CloudflareAPIEmail = Read-Host -Prompt 'Enter the API email e.g. user@domain' ;

$ headers = @ {

    "X-Auth-Key" = $ CloudflareAPIKey ;

    "X-Auth-Email" = $ CloudflareAPIEmail ;

    "Content-Type" = 'application/json'

}

 

#this may need changing when Cloudflare update the api

[ string ] $ cloudFlareApiBaseUrl = "https://api.cloudflare.com/client/v4"

 

#Force TLS version to be 1.2 (cloudflare only allows tls1.2 now)

[ Net.ServicePointManager ]::SecurityProtocol = [ Net.SecurityProtocolType ]::Tls12

 

# Get Zones from cloudflare

$ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones" -Method "GET" -Headers $ headers

$ CloudflarePagedDomains = ConvertFrom-Json $ request .Content

[ int ] $ pageCount = $ CloudflarePagedDomains .result_info [ 0 ].total_pages

 

$ CloudflareAllDomains = @ ()

 

$ IpSearchValue = Read-Host -Prompt 'IP address to search for in Cloudflare' ;

 

write-host "List of all domains in cloudflare" -foregroundcolor "Yellow"

for ( $ i = 1 ; $ i -le $ pageCount ; $ i ++)

{

    $ Parameters = @ {

        page = $ i

      }

      $ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones" -Method "GET" -Headers $ headers -Body $ Parameters

      $ CloudflarePagedDomains = ConvertFrom-Json $ request .Content

    #loop through $request.result array...

    Foreach ( $ CloudflareDomain IN $ CloudflarePagedDomains .result ) {

        $ CloudflareAllDomains += $ CloudflareDomain .name ;

        write-host " $ ( $ CloudflareDomain .name ) `r " -foregroundcolor "DarkCyan"

    }

}

 

Foreach ( $ CloudflareDomain IN $ CloudflareAllDomains ) {

    $ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones/?name= $ { CloudflareDomain }" -Method "GET" -Headers $ headers

   

    $ zoneId = $ ( ConvertFrom-Json $ request .Content ).result[ 0 ].id

    write-host "Searching $ (( $ CloudflareDomain ).padright( 75 )) $ ( $ zoneId )" -foregroundcolor "Yellow"

    $ Parameters = @ {

        type = 'A'

        content = $ IpSearchValue

      }

    $ request = Invoke-WebRequest -Uri " $ { cloudFlareApiBaseUrl }/zones/ $ { zoneId }/dns_records" -Method "GET" -Headers $ headers -Body $ Parameters

 

    Foreach ( $ RecordFound IN $ ( ConvertFrom-Json $ request .Content ).result) {

        write-host " $ (( $ RecordFound .name ).padright( 75 )) $ ( $ RecordFound .content )" -foregroundcolor "Green"

    }

}

 

 


Share/Bookmark