Powershell - load arguments from a file

This is my second question - a pretty quick sequence! Essentially, at the moment, I am running a powershell script, which I am running manually, and passing arguments to the CMD line, for example:

PostBackupCheck.Ps1 C 1 Hello Test Roger 

They are placed in variables and used in the script.

Is there any way to add these lines line by line to a text file, for example:

 C 1 Hello Test Roger 0 C 2 Hello Test Roger 1 C 3 Hello Test Roger 2 

And then run the Powershell script to use the first line, make a script, then go back and use the next line, execute 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

 mount.exe SERVERNAME_DRIVELETTER_b00x_ixxx.spi 

Thanks!

+4
source share
1 answer

You can try to do something like this:

p.txt content:

 C 1 Hello Test Roger 0 C 2 Hello Test Roger 1 C 3 Hello Test Roger 2 

contents of 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" 

script call:

 (Get-Content .\p.txt ) | % { invoke-expression ".\p.ps1 $_" } 

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 editing 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 } 
+3
source

Source: https://habr.com/ru/post/1446667/


All Articles