5

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!

1 Answer 1

5

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 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - can you look to the above edit - this is great, but I'm not sure if it fits my problem as I don't think I described in enough detail!
Awesome, So your piping the text file into the next statement, what exactly does the % { $a = $_ -split ' ' ; do?
@TheD '%' is the alias of foreach-object cmdlet. Because I pipe one line at time I need to split it using the space character and assign to a variable $a. ';' ends the split command and let the mount.exe start his job with the parameter build from the $a string array. I hope to be clear ;) I'm not an english native speaker...
Out of interest - can I then keep each the whole string as a single variable? i.e. SERVERNAME_DRIVELETTER_b00x_ixxx.spi as one string in a variable? i.e. what is after the mount.exe as one variable like $baseImage ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.