PowerSession-rs icon indicating copy to clipboard operation
PowerSession-rs copied to clipboard

Script feature

Open franklupo opened this issue 1 year ago • 2 comments

hi, you can use a script that runs the commands and they are recorded like https://github.com/zechris/asciinema-rec_script

best regadars

franklupo avatar Apr 04 '24 08:04 franklupo

This feature would be great for presenting basic context/comparisons !

plutonium-239 avatar Apr 29 '25 21:04 plutonium-239

Hi, To do this I developed this I created a powershell script that accepts 2 parameters file input and file output. The script runs PowerSession with a new modified script based on the input file and simulates typing.

there are 2 tags: #TP-START star typing #TP-END end typing

by default the typing is 20 milliseconds. if you want to change it use #TP-START,SM0 0 milliseconds #TP-START,SM10 10 milliseconds and so on.

The output file and the recording file

That's enough for me for now.

function Build-AsciinemaFromPs1() {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [string]$InputFile,

        [Parameter(Mandatory)]
        [string]$OutputFile
    )

    process {
        $lines = Get-Content -Path $InputFile
        $linesInTyping = @()
        $inTyping = $false
        $sleepMilliSec = 0

        $data = '
# Simulate typing line
function TypeingLine([int[]]$Line, [int] $SleepMilliSec = 20) {
    if ($Line.Length -gt 0) {
        $chars = [char[]]$Line
        $data = [String]::new([char[]]$Line) | bat --language=powershell --style=plain --decorations=always --color=always

        foreach ($item in [char[]]$data) {
            [console]::Write($item)
            if ($chars.contains($item)) {
                Start-Sleep -Milliseconds $SleepMilliSec
            }
        }
    }
    [console]::WriteLine()
}
'.Split([Environment]::NewLine)

        #loop all line
        for ($i = 0; $i -lt $lines.Length; $i++) {
            $line = $lines[$i]

            if ($line.StartsWith('#TP-START')) {
                $inTyping = $true
                $linesInTyping = @()

                #default
                $sleepMilliSec = 20

                foreach ($value in $line.Split(',')) {
                    if ($value.StartsWith('SM')) {
                        $sleepMilliSec = [int]$value.Substring(2)
                    }
                }
            }
            elseif ($line.StartsWith('#TP-END')) {
                $inTyping = $false

                #typing
                foreach ($item in $linesInTyping) {
                    $data += "TypeingLine -Line @($([int[]][char[]]$item -join ",")) -SleepMilliSec $sleepMilliSec"
                }

                #lines code
                $data += $linesInTyping
            }
            elseif ($inTyping) {
                $linesInTyping += $line
            }
            else {
                $data += $line
            }
        }

        $fileNewPs1 = "$(New-TemporaryFile).ps1"
        $data | Out-File -filepath $fileNewPs1  -Force
        write-host $fileNewPs1

        $tmpExe = "$(New-TemporaryFile)-PowerSession.exe"
        write-host $tmpExe
        curl -L https://github.com/Watfaq/PowerSession-rs/releases/download/v0.1.11/PowerSession.exe -o $tmpExe
        Invoke-Expression "$tmpExe rec $OutputFile -f -c 'pwsh.exe $fileNewPs1'"

        Remove-Item $tmpExe
        Remove-Item $fileNewPs1
    }
}

example

# SPDX-FileCopyrightText: Copyright Corsinvest Srl
# SPDX-License-Identifier: GPL-3.0-only

#TP-START,SM0
#    ______                _                      __
#   / ____/___  __________(_)___ _   _____  _____/ /_
#  / /   / __ \/ ___/ ___/ / __ \ | / / _ \/ ___/ __/
# / /___/ /_/ / /  (__  ) / / / / |/ /  __(__  ) /_
# \____/\____/_/  /____/_/_/ /_/|___/\___/____/\__/
#
# PowerShell for Proxmox VE         (Made in Italy)
#
# cv4pve-api-powershell is a part of suite cv4pve.
# For more information visit https://www.corsinvest.it/cv4pve

#TP-END

#TP-START
# Today we will see how to use PowerShell commands for Proxmox VE

# Github https://github.com/Corsinvest/cv4pve-api-powershell
# Documentation https://raw.githack.com/Corsinvest/cv4pve-api-powershell/master/doc/index.html

# Install module from gallery https://www.powershellgallery.com/packages/Corsinvest.ProxmoxVE.Api

Install-Module -Name Corsinvest.ProxmoxVE.Api -Force
#TP-END

#TP-START

# Connect to node/cluster using Credential or ApiToken
$ticket = Connect-PveCluster -HostsAndPorts 192.168.0.1:8006,192.168.0.1 -SkipCertificateCheck -ApiToken $ENV:PveApiToken
#TP-END

#TP-START

# The global ticker are saved in $Global:PveTicketLast
# For commands if the -PveTicket parameter is not specified, $Global:PveTicketLast will be used

$ret = Get-PveVersion

# $ret is a class of type PveResponse
#TP-END

Start-Sleep -Milliseconds 500

#TP-START
$ret
#TP-END

Start-Sleep -Milliseconds 500

#TP-START
$ret.ToData()
#TP-END

Start-Sleep -Milliseconds 500

#TP-START
$ret.ToTable()
#TP-END

Start-Sleep -Milliseconds 1000

#TP-START
# Get nodes of cluster
(Get-PveNodes).ToData() | Select-Object -Property type,node,uptime,status,maxcpu,maxdisk | Out-Default
#TP-END

Start-Sleep -Milliseconds 500

#TP-START
# Special command get vm from id or name
Get-PveVm -VmIdOrName 100
#TP-END

#TP-START
# Special command get vm from id or name
Get-PveVm -VmIdOrName 100
#TP-END

#TP-START
# Get snapshots from Vm 102
(Get-PveNodesQemuSnapshot -Node cc02 -Vmid 102).ToTable()
#TP-END

#TP-START
# Get snapshots from Vm 102
(Get-PveVm -VmIdOrName 102 | Get-PveNodesQemuSnapshot).ToTable()
#TP-END

#TP-START
# For build documentation use command Build-PveDocumentation
#TP-END

franklupo avatar Apr 30 '25 07:04 franklupo