Skip to content

Instantly share code, notes, and snippets.

@jtn-webkoncept
Last active October 27, 2021 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtn-webkoncept/d25c92db8ea27dff275da871be1a4814 to your computer and use it in GitHub Desktop.
Save jtn-webkoncept/d25c92db8ea27dff275da871be1a4814 to your computer and use it in GitHub Desktop.
Run remote executing commands, gather return data
Function Req {
Param(
[Parameter(Mandatory = $True)]
[hashtable]$Params,
[int]$Retries = 3,
[int]$SecondsDelay = 5
)
#$Params.Add('UserAgent', 'For weblogs Require Retry Function')
$method = $Params['Method']
$url = $Params['Uri']
$cmd = { Write-Host "$method $url..." -NoNewline; Invoke-WebRequest -Verbose @Params }
$retryCount = 0
$completed = $false
$response = $null
while (-not $completed) {
try {
$response = Invoke-Command $cmd -ArgumentList $Params
#$retryinfo = $retryamount
if ($response.StatusCode -ne 200) {
throw "Expecting reponse code 200, was: $($response.StatusCode)"
}
$completed = $true
}
catch {
New-Item -ItemType Directory -Force -Path C:\Users\pc1\Desktop\PS2020\t-frame\output\log\singlecommand-log.txt
"$(Get-Date -Format G): Request to $url failed. $_" | Out-File -FilePath 'C:\Users\pc1\Desktop\PS2020\t-frame\output\log\singlecommand-log.txt' -Encoding utf8 -Append
if ($retrycount -ge $Retries) {
Write-Warning "Request to $url failed the maximum number of $retryCount times."
throw
}
else {
Write-Warning "Request to $url failed. Retrying in $SecondsDelay seconds."
Start-Sleep $SecondsDelay
$retrycount++
}
}
}
Write-Host "OK - from req function ($($response.StatusCode))"
return $response, $retryCount
Write-Host "This is trytycount!!! >>> $($retrycount[$_])"
}
$FileList = (Get-ChildItem 'C:\Users\pc1\Desktop\PS2020\t-frame\input\singecommand-input.txt').fullname
$c1 = 0
#Line definition for loading bar
$m = Get-Content -Path $FileList | Measure-Object -line
@($m).lines
$myOutput = @() # Declare empty array
foreach ($FileName in $FileList) {
$myOutput += foreach ($DomainName in (Get-Content $FileName)) {
write-host "----------"
#Loading bar start
$c1++
Write-Host (" --->- Processing $DomainName -<--- ")
Write-Progress -Id 0 -Activity 'Checking servers' -Status "Processing $($c1) of $(@($m).lines)" -CurrentOperation $DomainName -PercentComplete (($c1 / @($m).lines) * 100)
#Loading bar end
$framworkcommand1 = "!Error Default!"
$framworkcommand2 = "!Error Default!"
$framworkcommand3 = "!Error Default!"
$framworkcommand4 = "!Error Default!"
try {
#CHANGE THIS AND BE HAPPY
#DEPLOYER
$deploy1 = "/fwork.php?auth=12512&deploy=g"
#VARIABLE COMMANDS:
$deploy2 = "/installer.php?auth=12512&deploy=gad"
$deploy3 = "/installer.php?auth=12512&deploy=updatef"
#REMOVER
$deploy4 = "/installer.php?auth=12512&remove=t"
#Deploy latest installer
$url1 = $DomainName + $deploy1
$url2 = $DomainName + $deploy2
$url3 = $DomainName + $deploy3
$url4 = $DomainName + $deploy4
write-host "Deploying some awesome shit on: $DomainName"
$framworkcommand1 = Req -Params @{ 'Method' = 'GET'; 'Uri' = $url1 }
$framworkcommand1 = $($framworkcommand1.StatusDescription) + " Content: " + $framworkcommand1.Content
write-host $framworkcommand1
$framworkcommand2 = Req -Params @{ 'Method' = 'GET'; 'Uri' = $url2 }
$framworkcommand2 = $($framworkcommand2.StatusDescription) + " Content: " + $framworkcommand2.Content
write-host $framworkcommand2
$framworkcommand3 = Req -Params @{ 'Method' = 'GET'; 'Uri' = $url3 }
$framworkcommand3 = $($framworkcommand3.StatusDescription) + " Content: " + $framworkcommand3.Content
write-host $framworkcommand3
$framworkcommand4 = Req -Params @{ 'Method' = 'GET'; 'Uri' = $url4 }
$framworkcommand4 = $($framworkcommand4.StatusDescription) + " Content: " + $framworkcommand4.Content
write-host $framworkcommand4
[PSCustomObject]@{
Resolved = $true
deploy = $framworkcommand1
output1 = $framworkcommand2
output2 = $framworkcommand3
remove = $framworkcommand4
DomainName = $DomainName
retries = $retryCount
Status = 'OK'
}
}
catch {
[PSCustomObject]@{
Resolved = $true
deploy = $framworkcommand1
output1 = $framworkcommand2
output2 = $framworkcommand3
remove = $framworkcommand4
DomainName = $DomainName
retries = $retryCount
Status = $_.Exception.Message
}
}
}
}
write-host "<-- Loop of $DomainName complete -->"
$myOutput | Select-Object -Property DomainName,
deploy,
output1,
output2,
remove,
retries,
Status |
Export-Csv -NoTypeInformation -Path C:\Users\pc1\Desktop\PS2020\t-frame\output\output-singlecommand.csv
# or
$myOutput | Out-GridView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment