Comments (4)
Hello Matthew,
I don't think it is easily feasible to build a comment removal function into PS2EXE. The effort to detect and the risk of misinterpretation of comments, multi-line comments, directives like #requires, help texts, here strings or embedded C# code and others would be far too great.
I searched the internet and found only one script that comes close to accomplishing this task. I suggest you do a cleanup before running PS2EXE.
By the way, how big are your scripts if you can save several hundreds KBytes by removing comments?
Greetings
Markus
from ps2exe.
A final linked script with all comments can take 2000-4500 lines. Resulting EXE can be larger 500KB. After cleaning EXE takes 300KB.
I use this customized cleaner (found in internet).
function Remove-Comments {
[CmdletBinding( DefaultParameterSetName = 'FilePath' )]
Param (
[Parameter(Position = 0, Mandatory, ParameterSetName = 'FilePath' )]
[ValidateNotNullOrEmpty()]
[String]$Path,
[Parameter(Position = 0, ValueFromPipeline, Mandatory, ParameterSetName = 'ScriptBlock' )]
[ValidateNotNullOrEmpty()]
[ScriptBlock]$ScriptBlock
)
if ($PSBoundParameters['Path']) {
$null = Get-ChildItem $Path -ErrorAction Stop
$ScriptBlockString = [IO.File]::ReadAllText((Resolve-Path $Path))
$ScriptBlock = [ScriptBlock]::Create($ScriptBlockString)
} else {
# Convert the scriptblock to a string so that it can be referenced with array notation
$ScriptBlockString = $ScriptBlock.ToString()
}
# Tokenize the scriptblock and return all tokens except for comments
$Tokens = @([System.Management.Automation.PSParser]::Tokenize($ScriptBlock, [Ref] $Null)).Where{ $_.Content -match '^#Requires' -or $_.Type -ne 'Comment' }
$StringBuilder = [Text.StringBuilder]::new()
# The majority of the remaining code comes from Lee Holmes' Show-ColorizedContent script.
$CurrentColumn = 1
$NewlineCount = 0
foreach ($CurrentToken in $Tokens) {
# Now output the token
if (($CurrentToken.Type -eq 'NewLine') -or ($CurrentToken.Type -eq 'LineContinuation')) {
$CurrentColumn = 1
# Only insert a single newline. Sequential newlines are ignored in order to save space.
if ($NewlineCount -eq 0) {
$null = $StringBuilder.AppendLine()
}
$NewlineCount++
} else {
$NewlineCount = 0
# Do any indenting
if ($CurrentColumn -lt $CurrentToken.StartColumn) {
# Insert a single space in between tokens on the same line. Extraneous whiltespace is ignored.
if ($CurrentColumn -ne 1) {
$null = $StringBuilder.Append(' ')
}
}
# See where the token ends
$CurrentTokenEnd = $CurrentToken.Start + $CurrentToken.Length - 1
# Handle the line numbering for multi-line strings
if (($CurrentToken.Type -eq 'String') -and ($CurrentToken.EndLine -gt $CurrentToken.StartLine)) {
$LineCounter = $CurrentToken.StartLine
$StringLines = $(-join $ScriptBlockString[$CurrentToken.Start..$CurrentTokenEnd] -split '`r`n')
foreach ($StringLine in $StringLines) {
$null = $StringBuilder.Append($StringLine)
$LineCounter++
}
}
# Write out a regular token
else {
$null = $StringBuilder.Append((-join $ScriptBlockString[$CurrentToken.Start..$CurrentTokenEnd]))
}
# Update our position in the column
$CurrentColumn = $CurrentToken.EndColumn
}
}
$StringBuilder.ToString()
} # END
from ps2exe.
Hello Matthew,
this script is what I found too. A great script.
I'm sorry to tell you that I won't be integrating it. There are several reasons for this (e.g. some virus scanners recognise it as malware, I don't want to provide support for other people's code that picks apart scripts), but the main reason is that the licence makes it impossible to adopt.
Greetings
Markus
from ps2exe.
Markus, no problem. Your current solution is great itself. Thanks for your work.
from ps2exe.
Related Issues (20)
- Powershell Core - 7 HOT 6
- The module may have listeners HOT 3
- Issue with HKCU registry in compiled script? HOT 1
- Declaring a parameter as [string] does not pass on value of parameter if [switch] params also declared. HOT 1
- Invoke-ps2exe : 无法将“Invoke-ps2exe”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路 径,请确保路径正确,然后再试一次。 HOT 2
- Add special variables to determine if a script is running in ps2exe or not HOT 2
- Continuous Integration: auto test scripts when PR and Push and auto close of stale issue?
- Console Background, font etc. HOT 7
- Socket detected as malware
- PS2EXE pathing issue after converting to exe HOT 6
- PS2EXE may be sucking up the compiled scripts HOT 4
- ps2exe - Not sure if it is a problem or limitation HOT 5
- More of a question. How to get the exe to output an exit code? HOT 2
- Doesn't Support Threading. HOT 2
- Windows 10 Patch from March 2024 breaks Powershell script HOT 5
- - HOT 1
- The necessary DLL cannot be loaded. HOT 2
- Executable Compiled with PS2EXE Fails to Trigger from Wazuh Active Response HOT 1
- Does not support powershell 5.X+ HOT 2
- How to add -help switch to a compiled script? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ps2exe.