将PowerShell变量输出到文本文件

时间:2013-12-31 13:27:32

标签: powershell

我是PowerShell的新手,并且有一个脚本循环通过Active Directory搜索某些计算机。我得到几个变量然后运行函数来检查诸如WMI和注册表设置之类的东西。

在控制台中,我的脚本运行非常简单Write-Host命令可以根据需要在屏幕上打印数据。我在使用管道时知道Export-Csv ...但我不打算从管道打印。

我想将变量写入文本文件,继续循环,然后检查AD中的下一台计算机...输出下一行相同变量的下一次迭代。这是我的写主持人:

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 个答案:

答案 0 :(得分:23)

我通常在这些循环中构造自定义对象,然后将这些对象添加到我可以轻松操作,排序,导出为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"

答案 1 :(得分:18)

使用此:

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

答案 2 :(得分:7)

您可以使用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

答案 3 :(得分:2)

$computer,$Speed,$Regcheck将创建一个数组,并为每个变量运行out-file个=它们会分开。如果首先使用变量构造单个字符串,它将显示一行。像这样:

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

答案 4 :(得分: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"

干杯

答案 5 :(得分:0)

简单的解决方案是在管道到Out-File之前避免创建数组。 PowerShell的规则#1是逗号是一个特殊的分隔符,默认行为是创建一个数组。连接是这样完成的。

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

这将创建一个包含三个项目的数组。

$computer,$Speed,$Regcheck
FYKJ
100
YES

VS。用逗号分隔的三个项目的连接。

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