page to describe creating and working with simple script files
This commit is contained in:
parent
f3bac7faf0
commit
8e9100ae4a
61
docs/learning-powershell/create-powershell-scripts.md
Normal file
61
docs/learning-powershell/create-powershell-scripts.md
Normal file
|
@ -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
|
|
@ -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
|
||||
----
|
||||
|
|
Loading…
Reference in a new issue