$sImageMagickHome = "C:\ImageMagick"
$sImageMagickConv = "$sImageMagickHome\convert.exe"
$sImageMagickArgs = #( '--%',
'-background transparent',
'-fill hsb(0,0,0)',
'-font Arial',
'-pointsize 18',
'-size 18x26',
'-gravity center')
for ( $i = 0x01; $i -le 0x05; $i++ )
{
$y = [char]$i
& $sImageMagickConv $sImageMagickArgs label:$y $sCharsDir\$y.png
#Write-Host $sImageMagickConv $sImageMagickArgs label:$y $sCharsDir\$y.png
}
Using Write-Host I can get an example to copy paste into the command line and I find it does run correctly if I run this single line from the PowerShell prompt:
C:\ImageMagick\convert.exe --% -background transparent -fill hsb(0,0,0) -font Arial -pointsize 18 -size 18x26 -gravity center label:☺ C:\Users\erics_000\Desktop\Output\Chars\☺.png
Using the call operator '&' from inside the script does not work at all however and leads to some error messages:
convert.exe: UnableToOpenBlob `--%': No such file or directory # error/blob.c/OpenBlob/2697.
convert.exe: NoDecodeDelegateForThisImageFormat `' # error/constitute.c/ReadImage/501.
convert.exe: UnrecognizedOption `-background transparent' # error/convert.c/ConvertImageCommand/858.
The article I have been reading is:
http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
Thank you...
The following script works for me:
$sImageMagickHome = "C:\dev\im"
$sImageMagickConv = "$sImageMagickHome\convert.exe"
$sImageMagickArgs = #('-background', 'transparent',
'-fill', 'hsb(0,0,0)',
'-font', 'Arial',
'-pointsize', '18',
'-size', '18x26',
'-gravity', 'center')
for ( $i = 65; $i -le 67; $i++ )
{
$y = [char]$i
& $sImageMagickConv $sImageMagickArgs label:$y c:\dev\$y.bmp
}
Note that you cannot just Write-Host the arguments and try running it from command line, Powershell does special processing for & operator (adds quotes where needed), which it does not when you pass same arguments to Write-Host.
You'd probably want to install PSCX and play around with echoargs utility bundled with it to gain better understanding of how arguments are passed.
Related
In a PowerShell script that I'm creating, I want to output specific commands with the parameter values being passed. The output could go to a log file and/or the console output. The following will output what I want to the console but I have to duplicate the line of script of interest and at some point a subtle mistake will be made where the commands don't match. I've tried Set-PSDebug and Trace-Command and neither give the result I seek. I've thought about putting the line of script into a string, writing it out, and then calling Invoke-Expression but I'll give up autocompletion/intellisense.
Example with duplicated line for writing and execution:
Write-Output "New-AzureRmResourceGroup -Name $rgFullName -Location $location -Tag $tags -Force"
New-AzureRmResourceGroup -Name $rgFullName -Location $location -Tag $tags -Force
Output result with expanded variables. $tags didn't expand to actual hashtable values:
New-AzureRmResourceGroup -Name StorageAccounts -Location West US -Tag System.Collections.Hashtable -Force
What other options or commandlets can I use to achieve the tracing without writing duplicated code and maybe even expanding the hashtable?
There is no built-in feature I'm aware of that echoes a version of a command being executed with variables and expressions used in arguments expanded.
Even if there were, it would only work faithfully in simple cases, because not all objects have literal representations.
However, with limitations, you can roll your own solution, based on &, the call operator, and parameter splatting via a hashtable of argument values defined up front:
# Sample argument values.
$rgFullName = 'full name'
$location = 'loc'
$tags = #{ one = 1; two = 2; three = 3 }
# Define the command to execute:
# * as a string variable that contains the command name / path
# * as a hashtable that defines the arguments to pass via
# splatting (see below.)
$command = 'New-AzureRmResourceGroup'
$commandArgs = [ordered] #{
Name = $rgFullName
Location = $location
Tag = $tags
Force = $True
}
# Echo the command to be executed.
$command, $commandArgs
# Execute the command, using & and splatting (note the '#' instead of '$')
& $command #commandArgs
The above echoes the following (excluding any output from the actual execution):
New-AzureRmResourceGroup
Name Value
---- -----
Name full name
Location loc
Tag {two, three, one}
Force True
As you can see:
PowerShell's default output formatting results in a multi-line representation of the hashtable used for splatting.
The $tags entry, a hashtable itself, is unfortunately only represented by its keys - the values are missing.
However, you can customize the output programmatically to create a single-line representation that approximates the command with expanded arguments, including showing hashtables with their values, using helper function convertTo-PseudoCommandLine:
# Helper function that converts a command name and its arguments specified
# via a hashtable or array into a pseudo-command line string that
# *approximates* the command using literal values.
# Main use is for logging, to reflect commands with their expanded arguments.
function convertTo-PseudoCommandLine ($commandName, $commandArgs) {
# Helper script block that transforms a single parameter-name/value pair
# into part of a command line.
$sbToCmdLineArg = { param($paramName, $arg)
$argTransformed = ''; $sep = ' '
if ($arg -is [Collections.IDictionary]) { # hashtable
$argTransformed = '#{{{0}}}' -f ($(foreach ($key in $arg.Keys) { '{0}={1}' -f (& $sbToCmdLineArg '' $key), (& $sbToCmdLineArg '' $arg[$key]) }) -join ';')
} elseif ($arg -is [Collections.ICollection]) { # array / collection
$argTransformed = $(foreach ($el in $arg) { & $sbToCmdLineArg $el }) -join ','
}
elseif ($arg -is [bool]) { # assume it is a switch
$argTransformed = ('$False', '$True')[$arg]
$sep = ':' # passing an argument to a switch requires -switch:<val> format
} elseif ($arg -match '^[$#(]|\s|"') {
$argTransformed = "'{0}'" -f ($arg -replace "'", "''") # single-quote and escape embedded single quotes
} else {
$argTransformed = "$arg" # stringify as is - no quoting needed
}
if ($paramName) { # a parameter-argument pair
'-{0}{1}{2}' -f $paramName, $sep, $argTransformed
} else { # the command name or a hashtable key or value
$argTransformed
}
}
# Synthesize and output the pseudo-command line.
$cmdLine = (& $sbToCmdLineArg '' $commandName)
if ($commandArgs -is [Collections.IDictionary]) { # hashtable
$cmdLine += ' ' +
$(foreach ($param in $commandArgs.Keys) { & $sbToCmdLineArg $param $commandArgs[$param] }) -join ' '
} elseif ($commandArgs) { # array / other collection
$cmdLine += ' ' +
$(foreach ($arg in $commandArgs) { & $sbToCmdLineArg '' $arg }) -join ' '
}
# Output the command line.
# If the comamnd name ended up quoted, we must prepend '& '
if ($cmdLine[0] -eq "'") {
"& $cmdLine"
} else {
$cmdLine
}
}
With convertTo-PseudoCommandLine defined (before or above the code below), you can then use:
# Sample argument values.
$rgFullName = 'full name'
$location = 'loc'
$tags = #{ one = 1; two = 2; three = 3 }
# Define the command to execute:
# * as a string variable that contains the command name / path
# * as a hashtable that defines the arguments to pass via
# splatting (see below.)
$command = 'New-AzureRmResourceGroup'
$commandArgs = [ordered] #{
Name = $rgFullName
Location = $location
Tag = $tags
Force = $True
}
# Echo the command to be executed as a pseud-command line
# created by the helper function.
convertTo-PseudoCommandLine $command $commandArgs
# Execute the command, using & and splatting (note the '#' instead of '$')
& $command #commandArgs
This yields (excluding any output from the actual execution):
New-AzureRmResourceGroup -Name 'full name' -Location loc -Tag #{two=2;three=3;one=1} -Force:$True
Instead of typing this long command on the command line:
python babynames.py baby1990.html baby1992.html baby1994.html ... baby2008.html
Is there a shorter way to type like this for years from 1990 to 2008 with a increment of 2?
powershell way :
$cmd="babynames.py"
(1998..2008)|%{
if ($_ % 2 -eq 0){
$cmd=$cmd+" baby"+$_+".html"
}
}
start-process python.exe $cmd
Yeah, do a For loop.
$Cmd = "python babynames.py"
For($a=1990;$a -le 2008;$($a++;$a++)){
$cmd = "$cmd baby$a.html"
}
& $cmd
Another version:
$f = 0..9 | % { "baby$(2 * $_ + 1990).html" }
& python.exe babynames.py $f
or in one line:
& python.exe babynames.py (0..9 | % { "baby$(2 * $_ + 1990).html" })
Basicly I am trying to find a way to put a variable for replacing a line in powershell.
The current script:
$switches = get-outlookinbox | where subject -eq "Hello"
$e = $switches.body
$e = $e.replace("Hello:","")
$e = $e.replace(" Number","")
$e = $e.replace(":1","")
$e = $e.replace(":2","")
$e = $e.replace(":3","")
$e = $e.replace(":4","")
$e = $e.replace(":99","")
You can see what I am going for here... But I don't want 99 lines of replace code, any thoughts on this?
Also, the numbers MUST have : infront of it, otherwise the replace will corrupt the file since it contains only IP's and ports, it's the ports I want to remove from the output.
You can use a simple foreach loop and iterate from 99 to 1:
foreach ($n in 99..1)
{
$e = $e.Replace(":$n", " ")
}
Or, if you prefer one line:
foreach ($n in 99..1) { $e = $e.Replace(":$n", " ") }
Demo:
PS > $mystr = "a:1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20b"
PS > foreach ($n in 20..1) { $mystr = $mystr.replace(":$n", "") }
PS > $mystr
ab
PS >
Regular expressions : regex101.com
$e = $e -replace ':\d+',""
No loops necessary
Might as well get 'em all while you're at it:
$e -replace 'Hello:| Number|:\d{1,2}'
I've started to use the start-transcript in my profile to keep a log of everything I do via the shell.
it's becoming useful for looking back at what changes are made and when they were made. I'm also beginning to use it as the first steps of documentation. I've been commenting the things done in the shell for future reference.
The thing that is proving tricky is the formatting is that of a text doc and is not as easy to read as the shell (error, verbose and warning colours mainly).
I was wondering if anybody uses the Transcript functionality in this way and has a viewer of preference or a script that parses the log file to produce a doc of some sort?
Edit: i'm interested to know why the question has been down voted...
I believe it will be very hard to parse a transcript to create an accurate formatted document. You could however use the console host API to capture (parts of) the screen buffer.
This Windows Powershell blog article describes how this works.
A trivial way to use the (modified) script (Get-ConsoleAsHtml.ps1) is to modify your prompt function, so that all lines from the buffer that haven't been written to your html transcript yet, are saved every time the prompt function is called. The first block of code is the contents of the modified script, the second block of code shows how you can use this script in your profile.
###########################################################################################################
# Get-ConsoleAsHtml.ps1
#
# The script captures console screen buffer up to the current cursor position and returns it in HTML format.
# (Jon Z: Added a startline parameter)
#
# Returns: UTF8-encoded string.
#
# Example:
#
# $htmlFileName = "$env:temp\ConsoleBuffer.html"
# .\Get-ConsoleAsHtml 5 | out-file $htmlFileName -encoding UTF8
# $null = [System.Diagnostics.Process]::Start("$htmlFileName")
#
param (
$startline = 0
)
# Check the host name and exit if the host is not the Windows PowerShell console host.
if ($host.Name -ne 'ConsoleHost')
{
write-host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)."
exit -1
}
# The Windows PowerShell console host redefines DarkYellow and DarkMagenta colors and uses them as defaults.
# The redefined colors do not correspond to the color names used in HTML, so they need to be mapped to digital color codes.
#
function Normalize-HtmlColor ($color)
{
if ($color -eq "DarkYellow") { $color = "#eeedf0" }
if ($color -eq "DarkMagenta") { $color = "#012456" }
return $color
}
# Create an HTML span from text using the named console colors.
#
function Make-HtmlSpan ($text, $forecolor = "DarkYellow", $backcolor = "DarkMagenta")
{
$forecolor = Normalize-HtmlColor $forecolor
$backcolor = Normalize-HtmlColor $backcolor
# You can also add font-weight:bold tag here if you want a bold font in output.
return "<span style='font-family:Courier New;color:$forecolor;background:$backcolor'>$text</span>"
}
# Generate an HTML span and append it to HTML string builder
#
function Append-HtmlSpan
{
$spanText = $spanBuilder.ToString()
$spanHtml = Make-HtmlSpan $spanText $currentForegroundColor $currentBackgroundColor
$null = $htmlBuilder.Append($spanHtml)
}
# Append line break to HTML builder
#
function Append-HtmlBreak
{
$null = $htmlBuilder.Append("<br>")
}
# Initialize the HTML string builder.
$htmlBuilder = new-object system.text.stringbuilder
$null = $htmlBuilder.Append("<pre style='MARGIN: 0in 10pt 0in;line-height:normal';font-size:10pt>")
# Grab the console screen buffer contents using the Host console API.
$bufferWidth = $host.ui.rawui.BufferSize.Width
$bufferHeight = $host.ui.rawui.CursorPosition.Y
$rec = new-object System.Management.Automation.Host.Rectangle 0,0,($bufferWidth - 1),$bufferHeight
$buffer = $host.ui.rawui.GetBufferContents($rec)
# Iterate through the lines in the console buffer.
for($i = $startline; $i -lt $bufferHeight; $i++)
{
$spanBuilder = new-object system.text.stringbuilder
# Track the colors to identify spans of text with the same formatting.
$currentForegroundColor = $buffer[$i, 0].Foregroundcolor
$currentBackgroundColor = $buffer[$i, 0].Backgroundcolor
for($j = 0; $j -lt $bufferWidth; $j++)
{
$cell = $buffer[$i,$j]
# If the colors change, generate an HTML span and append it to the HTML string builder.
if (($cell.ForegroundColor -ne $currentForegroundColor) -or ($cell.BackgroundColor -ne $currentBackgroundColor))
{
Append-HtmlSpan
# Reset the span builder and colors.
$spanBuilder = new-object system.text.stringbuilder
$currentForegroundColor = $cell.Foregroundcolor
$currentBackgroundColor = $cell.Backgroundcolor
}
# Substitute characters which have special meaning in HTML.
switch ($cell.Character)
{
'>' { $htmlChar = '>' }
'<' { $htmlChar = '<' }
'&' { $htmlChar = '&' }
default
{
$htmlChar = $cell.Character
}
}
$null = $spanBuilder.Append($htmlChar)
}
Append-HtmlSpan
Append-HtmlBreak
}
# Append HTML ending tag.
$null = $htmlBuilder.Append("</pre>")
return $htmlBuilder.ToString()
Example of a profile:
############################################################################################################
# Microsoft.PowerShell_profile.ps1
#
$docpath = [environment]::GetFolderPath([environment+SpecialFolder]::MyDocuments)
$transcript = "$($docpath)\PowerShell_transcript.$(get-date -f 'yyyyMMddHHmmss').html";
$global:lastloggedline = 0
function prompt {
&'D:\Scripts\Get-ConsoleAsHtml.ps1' $global:lastloggedline | out-file $transcript -append;
$global:lastloggedline = $host.ui.rawui.cursorposition.Y
"PS $pwd$('>' * ($nestedPromptLevel + 1)) "
}
I can't figure out why the following code fails:
# test.ps1
"`$args: ($args)"
"`$args count: $($args.length)"
# this fails
0..$($args.length - 1) | %{ $args[$_] = ($args[$_] -replace '`n',"`n") }
# this works
$i = 0
foreach ( $el in $args ) { $args[$i] = $args[$i] -replace '`n',"`n"; $i++ }
"$args"
I'm calling it like so:
rem from cmd.exe
powershell.exe -noprofile -file test.ps1 "a`nb" "c"
Scoping issue. The $args inside the foreach-object (%) scriptblock is local to that scriptblock. The following works:
"`$args: $args"
"`$args count: $($args.length)"
$a = $args
# this fails
0..$($args.length - 1) | %{ $a[$_] = ($a[$_] -replace '`n',"`n") }
$a
Keith answered this question. Just wanted to add some more info, because I found it useful many times. Look at the code:
[21]: function test{
>> $args -replace '`n',"`n"
>> }
>>
[22]: test 'replace all`nnew' 'lines`nin the `nstring' 'eof`ntest'
replace all
new
lines
in the
string
eof
test
The -replace operator works with array too!