我有4个变量,我想让脚本多次执行这些变量,我的想法是将每个变量都放在自己的文件中,然后遍历文件,并具有将变量循环的主脚本。
我想知道这是否很聪明,或者我能否以更简单的方式做到这一点 基本上看起来像这样
File1
$SFTP_NAME = "PATH_1"
$CUSTOMER_NAME = "Customer_1"
$BACKUP_LOCATION = "Customer_1"
$IP_ADDRESS = 192.168.159.11
File2
$SFTP_NAME = "PATH_2"
$CUSTOMER_NAME = "Customer_2"
$BACKUP_LOCATION = "Customer_2"
$IP_ADDRESS = 192.168.159.12
Main Script
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME\$BACKUP_LOCATION
New-Item -ItemType directory -Path \\ftp\Customers\$SFTP_NAME\$CUSTOMER_NAME\$IP_ADDRESS
答案 0 :(得分:2)
我推荐一个包含四列的CSV文件:
$configurations = @'
SFTP_NAME,CUSTOMER_NAME,BACKUP_LOCATION,IP_ADDRESS
PATH_1,Customer_1,Customer_1,192.168.159.11
PATH_2,Customer_2,Customer_2,192.168.159.12
'@ | ConvertFrom-Csv
#or prepare csv file in Excel and import
#$configurations = Import-Csv Csvfile.csv
$configurations | % {
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.BACKUP_LOCATION)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.IP_ADDRESS)"
}
答案 1 :(得分:0)
创建一个数组,并使用自定义对象将您的配置数据存储在其中。然后循环遍历数组并使用数据创建项。
$fileList = @()
$fileList += New-Object psobject -Property @{ "SFTP_NAME"="PATH_1"
"CUSTOMER_NAME"="Customer_1"
"BACKUP_LOCATION"="Customer_1"
"IP_ADDRESS"="192.168.159.11"}
$fileList += New-Object psobject -Property @{ "SFTP_NAME"="PATH_2"
"CUSTOMER_NAME"="Customer_2"
"BACKUP_LOCATION"="Customer_2"
"IP_ADDRESS"="192.168.159.12"}
$fileList | foreach {
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.BACKUP_LOCATION)"
New-Item -ItemType directory -Path "\\ftp\Customers\$($_.SFTP_NAME)\$($_.CUSTOMER_NAME)\$($_.IP_ADDRESS)"
}