powershell - 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
解决方案
我发现了“错误”。脚本中有一个特殊字符可以修改执行。我用记事本再次编写了脚本,现在一切正常。谢谢您的帮助 !
推荐阅读
- node.js - 如何检查 JSON 是否包含某些内容 NODEJS
- android - 来自分页库 3 的 PagingSource,结果为回调
- python - 在 PyCharm 上的单个项目上运行两个文件
- powershell - 嵌套的while循环只运行一次
- python - 使用全局变量创建新数据框
- macos - 在现有的 Flutter 项目上启用不同风格的桌面平台
- django - Django中的“模板树的顶层”在哪里?
- javascript - 是的,将最小最大日期与构建日期而不是当前日期进行比较
- python - 作为熊猫数据框中条目的列表的布尔掩码
- angular - pipe() 在 angularfire 中完成后如何调用函数