From 8e9100ae4a815eda6b26bf60bb90a4f8219b590e Mon Sep 17 00:00:00 2001 From: Michael Greene Date: Fri, 29 Jul 2016 13:04:33 -0500 Subject: [PATCH] page to describe creating and working with simple script files --- .../create-powershell-scripts.md | 61 +++++++++++++++++++ .../powershell-beginners-guide.md | 8 --- 2 files changed, 61 insertions(+), 8 deletions(-) create mode 100644 docs/learning-powershell/create-powershell-scripts.md diff --git a/docs/learning-powershell/create-powershell-scripts.md b/docs/learning-powershell/create-powershell-scripts.md new file mode 100644 index 0000000000..e89fa1b59b --- /dev/null +++ b/docs/learning-powershell/create-powershell-scripts.md @@ -0,0 +1,61 @@ +How to Create and Run PowerShell scripts +==== + +You can combine a series of commands in a text file and save it with the file extension '.ps1', and the file will become a PowerShell script. +This would begin by opening your favorite text editor and pasting in the following example. + +``` PowerShell +# Script to return current IPv4 addresses on a Linux or MacOS host +$ipInfo = ifconfig | select-string 'inet' +$ipInfo = [regex]::matches($ipInfo,"addr:\b(?:\d{1,3}\.){3}\d{1,3}\b") | ForEach-Object value +foreach ($ip in $ipInfo) { + $ip.replace('addr:','') +} +``` + +Then save the file to something memorable, such as .\NetIP.ps1. +In the future when you need to get the IP addresses for the node, you can simplify this task by executing the script. + +``` PowerShell +PS>.\NetIP.ps1 +10.0.0.1 +127.0.0.1 +``` +You can accomplish this same task on Windows. + +```PowerShell +# One line script to return current IPv4 addresses on a Windows host +Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | ForEach-Object IPAddress +``` +As before, save the file as .\NetIP.ps1 and execute within a PowerShell environment. +Note: If you are using Windows, make sure you set the PowerShell's execution policy to "RemoteSigned" in this case. +See [Running PowerShell Scripts Is as Easy as 1-2-3](run-ps) for more details. + +```PowerShell +PS C:\NetIP.ps1 +127.0.0.1 +10.0.0.1 +``` + +Creating a script that can accomplish the same task on multiple operating systems +---- + +If you would like to author one script that will return the IP address across Linux, MacOS, or Windows, you could accomplish this using an IF statement. + +```PowerShell +# Script to return current IPv4 addresses for Linux, MacOS, or Windows +$IP = if ($IsLinux -or $IsOSX) { + $ipInfo = ifconfig | select-string 'inet' + $ipInfo = [regex]::matches($ipInfo,"addr:\b(?:\d{1,3}\.){3}\d{1,3}\b") | ForEach-Object value + foreach ($ip in $ipInfo) { + $ip.replace('addr:','') + } +} +else { + Get-NetIPAddress | Where-Object {$_.AddressFamily -eq 'IPv4'} | ForEach-Object IPAddress +} + +# Remove loopback address from output regardless of platform +$IP | Where-Object {$_ -ne '127.0.0.1'} +``` +[run-ps]:http://windowsitpro.com/powershell/running-powershell-scripts-easy-1-2-3 \ No newline at end of file diff --git a/docs/learning-powershell/powershell-beginners-guide.md b/docs/learning-powershell/powershell-beginners-guide.md index a4046fbce4..c253d185d6 100644 --- a/docs/learning-powershell/powershell-beginners-guide.md +++ b/docs/learning-powershell/powershell-beginners-guide.md @@ -259,15 +259,7 @@ Mode LastWriteTime Length Name -a---- 5/16/2016 1:15 PM 32972 test.log ``` -How to Create and Run PowerShell scripts ----- -- You can use ISE, Visual Studio Code or your favorite editor to create a PowerShell script and save the script with a .ps1 file extension (for example, helloworld.ps1) -- To run the script, cd to your current folder and type ./yourscript.ps1 (for example, ./helloworld.ps1). -Note: If you are using Windows, make sure you set the PowerShell's execution policy to "RemoteSigned" in this case. -See [Running PowerShell Scripts Is as Easy as 1-2-3] [run-ps] for more details. - -[run-ps]:http://windowsitpro.com/powershell/running-powershell-scripts-easy-1-2-3 Recommended Training and Reading ----