【问题标题】:Output PowerShell variables to a text file将 PowerShell 变量输出到文本文件
【发布时间】:2014-01-18 10:28:06
【问题描述】:

我是 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。

【问题讨论】:

    标签: powershell


    【解决方案1】:

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

    【讨论】:

    • 当我运行它时,$computer 没有显示机器名称?它在 CSV 中显示为“System.DirectoryServices.PropertyValueCollection”
    • 好吧,我不知道你的 $computer 变量是在哪里生成的,但是你所指的类有一个 ToString 方法,所以你可以这样做: $myobj.computer = $计算机.ToString()。如果这不起作用,我需要知道你在 $computer 变量中到底放了什么。
    • 这成功了!谢谢。仅供参考...计算机变量直接从 AD technet.microsoft.com/en-us/library/ff730959.aspx
    • 太棒了!我猜 out-file 会进行隐式字符串转换,而 export-csv 不会。无论如何,这种技术将允许您对数据进行切片和切块,并且更加“面向对象”,因此 AFAIK 这是做事的“正确”方式(至少在我的脑海中:-))
    【解决方案2】:

    使用这个:

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

    【讨论】:

    • 这应该是公认的答案。它干净简单。我敢打赌,大多数人都忽略了这样一个事实,即包裹变量的双引号是什么伎俩。
    【解决方案3】:

    简单的解决方案是避免在管道到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
    

    对比由逗号分隔的三个项目的串联。

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        • 这对我有用,但我必须在连接字符串周围使用双引号。 ","
        【解决方案6】:

        我在 Google 搜索中处于领先地位。出于善意,我已经包含了我从这段代码的一部分和我一路收集的其他代码拼凑而成的内容。

        # 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。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-24
        • 2019-10-11
        • 1970-01-01
        • 1970-01-01
        • 2020-07-02
        • 2019-01-24
        相关资源
        最近更新 更多