Skip to main content

Posts

Showing posts with the label PowerShell

Compare multiple variables Conditions with if else in PowerShell

Multiple conditions to evaluate is sometimes necessary in order to decide whether the script should continue to execute a specific command or do another process.

PowerShell is able to do this kind of task using if else statement.

Example script below just shows 3 variables that should match and it will output "OK", the output is just a write-host command but it could be replaced with other codes.

And if one of the statements does not match the "else" statement is executed.Example code: All statements or condition matches

$var1="Online"$var2="Running"$var3="Proceed"if (($var1-eq"Online") -and ($var2-eq"Running")-and ($var3-eq"Proceed")) {write-host"OK"} else {write-host"Not OK unable to Proceed"}

Python check if an item is on the list

How to check whether an item exist on the list?
If then else and using the "in" operator in Python is able to check whether an item is on the list or not.
Here's a code example, it's not a complete program but a demonstration how the snippet can be used, the code can be integrated into an existing complete code.
lst = list() #list declaration in Python      #if item is already on the list it will just continue and will not execute the else command            if yitem in lst: continue          else:           print(" Item 'yitem' is not present in the list, appending item to the list.")           lst.append(yitem)  #action that will be done if item was not found on the list
For completeness sake if something needs to be done if the item is found, then the code will be like this:
       if yitem in lst:             print "Item is found and is present on the list."          #do something more if required        else:           print(" Item 'yitem&…

PowerShell switch case call function

Code below collect user input and uses switch case statement to check if the input matches, and if it matches a function is called that will execute commands.Here’s the code:#get or read from user input$computer_name=read-host ("Enter Computer Name:")switch ($computer_name ){#if the input is computer_1 then function func_comuputer1 is called computer_1{func_computer1}computer_2{func_computer2}}#function called if computer_1 is the inputfunctionfunc_computer1 {write-host"You entered Computer_1"#or replaced with other function like reboot / shutdown /or other commands#Restart-Computer -ComputerName computer_1}
#function called if computer_2 is the inputfunctionfunc_computer2 {write-host"You entered Computer_2"#or replaced with other function like reboot / shutdown /or other commands#Ex: Enter-Pssession computer_2}

Code below will keep asking for input and will only exit if a specific string matches to exit the script. Here’s the code:do {$computer_name

How to upload to S3 using PowerShell

Uploading to S3 using PowerShell is quite easy.Here’s the code:$accesskey='123'
$secretkey='456'
$dregion='us-east-1'Write-S3Object-AccessKey$accesskey-SecretKey$secretkey-Region$dregion-BucketName-FileC:\Temp\test-file.txt#Replace NameofthebucketinS3 with the folder name found in S3#test-file.txt is the file that will be uploaded to S3 and is located on c:\Temp#Change the folder location c:\Temp and the file to be uploaded #Above code will upload the file to the root of the S3 Bucket#If need to upload a file to a specific subfolder in S3 bucket used the code below$accesskey='123'
$secretkey='456'
$dregion='us-east-1'Write-S3Object-AccessKey$accesskey-SecretKey$secretkey-Region$dregion-BucketName NameofthebucketinS3/SubFolderBucket-FileC:\Temp\test-file.txt