我正在处理一个批处理文件,它将:运行powershell - > import activedirectory - >放置查询 - >将结果写入文本文件,最终将被Splunk摄取。
我目前的PowerShell脚本是:
get-adcomputer -filter 'enabled -eq "true"' | select DistinguishedName | export-csv -append -path &"D:\Ingest Files\output.txt"
如果我先说:
,这在cmd中工作正常powershell -noexit import-module activedirectory
但是,当我将它放入批处理脚本时,根据看似正确的语法,它会抛出一个错误,即不允许使用&符号:
powershell -noexit import-module activedirectory -command "& {get-adcomputer -filter 'enabled -eq "true"' | select DistinguishedName | export-csv -append -path &"D:\Ingest Files\output.txt"}"
经过多次修补后,我发现这并不是错误,但是关闭cmd并且实际上没有写入文件:
powershell -noexit import-module activedirectory; get-adcomputer -filter 'enabled -eq "true"' | select DistinguishedName | export-csv -append -path &"D:\Ingest Files\output.txt"
我觉得我几乎没有遗漏语法,但它似乎无法诊断。我认为问题出现是因为我试图导入模块,但当我删除该部分时,我仍然不允许使用&符号。有没有更好的方法来做到这一点,或者我只是需要一个小调整?
答案 0 :(得分:1)
我找到了解决方法。我创建了一个包含以下内容的PowerShell脚本:
import-module activedirectory; get-adcomputer -filter 'enabled -eq "true"' | select DistinguishedName | export-csv -append -path D:\Ingest\test.txt
我的批处理脚本是:
powershell -noexit -file d:\ingest\ou.ps1
答案 1 :(得分:0)
理论上,假设脚本本身是正确的,您应该能够这样做:
powershell -noprofile -command "& { import-module activedirectory; get-adcomputer -filter 'enabled -eq "true"' | select DistinguishedName | export-csv -append -path 'D:\Ingest Files\output.txt' }"
引用可能仍存在问题,但可以使用转义字符轻松修复