Loops with Powershell and other Syntax stuff
Loops
There are several ways to do loops in powershell.
Here is an inline example
$a | %{$_.object <do some action>};
% = foreach
I prefer this method of looping. It makes it easier for me to visualize.
$a = <list of stuff>
foreach($stuff in $a){
<do some action>
}
Another loop I have started to use is the DO WHILE LOOP. In this example I start a database backup job. I want to be able to verify that the backup job complete successfully before moving forward in the script, by using the Read-Host cmdlet.
I start the backup job via the $backup and the .start() method. Then wait 5 seconds so that I am sure that the job starts and I don't get false positives. Then I start the DO WHILE LOOP. Print to the screen "backup job running......" and then check if the job is still running. If $status.CurrentRunStatus contains 'executing' then keep looping. If the job succeeds or fails then the loop will stop. I then write to the screen the status of the job completion, either it failed or succeeded.
$job = dir | ?{$_.name -like '*backup*' -and $_.isenabled -eq 'true'}
$backup =$job.start()
start-sleep -s 5
do
{
"backup job running......"
$status = dir
$status.refresh()
$status = dir | ?{$_.name -like '*backup*' -and $_.isenabled -eq 'true'}
start-sleep -s 2
} while($status.CurrentRunStatus -contains 'executing')
write-host "Backup Last run outcome ----> "$status.LastRunOutcome -BackgroundColor Blue -ForegroundColor Red
$response = read-host -Prompt "Did backup job for '$($instance)' successfully complete Y/N"
if ($response -eq 'Y'){ <do some action>}
else
{ write-host 'killing process troubleshoot backup failure' -ForegroundColor red -BackgroundColor blue }
There are several ways to do loops in powershell.
Here is an inline example
$a | %{$_.object <do some action>};
% = foreach
I prefer this method of looping. It makes it easier for me to visualize.
$a = <list of stuff>
foreach($stuff in $a){
<do some action>
}
Another loop I have started to use is the DO WHILE LOOP. In this example I start a database backup job. I want to be able to verify that the backup job complete successfully before moving forward in the script, by using the Read-Host cmdlet.
I start the backup job via the $backup and the .start() method. Then wait 5 seconds so that I am sure that the job starts and I don't get false positives. Then I start the DO WHILE LOOP. Print to the screen "backup job running......" and then check if the job is still running. If $status.CurrentRunStatus contains 'executing' then keep looping. If the job succeeds or fails then the loop will stop. I then write to the screen the status of the job completion, either it failed or succeeded.
$job = dir | ?{$_.name -like '*backup*' -and $_.isenabled -eq 'true'}
$backup =$job.start()
start-sleep -s 5
do
{
"backup job running......"
$status = dir
$status.refresh()
$status = dir | ?{$_.name -like '*backup*' -and $_.isenabled -eq 'true'}
start-sleep -s 2
} while($status.CurrentRunStatus -contains 'executing')
write-host "Backup Last run outcome ----> "$status.LastRunOutcome -BackgroundColor Blue -ForegroundColor Red
$response = read-host -Prompt "Did backup job for '$($instance)' successfully complete Y/N"
if ($response -eq 'Y'){ <do some action>}
else
{ write-host 'killing process troubleshoot backup failure' -ForegroundColor red -BackgroundColor blue }
Converting a STRING to an INT |
|
I work in a multi-platform environment, SQL Server, Postgres and Mongo. I often times will need to run some sort of scheduled maintenance for those DBs. Chron really sucks and I like to schedule this stuff via the SQL Agent. When hitting Linux servers, I use Posh SSH to do this. The output from the Invoke-SSHCommand is a STRING or ARRAY depending on the results, regardless of the type of data it is. So today I needed to convert the STRING results into an INT.
I use the trim() function to get rid of any whitespace just in case.
$b = $a.Output.trim() -as [INT]
Pretty straight ahead.
I use the trim() function to get rid of any whitespace just in case.
$b = $a.Output.trim() -as [INT]
Pretty straight ahead.