简体   繁体   中英

Power Shell Script for loop?

I am trying to create a script in PowerShell to run a utility with some default parameters. There will be multiple IP addresses into a CSV file that we will need to import. Then do a loop to run the utility for each IP address imported.

Set-Location 'C:\Program Files\Utility
$printerlist = Get-Content ".\Printer.csv"
$pass = Read-Host "Enter your Password"

.\Utility.exe -USERNAME="ADMIN" -PASSWORD="$pass"-ADDRESS="$printerlist" -PP-INSTALL="192.168.33.21" -JA-INSTALL="192.168.33.21" -IPA-INSTALL="192.168.33.21" -QUOTA-DELETE 

Foreach($printer in $printerlist){
   echo $line

}

As the comment points out, you need to loop over those IP addresses. You can use a feature called splatting to apply a set of default arguments:

Set-Location -Path $Env:ProgramFiles\Utility

$pass = Read-Host -Prompt 'Enter your Password'
$utilArgs = @(
    '-USERNAME="ADMIN"'
    "-PASSWORD=""$pass"""
    '-PP-INSTALL="192.168.33.21"'
    '-JA-INSTALL="192.168.33.21"'
    '-IPA-INSTALL="192.168.33.21"'
    '-QUOTA-DELETE'
)

foreach ($printer in Get-Content -Path Printer.csv) {
    .\Utility.exe "-ADDRESS=""$printer""" @utilArgs
}

Docs

about_Splatting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM