Scripts

From CDZwiki
Jump to: navigation, search

A well maintained library of scripts can accomplish nearly anything. Below are some of my favorite scripts organized by language.

Microsoft PowerShell

Microsoft's PowerShell is a very underrated piece of technology that allows for full control of a modern Windows system strictly through command line. While not a programming language in itself, PowerShell is privileged & capable enough to stand in for many other languages on a Windows system.

Remote Connectivity

PSSession.ps1 This script is a quick way to establish a PS session with encrypted credentials (based on [null]). This configuration allows you to call on the script with the first argument your target, and the second argument a path to another script to execute on the remote system.

$psusr = "domain\user"
$pspwd = Get-Content $lib\cred\ps.txt | ConvertTo-SecureString
$cred = New-Object -typename System.Management.Automation.PSCredential -argumentlist $psusr, $pspwd
$psses = New-PSSession -Computername $args[0] -Credential $cred: $psses
Invoke-Command -Session $psses -FilePath $args[1]
Remove-PSSession -Session $psses

Plink.ps1 If you've ever had to reach out over SSH to run a script you've probably used Plink before. The following allows you to integrate Plink via PowerShell script with the first argument your username, second argument your target host and last the command you wish to run.

$p = "C:\Program Files\PuTTY\plink.exe"
$u = $args[0]
$h = $args[1]
$c = $args[2]
& $p $u@$h $c

File Management

VHDMount.ps1 Sometimes you need to interact with VHDs via script - PowerShell will let you do that pretty easily. This configuration allows you to call on the script with the first argument "M" for Mount or "D" for dismount, the second argument your VHD source/target.

$vhd = $args[1] # change to static path to skip input
$c = $args[0] # argument may be either "M" for "Mount" or "D" for "Dismount"
$v = Test-Path variable:global:vhd
If ($c -eq "D"){
    echo "Right."
    echo "Dismount!"
    Dismount-DiskImage -Imagepath $vhd
    exit
}else{
    If ($c -eq "M"){
        #false for testing
        If ($v -eq $true){
            echo "Path is good!"
            If ($vhd -match ".vhdx"){
                echo "File is good!"
            }else{
                echo "Path OK but File not found!"
                Remove-Variable $vhd -ErrorAction SilentlyContinue
                exit
            }
        }else{
            While ($cv -eq $false){
                echo "Path not found." 
                $vhd = Read-Host -prompt "Please specify VHDX path (path & file MUST exist)"
                $cv = Test-Path $vhd
                If ($cv -eq $false){
                    Remove-Variable $vhd -ErrorAction SilentlyContinue
                    exit
                }else{
                    echo "Path is good!"
                    If ($vhd -match ".vhdx"){
                        echo "File is good!"
                    }else{
                        echo "Path OK but File not found!"
                        Remove-Variable $vhd -ErrorAction SilentlyContinue
                        exit
                    }
                }
            }
        }
        echo "Mounting . . ."
        $b = (Get-Volume).DriveLetter
        $r = Mount-DiskImage -Imagepath $vhd -passthru
        $p = (compare $b (Get-Volume).DriveLetter -PassThru)+":\"
        echo "Everything checks out! VHDX mounted as $p"
        exit
    }
}

Robocopy.ps1 Tired of remembering your Robocopy syntax all the time? Wish you could easily use PowerShell to script in Robocopy events? The following script does just that, with the first argument your source and second your destination.

$src = $args[0]
$dst = $args[1]
$opt = "/e /zb /copyall /purge /mt /r:1 /w:1 /xd `$Recycle.bin `"System Volume Information`""
Start-Process robocopy.exe -ArgumentList "$src $dst $opt"
exit

Microsoft Batch

Batch is a trusty utility useful for a variety of simple tasks on a Windows machine. Though a bit dated, .bat files are still heavily relied on in the Microsoft world.

Bash Script

The Go-To shell for Linux. Anything you've ever wanted to accomplish in Linux can be done with Bash Script; entire appliances have been written with just Bash!

PHP

PHP is just as underrated as PowerShell, or at least as shunned! For whatever reason, I've encountered a lot of grief over PHP, however this language is perfectly capable and runs the majority of the web these days. People only dislike PHP because they don't understand it.