我正在编写一个脚本来生成基于用户提供的变量值的文本文件。变量通过另一个应用程序提供,它们作为环境变量进入Windows。
所以我有以下内容:
[hashtable]$variables = @{InstallPath = $env:InstallPath; ADBaseOUValue1 = $env:ADBaseOUValue1; ADBaseOUValue2 = $env:ADBaseOUValue2; LDAPQueryValue1 = $env:LDAPQueryValue1; LDAPQueryValue2 = $env:LDAPQueryValue2}
此哈希表被截断,用户最多可以定义15个ADBaseOU和15个LDAP查询,并且还提供了其他变量,但它们都没有编号。
现在我需要验证数据。
我正在对其他变量(例如$ env:InstallPath)进行数据验证,以确保它们不为空且具有预期的数据类型。
在变量$ env:ADBaseOUValue2到15和$ env:LDAPQueryValue2到15中,我将如何:
验证变量后,值将进入Here String,如:
<setting name="ADBaseOU1" serializeAs="String">
<value>$env:ADBaseOUValue1</value>
</setting>
<setting name="ADBaseOU2" serializeAs="String">
<value>$env:ADBaseOUValue2</value>
</setting>
谢谢,这就是我现在所拥有的(对于验证部分):
Switch ($variables.GetEnumerator()) {
{($_.Name -eq "ADBaseOUValue1") -and ($_.Value -ne $null)} {
If ($env:ADBaseOUValue1 -notmatch $ldapUrlRegex) {
Write-Host ("{0}: The variable `"{1}`" was not a valid LDAP URL. Please re-run the script and provide a valid value for the variable." -f (Get-Date -Format s), $_.Name)
$variableErrorCount++
}
}
{($_.Name -eq "ADBaseOUValue1") -and ($_.Value -eq $null)} {
If ($env:ADBaseOUValue1 -notmatch $ldapUrlRegex) {
Write-Host ("{0}: The variable `"{1}`" was null. Please re-run the script and provide a value for the variable." -f (Get-Date -Format s), $_.Name)
$variableErrorCount++
}
}
{($_.Name -match "ADBaseOUValue(\d+)$") -and ($_.Name -ne "ADBaseOUValue1") -and ($_.Value -ne $null)} {
If ($env:ADBaseOUValue1 -notmatch $ldapUrlRegex) {
Write-Host ("{0}: The variable `"{1}`" was not a valid LDAP URL. Please re-run the script and provide a valid value for the variable." -f (Get-Date -Format s), $_.Name)
$variableErrorCount++
}
}
{($_.Name -eq "LDAPQueryValue1") -and ($_.Value -ne $null)} {
If ($env:LDAPQueryValue1 -notmatch "&") {
Write-Host ("{0}: Replacing `"&`" with `"&`" in {1}." -f (Get-Date -Format s), $_.Name)
$env:LDAPQueryValue1 = $env:LDAPQueryValue1.Replace("&", "&")
}
}
{($_.Name -eq "LDAPQueryValue1") -and ($_.Value -eq $null)} {
If ($env:ADBaseOUValue1 -notmatch $ldapUrlRegex) {
Write-Host ("{0}: The variable `"{1}`" was null. Please re-run the script and provide a value for the variable." -f (Get-Date -Format s), $_.Name)
$variableErrorCount++
}
}
{($_.Name -match "LDAPQueryValue(\d+)$") -and ($_.Name -ne "LDAPQueryValue1") -and ($_.Value -ne $null)} {
If ($env:??? -notmatch "&") {
Write-Host ("{0}: Replacing `"&`" with `"&`" in {1}." -f (Get-Date -Format s), $_.Name)
$env:??? = $env:???.Replace("&", "&")
}
}
}
答案 0 :(得分:0)
听起来你需要的是变量间接,即通过存储在变量中的名称来间接访问环境变量 ; e.g:
$env:Foo = 'bar' # sample env. var.
$varName = 'Foo' # the name of that env. var to use for indirect access
# To GET the value, via PS drive Env:
(Get-Item Env:$varName).Value # same as: $env:Foo
# To SET the value:
Set-Item Env:$varName 'new value' # same as: $env:Foo = 'new value'
在你的命令背景下:
# ...
{$_.Name -match "LDAPQueryValue(\d+)$") -and ($_.Name -ne "LDAPQueryValue1") -and ($_.Value -ne $null)} {
$currVal = (Get-Item Env:$($_.Name)).Value
If ($currVal -notmatch "&") {
Write-Host ("{0}: Replacing `"&`" with `"&`" in {1}." -f (Get-Date -Format s), $_.Name)
Set-Item Env:$($_.Name) $currVal.Replace("&", "&")
}
}
# ...