Powershell Scripting - kerryallen89/SYS-255-01 GitHub Wiki
Powershell Scripting
Lab Setup
Prerequisites
First, make sure you're using your AD Server, File Server (FS01), and your wks02 server. Also, you have to be logged into the user "alice" for this assignment as shown below.
Path, shortcuts, command completion, and history
This screenshot shows me performing commands within PowerShell such as "Write-Host $env:Path" and various other commands such as viewing what is inside the users file and the history of commands within alice.
Looping
This code sequence shows the assignments of the $env:Path string to the $mypath variable. This is then followed by the conversion of that path to an array using a split operator. Once there is an array displayed, you can loop through it using the Foreach method. Which allows you to describe a language command that can traverse all the items in a collection of items.
Aliasign and Get-ChildItem
This screenshot shows the object-oriented nature of Powershell. The legacy dir and ls commands point to a Powershell "cmdlet" called "Get-ChildItem" If the object contains other objects, it can be enumerated.
Create your own Alias
Here I created an Alias of another command that is commonly used in Linux systems which is "ifconfig". This replaces the command "ipconfig".
Next, I am going to create a new Alias for another command which includes the command "tracert" and "traceroute". I did this the same way I made ipconfig into ifconfig.
Deliverable 1. Create and demonstrate your own alias other than ifconfig. Provide a screenshot of the alias creation syntax and execution.
Creating a script
After typing in the command "mkdir scripting ; cd scripting ; notepad servers.txt", it opened up a notepad for a file called "servers.txt". I also created a file for a script that responds using the txt file called "servers.ps1". Below are the contents of both files and the commands I used to create both of them.
The ps1 file is used later in this part of the lab, it's better to create it now just to have it in advance. I had trouble trying to do that part as the ps1 file didn't exist when I needed it. Also, look at what file you are in when doing these commands, The amount of times I had to check to see I wasn't in "C:\users\alice\scripting\scripting\scripting" was so bad.
This screenshot shows Powershell getting the content of server.txt:
This shows me accessing the contents of "servers.ps1" and utilizing the Set-Execution-Context command. This allows current users to run local scripts and digitally signed remote scripts.
Deliverable 2. Extend this script to ping each server in the list one time. Provide a screenshot showing the syntax and output of your script.
This screenshot shows the scripting working by pinging the list of websites inside of servers.txt. Beside the Powershell terminal is a screenshot of the script itself. I added the line "ping -n 1 $server".
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.
Consider the following set of DNS Resolution Commands:
Deliverable 3. Write a script that takes two parameters that include the DNS response Type and a file with a list of hosts. Your output should look similar to the following. You may need to conduct some research to see how to pass more than one parameter. Provide a screenshot of your syntax and execution output.
$listofthings and $type are the parameters being passed in the script.
Remote Powershell
Move over to your AD Server (Windows Server) and open up a Powershell prompt. Though Windows does not natively support SSH for remote access, Powershell can be invoked remotely using PSSession. Refer to the following screenshot.
My attempt at this:
Deliverable 4. Provide a screenshot that shows a remote PS-Session and the command of your choice on FS01.
Deliverable 5. The following command shows how one can just launch a command remotely without having an interactive session. Explore the -ScriptBlock Option and provide a screenshot showing your own command launched on FS01.
EXAMPLE
My attempt at this:
Deliverable 6. Using Powershell on AD, figure out how to add a single user to Active Directory, and then how to add that user to a domain group that you create. Provide a screenshot that shows the command syntax and execution w/ response.
You need to run Powershell as Administrator for this step
New-ADUser -SamAccountName "TotallyNotKerry" -Name "Not Kerry"
Arguments for this command:
-SamAccountName
- Specifies the User Logon Name
-Name
- Specifies the full name of the user
New-ADGroup -Name "CoolGroup"
Arguments for this command
-Name
- Specifies the CN (common name) of the group
-GroupScope
- Specifies the scope of the group
Add-ADGroupMember -Identity "CoolGroup" -Members "TotallyNotKerry"
Arguments for this command:
-Identity
- Specifies the group to which you want to add members to
-Members
- Specifies the users (or groups if desired) you want to add to the group. If you want to add multiple members, you can do that by separating them with commas in the command line.
Get-ADGroupMember -Identity "CoolGroup"
This command is a way to verify the group membership of the user. Included in the screenshot is that Not Kerry is in the group.
Remove-ADUser -Identity "TotallyNotKerry" -Confirm:$false
Remove-ADGroup -Identity "CoolGroup" -Confirm:$false
FINAL SCREENSHOT FOR DELIVERABLE 6
I did this just so I could submit what I did entirely into my submission document
Deliverable 7: If you try the PS-Remoting commands against your workstation, it may fail due to firewall and other issues. Research the problem and see if you can resolve them. Take a screenshot of remote command invocation from AD to your workstation.
- To enable remote Powershell capabilities on wks02-kerry, I first had to run the command
Enable-PSRemoting -Forcein Powershell Administrator on wks02-kerry.
I originally thought you had to run on ad02-kerry, but it took me 15 minutes of searching on StackOverflow and Reddit to figure out it should be enabled on where you want to remote to.
I did run into a problem trying to connect to the remote session between WKS02-KERRY and AD02-KERRY. Despite enabling the WinRM firewall exception, it still doesn't work. I tried using and not using administrative PowerShell.
screenshot of it not working via hostname and below will be a screenshot of it not working via IP address
Im gonna see if disabling the firewalls on both AD02-KERRY and WKS02-KERRY will work and to check the connectivity of both.
I found out that my ad02-kerry DNS manager had my WKS02-kerry IP addressed logged as "10.0.5.100" and not "10.0.5.150". I fixed it and I eventually was able to ping my wks02-kerry from ad02-kerry.
After fixing that and trying to connect to my FS01-KERRY again to see what went wrong, this finally happened.
I WAS ABLE TO GET IT TO WORK!!!!!