Skip to content

Instantly share code, notes, and snippets.

@Jaykul
Last active June 25, 2022 22:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jaykul/13891cd6930bd3a4d9ea4f8e01376240 to your computer and use it in GitHub Desktop.
Save Jaykul/13891cd6930bd3a4d9ea4f8e01376240 to your computer and use it in GitHub Desktop.
An Invoke Wrapper For Labs
ComputerName Email UserName Password
NotOne user@gmail.com bob s3cr3t
NotTwo user2@gmail.com sue m04rs3cr3t
<#
.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,user@gmail.com,s3cr3t
NotTwo,user2@gmail.com,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) {
@{ }
}
}
}
}
<#
.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,user@gmail.com,bob,s3cr3t
NotTwo,user2@gmail.com,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
Get-ExecutionPolicy
"Restarting VLC from $Pid"
WowUser WowPassword GameMode
Hacker1 h4ck3r1 SinglePlayer
Hacker2 h4ck3r2 Multiplayer
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