English 中文(简体)
cm cm cm cm
原标题:Parsing and removing text with cmd or powershell

I ve checked quite a few other questions here but was unable to find what I need. I am currently trying to automate the process of finding out what VMs are in what Hyper V servers. Here is my current script.

@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

The problem I have hit is the servers that I can do dsquery from (Win2k3) can t do the ForEach-Object because they have an older version of Powershell and the Win2k8 servers that can do the ForEach-Object can t do a dsquery.

这里是我试图除服务器名称外复制和删除所有频率产出的样本。

"CN=SERVER001,OU=VUT,OU=Infastructure,OU=Server,DC=dcname,DC=local"
"CN=SERVER008,OU=ImagingWDS,DC=red002,DC=local"

Output should be

SERVER001
SERVER008

任何建议,要么采用另一种方法,要么只是我从档案中删除额外案文的小问题。

问题回答

run this against your dsquery.txt, it will parse what you need.

@echo off
setlocal enabledelayedexpansion
::Echos your Computer Name only.
for /F "tokens=1,2 delims==," %%G IN (dsquery.txt) DO echo %%H
pause
endlocal

基本上,它使用了两个不同的轨道:即“=”和“,”即刻在服务器名称上。 仅看第二点。 它根据你上述指示开展工作。





相关问题
complex batch replacement linux

I m trying to do some batch replacement for/with a fairly complex pattern So far I find the pattern as: find ( -name *.php -o -name *.html ) -exec grep -i -n hello {} + The string I want ...

How to split a string by spaces in a Windows batch file?

Suppose I have a string "AAA BBB CCC DDD EEE FFF". How can I split the string and retrieve the nth substring, in a batch file? The equivalent in C# would be "AAA BBB CCC DDD EEE FFF".Split()[n]

How to check which Operating System?

How can I check OS version in a batch file or through a vbs in an Windows 2k/2k3 environment ? You know ... Something like ... : "If winver Win2k then ... or if winver Win2k3 then ....

热门标签