我拥有的是一个名为$ usercity.city的变量,该变量是通过Msoluser查询获得的,用于获取用户城市。 我需要做的就是根据该值填充一个名为$ icthead的变量。我的意思是说,我有30个城市的icthead有所不同,从简单的切换到这样做有没有不同的方法? 就像从外部XML加载之类的东西一样,谢谢!如果您认为一个简单的开关不会减慢我的脚本的速度,那我就走那条路。
我想做的一个实际例子是:
$usercity=Milan then $icthead=email1@company.it
$usercity=London then $icthead=email2@company.it
$usercity=chicago then $icthead=email3@company.it
,但适用于20个或更多城市。一种解决方案是将每个电子邮件加载到数组[$ usercity]上,但我自己做不到,因为我真的很不好。预先感谢。
答案 0 :(得分:0)
我不确定是否要查找类似的内容,但是如果您将数据保存在excel表中,并且每行数据中包含每个城市的电子邮件,那么您可以抓取这种城市的邮件:
foreach($city in $usercity.city)
{
icthead
function icthead {
# This part is to open up the designated xlsx file and make powershell "look" in it.
$Excel = New-Object -ComObject excel.application
$DestinationPath = "Your xlsx file location"
$workbook = $Excel.workbooks.open($DestinationPath)
$Sheet = $workbook.worksheets.item("Name of the excel tab your information is in")
$Sheet.activate()
# This will find the horizontal row that the city is in.
$GetCity = $Sheet.Range("Vertical range .ex B2:B9999").find("$City")
$Row = $GetCity.Row
# This will require you to download the ImportExcel module - https://www.powershellgallery.com/packages/ImportExcel/3.0.0 - All credit to Douglas Finke
$XL = Import-Excel "Your xlsx file location"
# .Mail is an example. Should be your column title. It will grab the value that is in that row under that column and save it in the variable. Remember that xlsx is 0 indexed.
$CityMail = $XL[$row].Mail
}
$IctHead = $CityMail
Write-host $IctHead
}
答案 1 :(得分:0)
我认为您正在寻找external reference
。可以通过多种方式完成。以下是其中一些:
哈希表/字典变量:
#declare hashtable type variable.
$ht = @{}
#populate it with key/value items.
$ht.Add('Milan','email1@company.it')
$ht.Add('London','email2@company.it')
$ht.Add('Chicago','email3@company.it')
#input city name Milan
$city = 'Milan'
#now call hashtable variable by its key Milan
$ht[$city]
#output
email1@company.it
#usage example
Set-UserProperty -City $city -Email $ht[$city]
XML字符串:
[xml]$xml = @"
<?xml version="1.0" encoding="utf-8" ?>
<Dictionary>
<City name="Milan">
<Email>email1@company.it</Email>
</City>
<City name="London">
<Email>email2@company.it</Email>
</City>
<City name="Chicago">
<Email>email3@company.it</Email>
</City>
</Dictionary>
"@
$xml.Dictionary.City | Where-Object {$_.name -eq $city} | Select -ExpandProperty Email
CSV字符串:
$city = 'Milan'
$CSV = @"
"City","Email"
"Milan","email1@company.it"
"London","email2@company.it"
"Chicago","email3@company.it"
"@ | ConvertFrom-Csv
($CSV | Where-Object {$_.City -eq $city}).Email
#output
email1@company.it
XML / CSV文件:
here string
(@" ... "@
)的内容相同。例如:myfile.xml:
<?xml version="1.0" encoding="utf-8" ?>
<Dictionary>
<City name="Milan">
<Email>email1@company.it</Email>
</City>
<City name="London">
<Email>email2@company.it</Email>
</City>
<City name="Chicago">
<Email>email3@company.it</Email>
</City>
</Dictionary>
myfile.csv:
"City","Email"
"Milan","email1@company.it"
"London","email2@company.it"
"Chicago","email3@company.it"
分别用here string
或@" ... "@
替换上面代码中的Get-Content -Raw -Path 'myfile.xml'
(Get-Content -Raw -Path 'myfile.csv'
)。例如:
[xml] $ xml =获取内容-Raw -Path'myfile.xml'
或
$ CSV =获取内容-原始-路径'myfile.csv'
其余代码保持不变。