Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell - Load Arguments from a file

Tags:

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!

like image 864
PnP Avatar asked Oct 05 '22 23:10

PnP


1 Answers

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 }
like image 138
CB. Avatar answered Oct 10 '22 11:10

CB.