Question:
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:
1 2 |
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:
1 2 3 4 |
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
1 2 |
SERVERNAME_DRIVELETTER_b00x_ixxx.spi |
Where,
1 2 3 4 5 |
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:
1 2 3 |
MSSRV01 C 3 018 MSSRV02 D 9 119 |
And so on. It uses this information to mount a specific backup image (via ShadowProtect’s
1 2 |
mount.exe SERVERNAME_DRIVELETTER_b00x_ixxx.spi |
Thanks!
Answer:
You can try do something like this:
content of p.txt
:
1 2 3 4 |
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
1 2 3 4 5 6 7 8 9 10 |
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:
1 2 |
(Get-Content .\p.txt ) | % { invoke-expression ".\p.ps1 $_" } |
the result:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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.
1 2 3 |
Get-Content .\p.txt | % { $a = $_ -split ' ' ; mount.exe $($a[0])_$($a[1])_b00$($a[2])_i$($a[3]).spi } |