Thursday, August 17, 2017

Basic Powershell Scripting

Comments

Single line use: # Example
#This is a single comment in powershell
Multiple lines use <# #>
<#
This is a multiple lines comment.
It's used at the start of the script like the ones in the 80s
To put authors of script, version, contact information and help
#>

Variables

Native Variables: Just use the "$" symbol
$oneVar = "This is one var"
They are assigned in runtime depending of the values you provide.
$number2 = 2
If you wanna know what is the type of the variable you can use "A cmdlet" (commandlet), which is basically a "verb-word" phase, the most useful verbs are "get","set","disable" and the "word" are other items. The 1st cmdlet that I'm going to show you is the "Get-Member", PowerShell is an object oriented language, so, all it's threaded as an object.
This image shows "System.Int32" for the variable $a (similar to the previous definition of our "$number2 variable". This means that it's a .NET variable as well. Let's see what happend when we simulated the example of the "onvvar" variable.
It's a system.string also a .net object. This way to declare and initialize variables is called implicit. But we can also declare them explicitly, like this:
[int]$number2 = 2
[Datetime] $dt = Get-Date
In here we see our second cmdlet called "Get-Date" that retrieves the date of the system that you're currently working in. So in this 1st lesson let's end this little tutorial with the Hello Powershell
#Using a single line:
Write-Output "Hello Powershell's World"


#using several lines and defining variables
#implicitely
$hw = "Hello Powershell's World"
Write-Output $hw
write-Output "$hw"

#explicitely
[string]$hwe = "Hello Powershell's world"
write-Output $hwe
write-Output "$hw"
What is the difference between variable inside "" and outside them ?.

Chaotic Numbers in Powershell Module

Chaotic Numbers in Powershell Module

All the details are on the Technet site: Source: Own Technet Site

History

Usually, all the random numbers that we got from a computer are called pseudo-random because they comply all statistics and rules to be called "random." Now, in other hands, we have an equation exposed in a Seminal paper by the biologist Robert May back to 1976, called "Logistic Map" is nothing but a quadratic equation that with the correct parameters can lead to a chaotics behaviors and from there we got the "True Random Generator number." Let's let the image talk.
This graph is what in physics related to the chaos we called, Bifurcation diagram. A bifurcation is when the dynamics of the graph that start in single line divides into 2, then into 4, then 8, and then do it every time more frequent until we got an undetermined number of bifurcations when the parameter r is 4. Ohh, the equation: please keep it in mind is this one:
This is the simplest equation that behaves chaotically if you see it good, it's just a quadratic equation. where the Xn+1 refers to the value in the next iteration, and x0 is the value from the previous iteration. I used the "pseudo-random" generator, just to pick a random number in x0 (to kick off the equation values for x1,x2,x3,...Xn). This equation to run you need to set a number of iterations and a number of transients, the first element is related a how many times will execute the iterations, and the second one is related to how many values it will discard before taking values, this basically is because even in chaos you need to wait for some stabilization of the "system", in our case the logistic equation works to study some dynamic systems. So The defaults values of my script will be: Iterations = 5000 Transient = 2000 (Note that it follows the equation "Iterations = transient + 3000" And this must be true always, it's not required for the system, this condition I put it arbitrarily to make sure you are in the chaos.) Numbers = 1 Numbers is related to the number of random numbers that you want. If you use any negative value on those parameters it will bring the Absolute value of the number to bring it to something meaningful.

How to run a ps1 (works for any PS1 in the Galery):

The first thing you need to know is that scripts can be signed with an external Certificate to run without issues, but I don't use to sign any of my scripts, it's for this reason that you need to modify the computer execution policy of scripts on your testing computer by running in an elevated PowerShell console:
Set-ExecutionPolicy Unrestricted
And pressing "A" or "R" to accept the change. Note for the end of the testing: Change the policy by running the following command prior to close your PowerShell windows:
Set-ExecutionPolicy RemoteSigned
I have uploaded a file with the extension ".dll ". If you wanna know about the C# code behind to this please visit the web page https://chaos.j0rt3g4.com/, navigate to the bitbucket URL at the end of the web and download it or clone you'll get the full Visual Studio 2017 Project, you would need VS2017 community (free) and dot net in the latest version 4.7 For details about downloading the script please go to the source. If you want to know about the C# code behind scenes, go to the URL Here's a little video of how to use it: https://youtu.be/Zf7L2Vrix88

Monday, August 14, 2017

How Do I find my IP Address in powershell?

Just need to open a PowerShell console and write:
(Get-NetIPAddress | ?{ $_.AddressFamily -eq "IPv4"  -and !($_.IPAddress -match "169") -and !($_.IPadd
ress -match "127") }).IPAddress
Of course, if you do it like this
Get-NetIPAddress | ?{ $_.AddressFamily -eq "IPv4"  -and !($_.IPAddress -match "169") -and !($_.IPaddress -match "127") }
You would get an object that you can pipe with others cmdlet like ConvertTo-CSV /json/ etc. If you have doubts or question don't hesitate in asking or leaving your comment.

Welcome to LearnPowershell

Welcome to Powershell Scripting Blog

In here I'll be posting solutions to some issues I have encounter daily. So I will invite you to follow the reading and enjoy the blog. I receive notifications so if you have any answer or just wanna hire me for an automation I'll be an email or message off and thank you very much for reading.