PowerShell one counter behaves differently at some part of a script - powershell

I am running a PS script to create an excel file with some text and the corresponding pictures to the text. Till now everything works as expected but when i want to choose the right column for the pictures with a counter something weird happens.
Before the if statement the counter behaves like it should, but as soon i let it output the counter inside the if statement the counter does something weird what i cannot explain.
Part of the Script
pictures is an list of file names
rows_sorted is an list of camera names
foreach ($picture in $pictures) {
foreach ($row in $rows_sorted) {
$xlcounter += 1
Write $xlcounter -> counter looks fine
if ($picture -match $row ) {
$live_picture = "C:\Snapshots\pictures\$picture"
$image = [System.Drawing.Image]::FromFile($live_picture)
$sheet | Add-ExcelImage -Image $image -Row $xlcounter -Column 4 -ResizeCell
$image.Dispose()
Write $xlcounter -> Weird things happen
}
}
}
Example output of counter outside the if statement:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Example output of counter inside the if statement:
2 205 408 611 814 1017 1220 1423 1626 1829 2032 2235 2438 2641 2844 3047 3251 3454 3657 3860 4063
I tried to rename the counter because the same name was used in a few lines before, but that did not help either.

Related

CLLE SNDRCVF command not allowed

I am trying to compile this piece of CL code using Rational Series but keep getting error.
This is my CL code:
PGM
DCLF FILE(LAB4DF)
SNDRCVF RCDFMT(RECORD1) /* send, recieve file */
DOWHILE (&IN03 = '0')
SELECT
WHEN (&USERINPUT = '1' *OR &USERINPUT = '01') CALLSUBR OPTION1
OTHERWISE DO
*IN03 = '1'
ENDDO
ENDSELECT
ENDDO
SUBR OPTION1
DSPLIBL
ENDSUBR
ENDPGM
And this is my DSPF code
A R RECORD1
A 1 38'LAB 4'
A 3 3'Please select one of the following-
A options:'
A 6 11'3. Maximum Invalid Signon Attempt-
A s allowed'
A 8 11'5. Run Instructor''s Insurance Pr-
A ogram'
A 5 11'2. Signed on User''s Message Queu-
A e'
A 1 3'Yathavan Parameshwaran'
A 7 11'4. Initial number of active jobs -
A for storage allocation'
A 4 11'1. Previous sign on by signed on -
A user'
A 14 11'F3 = Exit'
A 14 31'F21 = Command Line'
A 2 70TIME
A 1 72DATE
A 9 11'Option: '
A USERINPUT 2 B 9 19
A 91 DSPATR(RI)
A 92 DSPATR(PC)
A MSGTXT1 70 O 11 11
A MSGTXT2 70 O 12 11
Is there a problem with my CL code or DSPF code?
You forgot to say what error you were getting. It's always important to put all the information about error messages into your questions.
There are two errors.
&IN03 is not defined
Your assignment to *IN03 should be to &IN03, but that's not how you do an assignment in CLP
If you want to be able to press F3, you have to code something like CA03(03) in the "Functions" for the record format.
To assign a variable in CL, code
CHGVAR name value
Looking at the documentation here, I suspect you need to add RCDFMT to your DCLF spec like so:
DCLF FILE(LAB4DF) RCDFMT(RECORD1)
SNDRCVF RCDFMT(RECORD1) /* send, recieve file */
If you really do only have 1 record format in your display file, then you can also omit the RCDFMT from both commands like so:
DCLF FILE(LAB4DF)
SNDRCVF /* send, recieve file */

How to add a new line after every integer

I am trying to figure out a way to make a new variable from another to output to a GUI. When I try to just display the variable through a lable it loses its line breaks.
I managed to figure out a solution when working with text but when it comes to numbers it does not work.
Here is what I have tried:
$ActiveUnits = #(Get-MsolAccountSku | Select-Object -ExpandProperty ActiveUnits)
$ActiveUnitsFix = "`n"
foreach ($Unit in $ActiveUnits) {
$ActiveUnitsFix += #($Unit + "`n")
}
The output that I am getting is this:
31425220100002521100001000000100000002137328420
When it should be something like this:
3
14
25
220
10000
25
21
10000
1000000
10000000
213
7
3
28
4
20
You could use the -join parameter for adding the new line if you receive an int array from (Get-MsolAccountSku).ActiveUnits.
[System.Int32[]]$ActiveUnits = (Get-MsolAccountSku).ActiveUnits
[System.String]$ActiveUnitsFix = $ActiveUnits -join [System.Environment]::NewLine
$ActiveUnitsFix

Why does an argument $args after = sign not get expanded? ($args as parameter to jvm typical -D switch)

