Last active
June 25, 2022 22:48
-
-
Save Jaykul/13891cd6930bd3a4d9ea4f8e01376240 to your computer and use it in GitHub Desktop.
An Invoke Wrapper For Labs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ComputerName | UserName | Password | ||
---|---|---|---|---|
NotOne | [email protected] | bob | s3cr3t | |
NotTwo | [email protected] | sue | m04rs3cr3t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Run a script against one or more computers from a list, by index. | |
.DESCRIPTION | |
Run a script by name agains one or more computers from a list, by index. | |
Supports using a ScriptName.csv for any ScriptName.ps1 to provide parameters per computer | |
.EXAMPLE | |
.\invoke.ps1 vlc | |
Runs the script vlc.ps1, prompting for a computer index. | |
.EXAMPLE | |
.\invoke.ps1 wow 1,2,3 | |
Runs the script wow.ps1 on computers 1, 2, and 3. | |
This would parse the Wow.csv file for parameters. | |
.NOTES | |
Assumes a Computers.csv file exists in the same directory as the script: | |
@" | |
ComputerName,Email,Password | |
NotOne,[email protected],s3cr3t | |
NotTwo,[email protected],m04rs3cr3t | |
"@ > Computers.csv | |
Supports a Wow.csv for a Wow.ps1 like: | |
@" | |
WowUser,WowPassword,GameMode | |
Hacker1,h4ck3r1,SinglePlayer | |
Hacker2,h4ck3r2,Multiplayer | |
"@ > Wow.csv | |
@" | |
param($WowUser,$WowPassword,$GameMode) | |
... | |
"@ > Wow.ps1 | |
In order to be able to run commands, the servers need PSRemoting enabled: | |
Enable-PSRemoting -force | |
# Set the service to start automatically | |
Set-Service WinRM -StartMode Automatic | |
# Verify start mode and state - it should be running | |
Get-WmiObject -Class win32_service | Where-Object {$_.name -like "WinRM"} | |
# For it to work without domain joined computers... | |
# Trust all hosts (or specify one to trust) | |
Set-Item WSMan:localhost\client\trustedhosts -value * | |
# Verify trusted hosts configuration | |
Get-Item WSMan:\localhost\Client\TrustedHosts | |
# You need to configure the firewall, or ... | |
# Just turn it off | |
netsh advfirewall set allprofiles state off | |
#> | |
[CmdletBinding()] | |
param( | |
# The base name of the script (without an extension) to run. | |
# Scripts mmust be in the folder with this script. | |
[Parameter(Mandatory)] | |
[ValidateScript({ Test-Path "$PSScriptRoot\${_}.ps1" })] | |
[ArgumentCompleter({ (Get-ChildItem $PSScriptRoot -Filter *.ps1).BaseName })] | |
[string]$Script, | |
# The seat number(s) for the computers to run commands against. | |
# Just hit enter to run on all of them | |
[Parameter(Mandatory)] | |
[AllowEmptyCollection()] | |
[int[]]$ComputerIndex | |
) | |
end { | |
if (!$ComputerIndex) { | |
# NOTE: it's critical that "100" is higher than the maximum number of lines in the Computers.csv file | |
$ComputerIndex = 1..100 | |
} | |
$Computers = @(Import-ComputerList -ComputerIndex $ComputerIndex) | |
$ArgumentList = @(Import-ArgumentList $Script $ComputerIndex) | |
for ($c = 0; $c -lt $Computers.Count; $c++) { | |
# Join the Computers parameters + the possible Arguments | |
$Parameters = $Computers[$c] + $ArgumentList[$c] | |
Write-Host Invoke-Command -File "${PSScriptRoot}\${Script}.ps1" @Parameters | |
# Invoke-Command -File "${PSScriptRoot}\${Script}.ps1" @Parameters | |
} | |
} | |
begin { | |
function Import-ComputerList { | |
param( | |
[int[]]$ComputerIndex = 1..100 | |
) | |
foreach ($computer in @(@($null) + @(Import-Csv Computers.csv))[$ComputerIndex]) { | |
[ordered]@{ | |
ComputerName = $computer.ComputerName | |
Credential = [PSCredential]::new($computer.Username, (ConvertTo-SecureString $computer.Password -AsPlainText -Force)) | |
} | |
} | |
} | |
function Import-ArgumentList { | |
param( | |
[string]$Script, | |
[int[]]$ComputerIndex = 1..100 | |
) | |
# $null is because arrays start at 0, but computers start at 1 | |
if (Test-Path "${Script}.csv") { | |
@(Get-Content "${Script}.csv")[$ComputerIndex] | ForEach-Object { | |
@{ | |
ArgumentList = $_ -split ',' | |
} | |
} | |
} else { | |
foreach ($i in $ComputerIndex) { | |
@{ } | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Run a script against one or more computers from a list, by index. | |
.EXAMPLE | |
.\invoke.ps1 vlc | |
Runs the script vlc.ps1, prompting for a computer index. | |
.EXAMPLE | |
.\invoke.ps1 vlc 1,2,3 | |
Runs the script vlc.ps1 on computers 1, 2, and 3. | |
.NOTES | |
Assumes a Computers.csv file exists in the same directory as the script: | |
@" | |
ComputerName,Email,Username,Password | |
NotOne,[email protected],bob,s3cr3t | |
NotTwo,[email protected],sue,m04rs3cr3t | |
"@ > Computers.csv | |
#> | |
[CmdletBinding()] | |
param( | |
# The base name of the script (without an extension) to run. | |
# Scripts mmust be in the folder with this script. | |
[Parameter(Mandatory)] | |
[ValidateScript({ Test-Path "$PSScriptRoot\${_}.ps1" })] | |
[ArgumentCompleter({ (Get-ChildItem $PSScriptRoot -Filter *.ps1).BaseName })] | |
[string]$Script, | |
# The seat number(s) for the computers to run commands against. | |
# Just hit enter to run on all of them | |
[Parameter(Mandatory)] | |
[AllowEmptyCollection()] | |
[int[]]$ComputerIndex | |
) | |
function Import-ComputerList { | |
param( | |
[int[]]$ComputerIndex = 1..100 | |
) | |
foreach ($computer in @(@($null) + @(Import-Csv Computers.csv))[$ComputerIndex]) { | |
@{ | |
ComputerName = $computer.ComputerName | |
Credential = [PSCredential]::new($computer.Username, (ConvertTo-SecureString $computer.Password -AsPlainText -Force)) | |
} | |
} | |
} | |
if (!$ComputerIndex) { | |
# NOTE: it's critical that "100" is higher than the maximum number of lines in the Computers.csv file | |
$ComputerIndex = 1..100 | |
} | |
foreach ($Computer in Import-ComputerList -ComputerIndex $ComputerIndex) { | |
# Write-Host Invoke-Command @Computer -File "${PSScriptRoot}\${Script}.ps1" | |
Invoke-Command @Computer -File "${PSScriptRoot}\${Script}.ps1" | |
} | |
# Invoke-Command -computername localhost {notepad.exe} | |
#psexec \\computername -u username -p password powershell.exe | |
#powershell.exe -executionpolicy unrestricted -command xcopy 'settings' '\\usersettings" /e /i /y | |
<# | |
# Become Admin | |
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { | |
Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs | |
exit | |
} | |
#> | |
# What is the Current Dir that this script is in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get-ExecutionPolicy | |
"Restarting VLC from $Pid" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WowUser | WowPassword | GameMode | |
---|---|---|---|
Hacker1 | h4ck3r1 | SinglePlayer | |
Hacker2 | h4ck3r2 | Multiplayer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($WowUser, $WowPassword, $GameMode) | |
Write-Host "Connecting to World of Warcraft... as user $WowUser for game mode $GameMode" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment