我正在编写一个脚本来根据用户提供的变量值生成文本文件。这些变量通过另一个应用程序提供,它们作为环境变量进入 Windows。
所以我有以下内容:
[hashtable]$variables = @{InstallPath = $env:InstallPath; ADBaseOUValue1 = $env:ADBaseOUValue1; ADBaseOUValue2 = $env:ADBaseOUValue2; LDAPQueryValue1 = $env:LDAPQueryValue1; LDAPQueryValue2 = $env:LDAPQueryValue2}
此哈希表被截断,用户最多可以定义 15 个 ADBaseOU 和 15 个 LDAP 查询,并且还提供了其他变量,但没有一个变量是编号的。
现在我需要验证数据。
- 我需要确保 ADBaseOUValue1 和 LDAPQueryValue1 不为空
- 我需要确保 ADBaseOUValue1 与正则表达式模式匹配
- 我需要确保,如果 LDAPQueryValue1 的与号 (&( 没有被"amp;"分隔,我替换它们
- 然后,我需要对用户提供的任何 ADBaseOUValue# 和 LDAPQueryValue# 进行检查 2 和 3。
我正在对其他变量(例如 $env:InstallPath(进行数据验证,以确保它们不为空并具有预期的数据类型。
在变量 $env:ADBaseOUValue2 到 15 和 $env:LDAPQueryValue2 到 15 中,我将如何:
- 在对其正则表达式执行"If"检查时定义变量名称
- 在执行"&"的"替换"时定义变量名称
验证变量后,值将进入此处字符串,如下所示:
<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("&", "&")
}
}
}
听起来你需要的是变量间接,即通过存储在变量中的名称间接访问环境变量;例如:
$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("&", "&")
}
}
# ...