powershell从脚本到日志文件的输出结果

powershell从脚本到日志文件的输出结果,powershell,powershell-2.0,powershell-3.0,Powershell,Powershell 2.0,Powershell 3.0,我有一个简单的Powershell代码,可以在服务器启动时ping服务器,然后禁用本地管理员帐户。如何将结果输出到日志文件,以便记录已禁用的内容 这就是我目前所拥有的 $computers = Get-ADComputer -filter {OperatingSystem -like "Windows Server*"} | ForEach ($computer in $computers) { $rtn = Test-Connection -CN $computer -Cou

我有一个简单的Powershell代码,可以在服务器启动时ping服务器,然后禁用本地管理员帐户。如何将结果输出到日志文件,以便记录已禁用的内容

这就是我目前所拥有的

$computers = Get-ADComputer -filter {OperatingSystem -like "Windows Server*"} | 
ForEach ($computer in $computers) {     
   $rtn = Test-Connection -CN $computer -Count 1 -BufferSize 16 -Quiet    
  IF($rtn -match 'True') {
    write-host -ForegroundColor green $computer | Disable-localUserAccount -ComputerName $computer -username Administrator
  } ELSE {
      Write-host -ForegroundColor red $computer
  }     
}

写入主机
直接写入控制台。该输出无法重定向到文件。如果要将输出重定向到文件,请将其替换为“写入输出”,并删除花哨的颜色。此外,我还将计算机列表导入一个
ForEach对象
循环,这样您就可以直接将输出写入文件。和
测试连接
返回一个布尔值,因此您可以在条件中直接使用它:

$computers | % {
  if (Test-Connection -CN $_ -Count 1 -BufferSize 16 -Quiet) {
    Write-Output "$_ online"
    Disable-localUserAccount -ComputerName $_ -username Administrator
  } else {
    Write-Output "$_ offline"
  }     
} | Out-File 'C:\path\to\your.log'

您在尝试写入文件时尝试了什么?使用PowerShell执行此操作非常简单,有多种方法。我已经尝试过此方法,但不确定这是正确的方法$outputstring=$computer$outputstring-join“,”>>C:\Create\u Adminuser\u computers.csv