简体   繁体   English

PowerShell Write-EventLog CmdLet手动工作,而不是在脚本中工作

[英]PowerShell Write-EventLog CmdLet works manually, not in script

I am writing a PowerShell script to disable and move users with in AD. 我正在编写PowerShell脚本来禁用和移动AD中的用户。 this part works great. 这部分效果很好。 However I would like to log each event in the event log. 但是,我想将每个事件记录在事件日志中。 The cmdlet Write-EventLog works fine when typed manually. 手动键入时,cmdlet Write-EventLog可以正常工作。

Write-EventLog –LogName ManageAD –Source “ManageUsers Script” –EntryType Information –EventID 1 –Message “This is a test message"

However, when I add the cmdlet to the script it errors. 但是,当我将cmdlet添加到脚本时会出错。

Write-EventLog –LogName ManageAD –Source “ManageUsers Script” –EntryType Information –EventID 1 –Message “User ” + $SAM + " has been disabled"

Write-EventLog : A positional parameter cannot be found that accepts argument 'Scriptâ??'.
At C:\test\ManageUsers.ps1:12 char:16
+     Write-EventLog <<<<  â?"LogName ManageAD â?"Source â?oManageUsers Scriptâ?? â?"EntryType Information â?"EventID
 â?"Message â?oUser â?? + $SAM + " has been disabled"
    + CategoryInfo          : InvalidArgument: (:) [Write-EventLog], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.WriteEventLogCommand

Any suggestions are appreciated. 任何建议表示赞赏。

Want to say the simple answer is you have smart quotes in your code. 想说一个简单的答案是您的代码中有智能引号。 Those came from where you originally copied that code or a product of your testing environment. 它们来自您最初复制该代码或测试环境产品的位置。 Either way you need to watch out for those buggers. 无论哪种方式,您都需要当心那些bug。

Write-EventLog –LogName ManageAD –Source “ManageUsers Script” –EntryType Information –EventID 1 –Message “User ” + $SAM + " has been disabled"

Should be instead. 应该代替。

Write-EventLog –LogName ManageAD –Source "ManageUsers Script" –EntryType Information –EventID 1 –Message "User " + $SAM + " has been disabled"

I would like to point out from the error that I see other artifacts as well... 我想从错误中指出我也看到了其他工件...

â?" LogName ManageAD â?" –“ LogName ManageAD –”

It makes me wonder if the hyphens are the long ones like an em-dash (Comes back to an earlier question about your editor choice!). 这使我想知道连字符是否是长的连字符,如破折号(回到前面有关编辑器选择的问题!)。

See the difference subtle here between the hyphen and 2 dashes - – —. 看到连字符和两个破折号之间的细微差别---。 Also have your dash against the basic hyphen. 还要对基本连字符加破折号。 The one on the right is the hyphen. 右边的是连字符。

在此处输入图片说明

Was the code working inside the ISE maybe and you were running the script in the regular console? 代码是否可以在ISE中运行,并且您是在常规控制台中运行脚本的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM