Lab10 setup - jacob-dinapoli/tech-journal GitHub Wiki

The easiest way to start powershell is to launch it from the start menu. You can also type powershell at the command prompt. The following shows starting powershell as a limited user and also from the command prompt. Try both. We will be using an unprivileged account on workstation 1 unless otherwise specified.

  • Step One: Path, shortcuts, command completion and history
    • Execute the following commands:
      • Write-Host $env:Path
      • cd c:\Users
      • pwd
      • cd ~
      • pwd
      • history
  • Step Two: Looping
    • The following code sequence shows assignment of the $env:Path string to the $mypath variable, followed by the conversion of that path to an array using the split operator. Once we have the array, we loop through it using the Foreach method. Give this a try.
      • $mypath = $env:Path
      • Write-Host $mypath
        • foreach($item in $mypath.split(';'))
        • {
        • Write-Host $item
        • }
  • Step Three: Aliasing and Get-ChildItem
    • The following screenshot further illustrates the object oriented nature of powershell. The legacy dir and ls commands really point to a Powershell "cmdlet" called Get-ChildItem. If the object contains other objects, it can be enumerated.
  • Step Four: Create your own Alias
    • Here's a common alias used in Windows by those who grew up with Linux. Note the error when the linux admin types in ifconfig. Setting the alias this way only lasts for the current session. If you want one to be persistent, you need to research powershell profiles.
    • Type the following commands:
      • Set-Alias -Name ifconfig -value ipconfig
      • ifconfig
  • Step Five: Creating a Script
    • Take the list of servers (servers.txt), and powershell file(servers.ps1), you may wish to have windows explorer show file extensions.You will notice right away when executing the script via ./servers.ps1 that there is an error. You have to configure windows to allow powershell scripting.
    • Write the following commands
      • mkdir scripting
      • cd scripting
      • notepad servers.txt
      • $servers=Get-Content .\servers.txt
      • foreach($server in $servers)
  • Step Six: Parameters
    • The script, as created, has a flaw in that the path and file name of the servers.txt file are "hard-coded", as opposed to being dynamically passed in as a parameter. Take a look at the following program, and then extend servers.ps1 to accept a file path as a parameter.