【发布时间】:2012-03-14 04:33:01
【问题描述】:
我在这里检查了很多其他问题,但找不到我需要的东西。我目前正在尝试自动化找出哪些虚拟机在哪些 Hyper V 服务器中的过程。这是我当前的脚本。
@echo off
set /P dc= "Which DC do you want? "
dsquery computer -name %DC%VUT* > dsquery.txt
type dsquery.txt | findstr /v IMAGINGOU | findstr /v OTHEROU > dsquery2.txt
powershell -command Get-Content dsquery2.txt | ForEach-Object { $_ -replace '"CN=', "" } | Set-Content dsquery3.txt
powershell -command Get-Content dsquery3.txt | ForEach-Object { $_ -replace ',OU=CAM,OU=Exchange,OU=Server,DC=RED001,DC=local"', "" } | Set-Content serverlist.txt
powershell -command .\Get-HyperVMod.ps1 -file output.csv
我遇到的问题是我可以从 (Win2k3) 执行 dsquery 的服务器无法执行 ForEach-Object,因为它们具有旧版本的 Powershell,而可以执行 ForEach-Object 的 Win2k8 服务器则无法执行做一个 dsquery。
这是一个 dsquery 输出示例,我试图解析并删除除服务器名称之外的所有内容。
"CN=SERVER001,OU=VUT,OU=Infastructure,OU=Server,DC=dcname,DC=local"
"CN=SERVER008,OU=ImagingWDS,DC=red002,DC=local"
输出应该是
SERVER001
SERVER008
关于使用其他方法执行此操作的任何建议,或者只是我从文件中删除多余文本的小问题。
【问题讨论】:
标签: powershell batch-file cmd batch-processing