首页 > 解决方案 > System.IO.StreamWriter 不包含名为“writeline”的方法

问题描述

我正在使用 PowerShell 5.1,但 StreamWriter 有问题。我正在使用这段代码:

$ExportFile = [System.IO.StreamWriter]::new("c:\temp\test.txt")
$ExportFile.WriteLine("Date,User,MyTest")
$ExportFile.Close()

当我使用 PowerShell ISE 运行脚本时,它可以工作。但是,当我在 PowerShell 窗口中运行脚本时,它并没有我所拥有的:

Method invocation failed because [System.String] does not contain a method named 'WriteLine'.
At D:\Scripts\[AD MCO] Audit UsrCptr For Deletion\Audit_UsrCptr_For_Deletionv2.ps1:55 char:7
+       $ExportFile.WriteLine("Date,User, ...
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
PS D:\Scripts\[AD MCO] Audit UsrCptr For Deletion> host


Name             : ConsoleHost
Version          : 5.1.14393.3471
InstanceId       : 6904efae-80c0-4e51-a115-e2e580a644a7
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : fr-FR
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

标签: powershell

解决方案


我发现了“错误”。脚本中有一个特殊字符可以修改执行。我用记事本再次编写了脚本,现在一切正常。谢谢您的帮助 !


推荐阅读