PowerShell Scripting - InaFricke/SYS-255 GitHub Wiki

Logging in

you can log in through command prompt by entering powershell

Paths, shortcuts, command completion and history

The Powershell designers have created "aliases" so that commands like ls, pwd, cd ~, history will all work on windows. Powershell is an object-oriented scripting language. To access the path string from the environment object, note the first command in the illustration.

write-host $env:Path

cd ~ ; pwd

history

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.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_foreach?view=powershell-7.3&viewFallbackFrom=powershell-7.1

foreach a language command you can use to traverse all the items in a collection of items.

https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_split?view=powershell-7.3&viewFallbackFrom=powershell-7.1

split use the Split operator to split one or more strings into substrings.

https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-arrays?view=powershell-7.3&viewFallbackFrom=powershell-7.1

array An array is a data structure that serves as a collection of multiple items. You can iterate over the array or access individual items using an index. The array is created as a sequential chunk of memory where each value is stored right next to the other.

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.

grep alias

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.

.\file.txt .\file.ps1

The Set-Execution-Context command shown above allows current users to run local scripts and digitally signed remote scripts. This is analogous to chmod +x on Linux for all the user's Powershell scripts.

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.

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.

The following command shows how one can just launch a command remotely without having an interactive session.

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.

https://blog.netwrix.com/2018/06/07/how-to-create-new-active-directory-users-with-powershell/

https://learn.microsoft.com/en-us/powershell/module/activedirectory/new-adgroup?view=windowsserver2022-ps

https://stackoverflow.com/questions/5072996/how-to-get-all-groups-that-a-user-is-a-member-of

New-ADUser "Shawn S"

Get-ADUser "Shawn S"

New-ADGroup "Sleuths"

Get-ADGroup "Sleuths"

Add-ADGroupMember -Identity "Sleuths" -Members "Shawn S"

Get-ADGroupMember "Sleuths"

PS-Remoting Fail / Solution

  1. The following command needs to be run on both computers. On the workstation log into Powershell as an administrator.

https://stackoverflow.com/questions/42384177/allowing-winrm-in-the-windows-firewall