如何在一行中输出多个变量

Tec*_*now 9 powershell scripting

我试图确定 CSV 中的用户是否处于活动状态,此外,我想知道它们是否是服务帐户、用户帐户或基于 OU 的计算机帐户。

一切都在膨胀,直到我尝试输出它......输出分为几行(每个变量一行)。

我希望输出在一行上(中间有逗号,这样我完成后就会有一个 CSV)...我已经尝试了二十种不同的方法,得到了四十种不同的结果 O_o。

奖励:为任何产生错误的名称添加一条消息(即用户不存在...)我正在考虑一个 if/else,但无法获得基本的输出行为。

我尝试过用括号括起来,连接变量,嵌套变量……用棍子敲打我的电脑……静静地思考我的人生选择……

$Users = Get-Content C:\ActiveUsers.txt

ForEach ($User in $Users){
$properties = Get-ADUser -Identity $User | select SamAccountName,Enabled,DistinguishedName 
if (Select-String -Pattern "UserMgt" -InputObject $properties) { $base = "User" }
if (Select-String -Pattern "ApplSec" -InputObject $properties) { $base = "Service Account" }
if (Select-String -Pattern "WkstnMgt" -InputObject $properties) { $base = "Machine Account" }
write-output $properties.SamAccountName $properties.Enabled $base
#$Output = Write-Output $properties.SamAccountName $properties.Enabled $base 
#$Output #| out-file  C:\UserStatus-OU2.csv -append
}
Run Code Online (Sandbox Code Playgroud)

mkl*_*nt0 8

重点关注您问题的一般标题(请参阅底部以了解您具体情况的最佳解决方案):

给定多个变量,例如,,,,$a如何将它们输出为单行字符串,并带有可配置的分隔符,例如?$b$c,

在以下示例中,假设值'foo''bar'、分别作为变量、和 的'baz'值,您可以使用以下(解构)赋值来创建它们:。$a$b$c$a, $b, $c = 'foo', 'bar', 'baz'

  • 使用-join运算符
PS> $a, $b, $c -join ','
foo,bar,baz
Run Code Online (Sandbox Code Playgroud)

这种方法的优点是可以像 LHS 一样处理任意大小的数组。

  • 使用可扩展字符串"..."字符串插值),如您自己的解决方案所示:
PS> "$a,$b,$c"
foo,bar,baz
Run Code Online (Sandbox Code Playgroud)
  • 使用-f,字符串格式化运算符
PS> '{0},{1},{2}' -f $a, $b, $c
foo,bar,baz
Run Code Online (Sandbox Code Playgroud)

至于你尝试过的

Write-Output $properties.SamAccountName $properties.Enabled $base

传递多个参数将它们一一Write-Output写入管道;如果这些参数是字符串,则每个字符串都打印在自己的行上,如果将输出发送到/或,这也适用。Out-File>Set-Content


也就是说,由于您正在为CSV 文件创建行,所以最好创建自定义对象来表示行并将它们序列化到文件中Export-Csv(基于您问题中的代码,而不是您的答案:

Get-Content C:\ActiveUsers.txt | 
  ForEach-Object {
    $properties = Get-ADUser -Identity $_ | Select-Object SamAccountName, Enabled, DistinguishedName 

    # Consider using `elseif` here, unless you really want to evaluate
    # all conditions every time.
    # Also, it's better to check a specific property value rather than
    # searching the string representation of the entire object, e.g.
    #   if ($properties.DistinguishedName -match 'UserMgmt') ...
    if (Select-String -Pattern "UserMgt" -InputObject $properties) { $base = "User" }
    if (Select-String -Pattern "ApplSec" -InputObject $properties) { $base = "Service Account" }
    if (Select-String -Pattern "WkstnMgt" -InputObject $properties) { $base = "Machine Account" }

    # For each user, output a custom object.
    # The custom objects' property names becomes the CSV column headers
    # when Export-Csv processes the outputs.
    [pscustomobject] @{
      SamAccountName = $properties.SamAccountName
      Enabled = $properties.SamAccountName
      Base = $base
    }

  } | Export-Csv -NoTypeInformation C:\UserStatus-OU2.csv
Run Code Online (Sandbox Code Playgroud)

请注意单个管道的使用。


nav*_*eed 7

我意识到这是一个老问题,但它可能会帮助那些正在寻找简单解决方案的人。这个语法对我来说要简单得多:

Write-Output "$($var1) $($var2)"
Run Code Online (Sandbox Code Playgroud)