I have the following definition in my *profile.ps1 file:
if(Test-Path $env:M2_HOME){
function mvn{
$cmd = "$env:M2_HOME\bin\mvn.bat"
& $cmd $args
}
}
When I define a function using this function in powershell like:
function d { mvn help:describe $args }
using like:
d -Dplugin=jar
everything is fine as opposed to defining the latter as:
function d { mvn help:describe -Dplugin=$args }
using like:
d jar
Is there some builtin to handle this corner case?
It looks like you just need to make sure you're passing the arguments as strings and ensure they're evaluated first:
function mvn{
$cmd = "$env:M2_HOME\bin\mvn.bat"
& $cmd $args
}
function d { mvn "help:describe" "-Dplugin=$($args)" }
for get argument through call function you should use like this
function test {
write-host $args[0]
write-host $args[1]
}
test stackoverflow powershell
output
stackoverflow
powershell
stackoverflow is first argument passed to function and powershell is second argument passed to function
for every argument passed to function
function test {
foreach ($a in $args){
write-host "output:$args"
}
}
test 1 2 3 4 5 6 7 8
output:
test 1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
output:1 2 3 4 5 6 7 8
for your function
if(Test-Path $env:M2_HOME){
function mvn{
$cmd = "$env:M2_HOME\bin\mvn.bat"
foreach ($arg in $args) {
& $cmd $args}
}
}
Someone seems to have made a similar observation:
The reason seems -D being a special character in powershell more or less marking where exactly each particluar special option string determined by -D ends, at least this modification to my calling function then works for me:
function x {mvn help:describe `-Dplugin=$args}
Sure it would be nice to handle such occasions in the hosting function somehow (mvn definition in my *profile.ps1), but that solutions rather seems out of scope of my question.

Null character appearing when I print a file

I have a code where I read a file and remove a block of line if a certain keyword matches. If I see the key word THERMST, I delete the line before and all lines until I reach a & :
QNODE "CExtHrn - Heater_Bidon" 1.0 T884 TOTAL
THERMST "CExtHrn" 0 2.500000E+01 3.000000E+01 883 ID 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 "Heater_Bidon"
NAME2 Heater_ CExtHrn - Heater_Bidon
NAME Heater_ 40097 40170 1
TABTYPE 884 TABLE OPERATION
TABDATA 884 885 INTERP
TABDATA 884 883 THERMST
TABTYPE 885 QNODE TIME
TABDATA 885 2.000000E+01 0.000000E+00
$
However, for an obscure reason, when I print to a new file, it gives several null characters on a certain line. The weird thing is that this line is not related with the line I just changed. If I don't modify the file, by commenting the following lines, I don't get any null characters.
# We delete the last 2 line and skip the rest of the qnode/thermst definition
splice #INPF1_OUT, -2;
# Skipping the lines until next comment line.
$ii++ until substr($INPF1_IN[$ii], 0, 1) eq '$';
$ii = $ii - 1;
Any idea what this could be? The null characters are causing problems for what I do with the file.
Here is what the line should be :
NAME winte_T 101269 101270 1
here is what it prints in the new file :
NAME winte_T ULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNULNUL 101269 101270 1
You can see that the line that cause the error is not related to the one that should be modified
Thank you, the code is below
#!/bin/perl
use strict;
use Text::ParseWords;
open (INPF1_in, '<', $INPF1)
or die "Not able to open : $INPF1";
my #INPF1_IN = <INPF1_in>;
close INPF1_in;
my #INPF1_OUT; # Output INPF1
my $cardno = 1;
my $ii = 0;
until ($ii > $#INPF1_IN) {
my $INPF_line = $INPF1_IN[$ii];
push(#INPF1_OUT, $INPF_line); # Adding line
chomp($INPF_line);
if ($INPF_line eq "-1") {
$cardno++;
}
if ($cardno == 9) {
my #line = parse_line(" ", 0, $INPF_line); # parsing the line elements
if ($line[0] eq "THERMST") { # If Thermostat
# We delete the last 2 line and skip the rest of the qnode/thermst definition
splice #INPF1_OUT, -2;
$ii++ until substr($INPF1_IN[$ii], 0, 1) eq '$';
$ii = $ii-1; # Skipping the lines until next comment line.
}
}
$ii++;
}
open (INPF1_out, '>', $INPF1);
print INPF1_out $_ foreach #INPF1_OUT;
close INPF1_out;
I may be misreading your code, but it look like you're trying to do something very simple in perl, a very hard way.
If I'm reading it right, what you're trying to do is take an input record format, and conditionally print certain lines. Perl has a very good tool for this, called the 'range operator'.
I think you will be able to accomplish what you want with something considerably simpler.
#!/bin/perl
use strict;
use warnings;
while ( <DATA> ) {
print unless ( m/^THERMST/ ... m/^\$$/ );
}
__DATA__
QNODE "CExtHrn - Heater_Bidon" 1.0 T884 TOTAL
THERMST "CExtHrn" 0 2.500000E+01 3.000000E+01 883 ID 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 0.000000E+00 "Heater_Bidon"
NAME2 Heater_ CExtHrn - Heater_Bidon
NAME Heater_ 40097 40170 1
TABTYPE 884 TABLE OPERATION
TABDATA 884 885 INTERP
TABDATA 884 883 THERMST
TABTYPE 885 QNODE TIME
TABDATA 885 2.000000E+01 0.000000E+00
$
This is an example, based on the data you've given so far - if you can give a bit more to show exactly what you're trying to accomplish, I would be pretty sure you can extract the information you need without having to do iterating through elements in an array of words. Perl can do better than that.
(I am guessing a bit, as it's completely unclear where you're getting $cardno from. However this should be quite easy to modify to suit your needs)

Re-Use of a line fails as "`r" doesn't move the 'line-pointer' ("`b" fails as well)

As C# does not has the options of powershell's write-progress I start to try to 're-use' the same line:
function ReUseFails {
$x=0
$nDec = 10
while ($true) {
$x++
$a = $x.ToString().PadLeft($nDec)
$z = $x.ToString().PadLeft($nDec)
Write-Host "`r$a $z" -noNewLine
Start-Sleep -s 1
}
}
ReUseFails
I expected because of `r (carriage return) to see in the same line (the next line overrides the prev.):
1 1 # and after 1 Second in that line:
2 2 # after the 2nd second
3 3 # (and so on)
but what I get is
1 1 2 2 3 3 4 4 ...
So the carriage return r has no effect?<br>
Is there a way to 'enable' thisr??
Even when I try to use `b I see additional spots but not a back-space.
Can it be that the DOS-console can do that (to show a spinning wheel) but bot the PS-console?
Regards