获取内容不会从包含环境变量和空格的路径读取文件



我正在使用PowerShell读取AD的某些扩展属性,然后从C:drive(如果存在)中读取本地文件,替换扩展属性中的值,然后写入文件回到同一个地方。

,我也在网络中读取一个通用文件,以防本地文件不存在,我将更新该文件并在必要时将其写入本地路径。

i可以将广告用户,扩展构成值,网络中的通用文件替换为某些变量,以及本地文件的路径,但这是我解决问题的地方。本地文件存储在用户的配置文件中(非常自然地包含空格),因此我需要在路径中使用环境变量。

这是我的代码:

$names=get-adgroupmember sg-deptest-test | foreach-object {
  # Get the relevant properties for each user in the group
  $userdetails = Get-ADUser -Id $_.samaccountname -Properties extensionattribute11, extensionattribute12, extensionattribute13, extensionattribute14, extensionattribute15
  # Assign the 3CX values in the extension attributes to some variables
  $AccountName = $userdetails.extensionattribute11
  $CallerID    = $userdetails.extensionattribute12
  $Extension   = $userdetails.extensionattribute13
  $ID          = $userdetails.extensionattribute14
  $Password    = $userdetails.extensionattribute15
  # Read the generic 3CX Configuration file on the network into the $3CXconfig
  # array
  $3CXconfig = Get-Content \domainitservicesappdeployment3cx3CXVoipPhone.ini
  # Set the relevant lines in the $3CXconfig array according to the variables
  # containing the extensionattribute values
  $3CXconfig[2] = "Name=$AccountName"
  $3CXconfig[4] = "CallerID=$CallerID"
  $3CXconfig[5] = "AuthUser=$Extension"
  $3CXconfig[6] = "AuthID=$ID"
  $3CXconfig[7] = "AuthPass=$Password"
  # Sets the path to the local config file
  $LocalFilePathPart1 = $env:localappdata
  $LocalFilePathPart2 = "3CX Voip Phone3CXVoipPhone.ini"
  $LocalFilePath      = $LocalFilePathPart1 + $LocalFilePathPart2
  # Read the existing 3CX configuration file into the $ExistingConfig array
  $ExistingConfig = Get-Content -Path $LocalFilePath
  # If the 3CX local config file already exists, then write the user values
  # into the correct lines in the array and output the file again. This results
  # in an unchanged 3CX config file.  If it doesn't exist, it'll write the
  # $3CXconfig array to the correct location. Some other values will need set
  # (e.g. Playback devices)
  if (Test-Path $LocalFilePath){
    $ExistingConfig[2] = $3CXConfig[2]
    $ExistingConfig[4] = $3CXConfig[4]
    $ExistingConfig[5] = $3CXConfig[5]
    $ExistingConfig[6] = $3CXConfig[6]
    $ExistingConfig[7] = $3CXConfig[7]
    $ExistingConfig | Out-File -Filepath $LocalFilePath -Encoding Default
  } else {
    $3CXconfig | Out-File -Filepath $LocalFilePath -Encoding Default
  }
}

问题在于所有哈希之间的界限。无论我尝试什么,Get-Content命令都不会在指定位置读取INI文件。路径就是这样:C:UsersUser NameAppdataLocal3CX Voip Phone3CXVoipPhone.ini。因此,环境变量零件(%localappdata%)和应用程序特定部分(3CX Voip Phone)中都有空间。

我使用了许多卷曲括号,引号,加法符号(+),Join-Path的组合,尽管其中许多正确编译,但没有一个最终都带有填充的$ExistingConfig

错误消息是:

不能将索引索引到空数组中。

很明显,该数组为null,因为Get-Content命令没有填充它。

只是为了确认,$3CXConfig变量的Get-Content命令可完美地工作,并从网络显示整个文件。将变量设置为扩展名变量的行也可以正常工作。

OP已确认问题是 empty (Zero-byte)$LocalFilePath文件。

换句话说: $LocalFilePath 存在,但恰好具有(这是一个零字节[ - 尺寸]文件)。

如果您使用Get-Content读取零字节文件的内容 - 例如,
$ExistingConfig = Get-Content $LocalFilePath-结果是$null不是 a an 空数组)。

因此,任何尝试 index 的尝试(使用下标)结果变量 - 例如,$ExistingConfig[2]-使PowerShell报告错误Cannot index into a null array.

最新更新