我是PowerShell的新手,并且有一个脚本循环通过Active Directory搜索某些计算机.我得到几个变量,然后运行函数来检查WMI和注册表设置等内容.
在控制台中,我的脚本运行良好而简单的Write-Host命令可以根据需要在屏幕上打印数据.我在使用管道时知道Export-Csv ...但我不打算从管道打印.
我想将变量写入文本文件,继续循环,并检查AD中的下一台计算机...输出下一行相同变量的下一次迭代.这是我的写主持人:
Write-Host ($computer)","($Speed)","($Regcheck)","($OU)
Run Code Online (Sandbox Code Playgroud)
输出文件:
$computer,$Speed,$Regcheck | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200
Run Code Online (Sandbox Code Playgroud)
它给了我数据,但每个变量都在它自己的行上.为什么?我想用逗号分隔的一行上的所有变量.是否有一种类似于VB writeline的简单方法?我的PowerShell版本似乎是2.0.
Tro*_*ndh 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"
Run Code Online (Sandbox Code Playgroud)
CB.*_*CB. 21
用这个:
"$computer, $Speed, $Regcheck" | out-file -filepath C:\temp\scripts\pshell\dump.txt -append -width 200
Run Code Online (Sandbox Code Playgroud)
您可以使用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;
Run Code Online (Sandbox Code Playgroud)
产量
pc1,9001,True
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
202264 次 |
| 最近记录: |