Powershell - Load Arguments from a file - powershell

This is my 2nd question is fairly quick succession! Essentially at the moment, I'm running a powershell script which I execute manually and pass arguments to on the CMD line like:
PostBackupCheck.Ps1 C 1 Hello Test Roger
Those are placed into variables and used in the script.
Is there a way I could add these line by line to a text file such like:
C 1 Hello Test Roger 0
C 2 Hello Test Roger 1
C 3 Hello Test Roger 2
And then get the Powershell script to use the first line, do the script, then loop back and use the next line, do the script, loop back and so on.
So in context - I need to mount images in the following naming context
SERVERNAME_DRIVELETTER_b00x_ixxx.spi
Where,
SERVERNAME = Some string
DRIVELETTER = Some Char
b00X - where X is some abritrary number
ixxx - where xxx is some abritrary number
So in my text file:
MSSRV01 C 3 018
MSSRV02 D 9 119
And so on. It uses this information to mount a specific backup image (via ShadowProtect's
mount.exe SERVERNAME_DRIVELETTER_b00x_ixxx.spi
Thanks!

You can try do something like this:
content of p.txt:
C 1 Hello Test Roger 0
C 2 Hello Test Roger 1
C 3 Hello Test Roger 2
the content of the script p.ps1
param($a,$b,$c,$d,$e,$f)
"param 1 is $a"
"param 2 is $b"
"param 3 is $c"
"param 4 is $d"
"param 5 is $e"
"param 6 is $f"
"End Script"
the call to script:
(Get-Content .\p.txt ) | % { invoke-expression ".\p.ps1 $_" }
the result:
param 1 is C
param 2 is 1
param 3 is Hello
param 4 is Test
param 5 is Roger
param 6 is 0
End Script
param 1 is C
param 2 is 2
param 3 is Hello
param 4 is Test
param 5 is Roger
param 6 is 1
End Script
param 1 is C
param 2 is 3
param 3 is Hello
param 4 is Test
param 5 is Roger
param 6 is 2
End Script
Edit:
after your edit you can try something like this.
Get-Content .\p.txt |
% { $a = $_ -split ' ' ; mount.exe $($a[0])_$($a[1])_b00$($a[2])_i$($a[3]).spi }

Related

PowerShell one counter behaves differently at some part of a script

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.

How to use if condition in selenium IDE

3 login based questions in the website to be automated
I need correct code to automate these.
enter image description here
When using "if ..." Commands in test steps in Selenium IDE for Chrome, you need to remember the following:
Use "if ... end" to form a single branch of test steps.
Use "if ... else ... end" to form two branches of test steps.
Use "if ... else if ... else ... end" to form multiple branches of test steps.
Use JavaScript Boolean expression syntax to specify conditions to "if" and "else if" commands.
${variable} is allowed in condition expressions.
Here is a sample test called "Condition" on how to use variables:
1 execute script | return (new Date()).getDay(); | day
2 echo | ${day}
3 if | ${day} < 1
4 echo | Sunday
5 else if | ${day} > 5
6 echo | Saturday
7 else
8 echo Weekday
9 end
Here is the log output when you execute the above test.
1. executeScript on return (new Date()).getDay(); with value day OK
echo: 0
3. if on ${day} < 1 OK
echo: Sunday
5. elseIf on ${day} > 5 OK
7. else OK
9. end OK

How does PowerShell split unquoted command-line arguments?

PowerShell does not split command arguments in the same way as cmd.exe or sh. The behaviour I'm stumbling over is that a . character sometimes, but not always, starts a new argument.
To illustrate:
PS D:\temp> gc .\list-args.py
import sys
print(*sys.argv, sep='\n')
PS D:\temp> py .\list-args.py a.txt .\b -c=d -e=.\f -g.h -.i=j
.\list-args.py
a.txt
.\b
-c=d
-e=
.\f
-g
.h
-.i=j
PS D:\temp>
What rules does Powershell use to separate one command-line argument from the next?
Where are these documented?
Here's echoargs output (http://ss64.com/ps/EchoArgs.exe).
.\echoargs .\list-args.py a.txt .\b -c=d -e=.\f -g.h -.i=j
Arg 0 is <.\list-args.py>
Arg 1 is <a.txt>
Arg 2 is <.\b>
Arg 3 is <-c=d>
Arg 4 is <-e=>
Arg 5 is <.\f>
Arg 6 is <-g>
Arg 7 is <.h>
Arg 8 is <-.i=j>
One quoting workaround:
.\echoargs .\list-args.py a.txt .\b -c=d -e='.\f' '-g.h' -.i=j
Arg 0 is <.\list-args.py>
Arg 1 is <a.txt>
Arg 2 is <.\b>
Arg 3 is <-c=d>
Arg 4 is <-e=.\f>
Arg 5 is <-g.h>
Arg 6 is <-.i=j>
. might be used for property references.
$a = [pscustomobject]#{name='joe'}
write-output $a.name
joe
The command line parses arrays too.
$b = 1,2,3
write-output $b[0]
1

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.

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