gpt4 book ai didi

powershell - 在PowerShell中验证编号的变量

转载 作者:行者123 更新时间:2023-12-03 00:10:12 26 4
gpt4 key购买 nike

我正在编写一个脚本,用于根据用户提供的变量值生成文本文件。这些变量是通过另一个应用程序提供的,它们作为环境变量进入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具有与号(&)不符的“&”,我将其替换为
  • 然后,我需要对用户提供的任何ADBaseOUValue#和LDAPQueryValue#进行检查2和3。

  • 我正在对其他变量(例如$ env:InstallPath)进行数据验证,以确保它们不为null并具有预期的数据类型。

    在变量$ env:ADBaseOUValue2到15和$ env:LDAPQueryValue2到15中,我将如何:
  • 对其正则表达式
  • 进行“如果”检查时,定义变量名称
  • 在执行“&”
  • 的“替换”时定义变量名称

    验证变量后,值将进入Here字符串,例如:
    <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;")
    }
    }
    }

    最佳答案

    听起来您需要的是变量间接访问,即通过存储在变量中的名称间接访问环境变量;例如。:

    $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;")
    }
    }

    # ...

    关于powershell - 在PowerShell中验证编号的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50766222/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com