【问题标题】:How to output multiple variables in one line如何在一行中输出多个变量
【发布时间】:2019-08-28 05:56:30
【问题描述】:

我正在尝试确定 CSV 中的用户是否处于活动状态,此外,我想知道他们是基于 OU 的服务帐户、用户帐户还是机器帐户。

一切都在膨胀,直到我尝试输出它......输出在多行(每个 var 一个)。

我希望输出在一行上(中间有逗号,所以我完成后会有一个 CSV)...我已经尝试了这 20 种不同的方法,得到了 40 种不同的结果 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
}

【问题讨论】:

  • 也许here 会有所帮助

标签: powershell scripting


【解决方案1】:

要关注问题的一般标题(请参阅底部以了解特定案例中的最佳解决方案):

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

在以下示例中,假设值 'foo''bar''baz' 分别作为变量 $a$b$c 的值,您可以使用以下(解构)分配:$a, $b, $c = 'foo', 'bar', 'baz'.

  • 使用 -join 运算符
PS> $a, $b, $c -join ','
foo,bar,baz

这种方法的优点是可以将任意大小的数组用作 LHS。

  • 使用可扩展字符串"...",字符串插值),就像在您自己的解决方案中一样:
PS> "$a,$b,$c"
foo,bar,baz
  • 使用-f,字符串格式化操作符
PS> '{0},{1},{2}' -f $a, $b, $c
foo,bar,baz

至于你尝试了什么

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

注意使用单个管道。

【讨论】:

  • 很高兴听到这个消息,@MarcEverlove。循环内的错误只有在 terminating 错误时才会成为问题,您可以使用try { ... } catch { ... } 处理该错误。将foreach 循环视为一个表达式,其多个输出会自动收集到一个数组中,这比在循环内使用+= 构建一个数组要高效得多。同样,在所有输出上调用Export-Csv一次,在循环外比在循环内使用Export-Csv -Append 快得多。
【解决方案2】:

好的,我解决了,它作为一个数组会更好,但它在这里(完成了“错误处理”):

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

ForEach ($User in $Users){
$properties = $null

$properties = Get-ADUser -Identity $User | select 
SamAccountName,Enabled,DistinguishedName 

If($Properties){

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" }


$Sammy = $Properties.SamAccountName
$Enabled = $properties.Enabled

"$Sammy, $Enabled, $base" | out-file =  C:\User_Status_OU.csv -Append

}
Else {

$Sammy = "No Record"
$Enabled = "No Record"
$base = "No Record"


"$Sammy, $Enabled, $base" | out-file =  C:\User_Status_OU.csv -Append
}

【讨论】:

  • 有什么理由不使用对象吗?您正在手动构建您的 CSV 文件...如果您将其交给结构化对象而不是字符串,Export-CSV cmdlet 将为您处理。
  • 嗯,很可能;键盘后面的螺母松了……我知道有更好的方法,那只是我能想到的最权宜之计……
  • 哈! [grin] 仍然,你的解决方案是......脆弱的。另外,您离构建对象并发送到 CSV 仅一步之遥。
  • 就在我尝试这个位之前,但它不能正常工作,输出是相同的对象一遍又一遍......不知道我做错了什么,或者它是否每次都写入数组一个错误,但输出超过了一个演出,而不是几个 mb... :$Poop = @{ Name = $properties.SamAccountName Status = $properties.Enabled Type = $base} $results += New-Object psobject -房产 $Poop $Result | Export-Csv C:\UserStatus-OU3.csv -Append
  • 您发送"$Sammy, $Enabled, $base" 的地方很容易变成一个对象,并将这些值分配给属性。然后您可以将每个发送到 CSV,或者将它们收集到一个集合中并一步发送出去。第二种方法会更快,因为文件写入往往相当慢......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-06
相关资源
最近更新 更多