验证PowerShell

时间:2018-06-08 18:06:18

标签: powershell hashtable powershell-v4.0 indirection

我正在编写一个脚本来生成基于用户提供的变量值的文本文件。变量通过另一个应用程序提供,它们作为环境变量进入Windows。

所以我有以下内容:

[hashtable]$variables = @{InstallPath = $env:InstallPath; ADBaseOUValue1 = $env:ADBaseOUValue1; ADBaseOUValue2 = $env:ADBaseOUValue2; LDAPQueryValue1 = $env:LDAPQueryValue1; LDAPQueryValue2 = $env:LDAPQueryValue2}

此哈希表被截断,用户最多可以定义15个ADBaseOU和15个LDAP查询,并且还提供了其他变量,但它们都没有编号。

现在我需要验证数据。

  1. 我需要确保ADBaseOUValue1和LDAPQueryValue1不为空
  2. 我需要确保ADBaseOUValue1与正则表达式匹配
  3. 我需要确保,如果LDAPQueryValue1的&符号(&)未跟“amp;”,我将替换它们
  4. 然后我需要为用户提供的任何ADBaseOUValue#和LDAPQueryValue#进行检查2和3。
  5. 我正在对其他变量(例如$ env:InstallPath)进行数据验证,以确保它们不为空且具有预期的数据类型。

    在变量$ env:ADBaseOUValue2到15和$ env:LDAPQueryValue2到15中,我将如何:

    1. 在对其正则表达式执行“If”检查时定义变量名称
    2. 在执行“&”
    3. 的“替换”时定义变量名称

      验证变量后,值将进入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 "&amp;") {
                  Write-Host ("{0}: Replacing `"&`" with `"&amp;`" in {1}." -f (Get-Date -Format s), $_.Name)
      
                  $env:LDAPQueryValue1 = $env:LDAPQueryValue1.Replace("&", "&amp;")
              }
          }
          {($_.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 "&amp;") {
                  Write-Host ("{0}: Replacing `"&`" with `"&amp;`" in {1}." -f (Get-Date -Format s), $_.Name)
      
                  $env:??? = $env:???.Replace("&", "&amp;")
              }
          }
      }
      

1 个答案:

答案 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 "&amp;") {
      Write-Host ("{0}: Replacing `"&`" with `"&amp;`" in {1}." -f (Get-Date -Format s), $_.Name)

      Set-Item Env:$($_.Name) $currVal.Replace("&", "&amp;")
  }
}

# ...