tag:blogger.com,1999:blog-16856035768671526472019-05-21T11:22:14.834-05:00Learn Powershellj0rt3g4http://www.blogger.com/profile/06952382884240769576noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-1685603576867152647.post-13805530303328093482017-08-17T01:45:00.000-05:002017-08-17T01:45:53.284-05:00Basic Powershell Scripting<h2> Comments </h2> Single line use: # Example <pre class="brush:powershell"><br />#This is a single comment in powershell<br /></pre> Multiple lines use <# #><pre class="brush:powershell"><br /><#<br />This is a multiple lines comment.<br />It's used at the start of the script like the ones in the 80s<br />To put authors of script, version, contact information and help<br />#><br /></pre> <h2> Variables </h2>Native Variables: Just use the "$" symbol <pre class="brush:powershell"><br />$oneVar = "This is one var"<br /></pre>They are assigned in runtime depending of the values you provide. <pre class="brush:powershell"><br />$number2 = 2<br /></pre> 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. <div class="separator" style="clear: both; text-align: center;"><a href="https://3.bp.blogspot.com/-PmP6GNLnjts/WZU5hHwpJ1I/AAAAAAAAliE/IwO2GpHJ0j0-fFw93fx_46JIzy-tEuSbgCLcBGAs/s1600/s1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://3.bp.blogspot.com/-PmP6GNLnjts/WZU5hHwpJ1I/AAAAAAAAliE/IwO2GpHJ0j0-fFw93fx_46JIzy-tEuSbgCLcBGAs/s1600/s1.png" data-original-width="848" data-original-height="372" /></a></div> 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. <div class="separator" style="clear: both; text-align: center;"><a href="https://4.bp.blogspot.com/-J31UbQ3CGNw/WZU6IZam0sI/AAAAAAAAliM/njiJlfVrToskQESF0a7bQB_LFdvKrihkwCLcBGAs/s1600/b2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://4.bp.blogspot.com/-J31UbQ3CGNw/WZU6IZam0sI/AAAAAAAAliM/njiJlfVrToskQESF0a7bQB_LFdvKrihkwCLcBGAs/s1600/b2.png" data-original-width="510" data-original-height="140" /></a></div> 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: <pre class="brush:powershell"><br />[int]$number2 = 2<br />[Datetime] $dt = Get-Date<br /></pre> 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 <pre class="brush:powershell"><br />#Using a single line:<br />Write-Output "Hello Powershell's World"<br /><br /><br />#using several lines and defining variables<br />#implicitely<br />$hw = "Hello Powershell's World"<br />Write-Output $hw<br />write-Output "$hw"<br /><br />#explicitely<br />[string]$hwe = "Hello Powershell's world"<br />write-Output $hwe<br />write-Output "$hw"<br /></pre> What is the difference between variable inside "" and outside them ?. j0rt3g4http://www.blogger.com/profile/06952382884240769576noreply@blogger.com2tag:blogger.com,1999:blog-1685603576867152647.post-84481451430609512652017-08-17T01:19:00.000-05:002017-08-17T01:19:09.709-05:00Chaotic Numbers in Powershell Module<h1> Chaotic Numbers in Powershell Module </h1> All the details are on the Technet site: Source: <a href="https://gallery.technet.microsoft.com/scriptcenter/PowerShell-Module-for-a-3213d5a6">Own Technet Site</a> <h2>History</h2> 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. <div class="separator" style="clear: both; text-align: center;"><a href="https://gallery.technet.microsoft.com/scriptcenter/site/view/file/178053/1/LogisticMap_BifurcationDiagram2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://gallery.technet.microsoft.com/scriptcenter/site/view/file/178053/1/LogisticMap_BifurcationDiagram2.png" data-original-width="800" data-original-height="566" /></a></div> 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: <div class="separator" style="clear: both; text-align: center;"><a href="https://gallery.technet.microsoft.com/scriptcenter/site/view/file/178054/1/eq2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://gallery.technet.microsoft.com/scriptcenter/site/view/file/178054/1/eq2.jpg" data-original-width="747" data-original-height="157" /></a></div> 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. <h2>How to run a ps1 (works for any PS1 in the Galery):</h2> 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: <pre class="brush:powershell"><br />Set-ExecutionPolicy Unrestricted<br /></pre>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: <pre class="brush:powershell"><br />Set-ExecutionPolicy RemoteSigned<br /></pre> 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 <a href="http://chaos.j0rt3g4.com"></a>Here's a little video of how to use it: <a href="https://youtu.be/Zf7L2Vrix88">https://youtu.be/Zf7L2Vrix88</a>j0rt3g4http://www.blogger.com/profile/06952382884240769576noreply@blogger.com0tag:blogger.com,1999:blog-1685603576867152647.post-3301460694720285312017-08-14T19:47:00.000-05:002017-08-14T20:14:52.837-05:00How Do I find my IP Address in powershell? Just need to open a PowerShell console and write: <pre class="brush:powershell"><br />(Get-NetIPAddress | ?{ $_.AddressFamily -eq "IPv4" -and !($_.IPAddress -match "169") -and !($_.IPadd<br />ress -match "127") }).IPAddress<br /></pre> Of course, if you do it like this <pre class="brush:powershell"><br />Get-NetIPAddress | ?{ $_.AddressFamily -eq "IPv4" -and !($_.IPAddress -match "169") -and !($_.IPaddress -match "127") }<br /></pre>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. j0rt3g4http://www.blogger.com/profile/06952382884240769576noreply@blogger.com0tag:blogger.com,1999:blog-1685603576867152647.post-90787285176699252562017-08-14T19:44:00.000-05:002017-08-14T19:44:31.066-05:00Welcome to LearnPowershell<h3> Welcome to Powershell Scripting Blog </h3> <p class="text-justify">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. </p>j0rt3g4http://www.blogger.com/profile/06952382884240769576noreply@blogger.com0