首页 文章

将PowerShell变量输出到文本文件

提问于
浏览
21

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

7 回答

  • 7

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

    用这个:

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

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

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

    "$computer,$Speed,$Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200
    
  • -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"
    

    干杯

  • 2

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

    假设你有两个变量:

    $computers = "pc1,pc2"
    $users = "user1,user2"
    

    要将它们保存到文本文件:

    $computers,$users | out-file c:\youfilename.txt
    

    有关Out-File的更多信息,请在shell中键入:

    Get-Help Out-File
    

相关问题