将PowerShell变量输出到文本文件

32
我刚开始学习PowerShell,有一个脚本通过遍历Active Directory搜索特定计算机。我获取了几个变量,然后运行函数来检查像WMI和注册表设置这样的东西。
在控制台中,我的脚本运行良好,简单的Write-Host命令可以按照我想要的方式将数据打印到屏幕上。我知道在使用通道时可以使用Export-Csv…但我不想从通道中打印输出。
我想将变量写入文本文件,继续循环,并检查AD中的下一个计算机…在下一行输出相同变量的下一个迭代。以下是我的写入-主机(Write-Host)命令:
Write-Host ($computer)","($Speed)","($Regcheck)","($OU)

输出文件:

$computer,$Speed,$Regcheck | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200

它给出了数据,但每个变量都在自己的行上。为什么?我想让所有变量在一行上用逗号分隔。是否有类似于VB writeline的简单方法可以做到这一点?我的PowerShell版本似乎是2.0。
6个回答

37

使用这个:

"$computer, $Speed, $Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200

1
这应该是被接受的答案。它干净简洁。我敢打赌大多数人都没有注意到将变量用双引号括起来才是关键。 - D-Klotz

32

我通常在这些循环中构建自定义对象,然后将这些对象添加到一个数组中,方便我进行操作、排序、导出至CSV等操作:

# Construct an out-array to use for data export
$OutArray = @()

# The computer loop you already have
foreach ($server in $serverlist)
    {
        # Construct an object
        $myobj = "" | Select "computer", "Speed", "Regcheck"

        # Fill the object
        $myobj.computer = $computer
        $myobj.speed = $speed
        $myobj.regcheck = $regcheck

        # Add the object to the out-array
        $outarray += $myobj

        # Wipe the object just to be sure
        $myobj = $null
    }

# After the loop, export the array to CSV
$outarray | export-csv "somefile.csv"

2
当我运行这个程序时,$computer没有显示出机器名称?它在CSV中显示为“System.DirectoryServices.PropertyValueCollection”。 - user3067193
好的,我不知道你的$computer变量是在哪里生成的,但是你所引用的类有一个ToString方法,所以你可以尝试这样做: $myobj.computer = $computer.ToString()。如果这样不行,我需要知道你在$computer变量中具体放了什么。 - Trondh
1
这个有效!谢谢。顺便说一下...计算机变量是直接从AD中提取的http://technet.microsoft.com/en-us/library/ff730959.aspx - user3067193
太好了!我猜out-file执行隐式字符串转换,而export-csv则不会。无论如何,这种技术将允许您切分和处理数据,并且更加“面向对象”,所以据我所知,这是做事情的“正确”方式(至少在我的脑海中:-)) - Trondh

9
您可以使用PowerShell的`-join`运算符将值数组连接在一起。以下是一个示例:
$FilePath = '{0}\temp\scripts\pshell\dump.txt' -f $env:SystemDrive;

$Computer = 'pc1';
$Speed = 9001;
$RegCheck = $true;

$Computer,$Speed,$RegCheck -join ',' | Out-File -FilePath $FilePath -Append -Width 200;

输出

pc1,9001,True

这对我起作用了,但是我必须在连接字符串周围使用双引号。"," - seagulledge

2

$computer,$Speed,$Regcheck将创建一个数组,并对每个变量运行out-file,它们会得到单独的行。如果您首先使用这些变量构建单个字符串,则它将显示为单个行。就像这样:

"$computer,$Speed,$Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200

1
简单的解决方案是在管道到Out-File之前避免创建数组。PowerShell的第一条规则是逗号是一个特殊的分隔符,默认行为是创建一个数组。连接的方法如下。
$computer + "," + $Speed + "," + $Regcheck | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200

这创建了一个包含三个项的数组。
$computer,$Speed,$Regcheck
FYKJ
100
YES

与由逗号分隔的三个项连接而成的字符串相比。

$computer + "," + $Speed + "," + $Regcheck
FYKJ,100,YES

1

我在谷歌搜索中找到了这里。为了表示善意,我已经包含了我从这段代码和其他代码中拼凑出来的内容。

# This script is useful if you have attributes or properties that span across several commandlets
# and you wish to export a certain data set but all of the properties you wish to export are not
# included in only one commandlet so you must use more than one to export the data set you want
#
# Created: Joshua Biddle 08/24/2017
# Edited: Joshua Biddle 08/24/2017
#

$A = Get-ADGroupMember "YourGroupName"

# Construct an out-array to use for data export
$Results = @()

foreach ($B in $A)
    {
  # Construct an object
        $myobj = Get-ADuser $B.samAccountName -Properties ScriptPath,Office
  
  # Fill the object
  $Properties = @{
  samAccountName = $myobj.samAccountName
  Name = $myobj.Name 
  Office = $myobj.Office 
  ScriptPath = $myobj.ScriptPath
  }

        # Add the object to the out-array
        $Results += New-Object psobject -Property $Properties
        
  # Wipe the object just to be sure
        $myobj = $null
    }

# After the loop, export the array to CSV
$Results | Select "samAccountName", "Name", "Office", "ScriptPath" | Export-CSV "C:\Temp\YourData.csv"

干杯


问题在于这个输出是CSV格式,而请求需要的是一个平面文件,不是CSV。 - user2883951

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接