2

您好我正在尝试比较两个 csv 并查看它们之间的增量。我想在 csv 中创建一个新列来捕获比较旧数据和新数据提取的停用和新用户。

虽然我能够创建新列,但无法将值填充到新列中。这里 User_status 是我正在创建的列,它应该包含停用的值。

这是我查看停用用户的一小部分代码:

if ($csv2.UserID -match $user.UserID)
{
    Write-Verbose "$user.UserID is in both files"
}
else
{
    Write-Verbose "$user.UserID is missing from csv2"
    $missing += $user
    $user_status = 'Deactivated'
    #Set-Variable -Name "user_status" -Value "Deactivated"
    $missing | Select-Object *,@{Name='User_Status';Expression= { $_.user_status  } } |Export-Csv -path "C:\Users\test.csv" -NoTypeInformation 

}
4

1 回答 1

4

您设置了变量$user_status,然后$_在尝试调用它时添加。相反,只需使用您填充的变量

if ($csv2.UserID -match $user.UserID)
{
    Write-Verbose "$user.UserID is in both files"
}
else
{
    Write-Verbose "$user.UserID is missing from csv2"
    $missing += $user
    $user_status = 'Deactivated'
    #Set-Variable -Name "user_status" -Value "Deactivated"
    $missing | Select-Object *,@{Name='User_Status';Expression= { $user_status  } } |Export-Csv -path "C:\Users\test.csv" -NoTypeInformation 

}

正如@zett42 所指出的,基本变量在引号内可以很好地扩展,而访问变量的属性需要一个子表达式

工作正常

"string with a simple $variable"

需要子表达式

"string with complex $($variable.property)"
于 2021-03-10T21:55:42.020 回答