批处理文件中的powershell
我正在尝试创建一个包含powershell的批处理脚本,但它不起作用,如下所示批处理文件中的powershell,powershell,batch-file,Powershell,Batch File,我正在尝试创建一个包含powershell的批处理脚本,但它不起作用,如下所示 @echo off rename D:\temp\*.csv temp.csv powershell.exe -Command "& {Import-Csv D:\temp\temp.csv | select ip,hostname | Export-Csv -Path D:\temp\temp01.csv –NoTypeInformation}" del /F /S /Q D:\temp\temp.csv
@echo off
rename D:\temp\*.csv temp.csv
powershell.exe -Command "& {Import-Csv D:\temp\temp.csv | select ip,hostname | Export-Csv -Path D:\temp\temp01.csv –NoTypeInformation}"
del /F /S /Q D:\temp\temp.csv
powershell -command "& {Rename-Item D:\temp\temp01.csv D:\temp\temp.txt}"
type D:\temp\temp.txt | findstr /v n/s | findstr /v n/a | findstr /v Hostname >> D:\temp\temp01.txt
del /F /S /Q D:\temp\temp.txt
rename D:\temp\temp01.txt temp.txt
powershell -command "& {Rename-Item D:\temp\temp.txt dad.csv}"
powershell -command "& {Get-Content D:\temp\dad.csv | where {$_ -match 'LWKS'} | Set-Content D:\temp\lwks.csv}"
powershell -command "& {Get-Content D:\temp\dad.csv | where {$_ -match 'WKS'} | Set-Content D:\temp\wks.csv}"
exit
但是,如果我使用cmd从上面的批处理脚本中运行单个命令,那么效果会非常好。
可以找到temp.csv
谢谢您的帮助。好的,您的原始脚本效率极低,而且设计糟糕。这是一个powershell,您可以随心所欲,不需要临时文件。通常,我不会有这么多评论,但我想确保每个人都理解我所做的。输出文件将位于执行此方法的工作目录中 用法(在cmd/batch中):powershell-command“&{.\FileContainsThisFunction.ps1;Extract Hosts original.csv}” 用法(在powershell中):\FileContainsThisFunction.ps1;提取原始主机.csv
function Extract-Hosts {
param(
[Parameter(Mandatory=$true)]
[String]$InputFile
)
if(-not (Test-Path $InputFile)) { throw ("Input file doesn't exist: {0}" -f $InputFile) }
# Extract filename without path or extension
$BaseName = Get-Item $InputFile | Select -ExpandProperty Basename
# Create a custom object that conains the outfilename and the content for lwks and wks
$OutLwks = @{ File = ('{0}-lwks.csv' -f $Basename); Content = @() }
$OutWks = @{ File = ('{0}-wks.csv' -f $Basename); Content = @() }
# First, delete the output files if they exist
$OutLwks, $OutWks | ForEach { if (Test-Path -Path:($_.File)) { Remove-Item $_.File } }
# Import the original csv into the pipeline
Import-Csv $InputFile |
# We only care about the IP and Hostname columns
Select -Property IP, Hostname |
# Where the hostname is not empty, nor contains n/a or n/s
Where { $_.Hostname -iNotMatch '(^$|n/a|n/s)' } |
ForEach-Object {
# If it contains lwks, add it to that list
if ($_ -imatch 'lwks') {
($OutLwks.Content += $_)
}
# if it contains wks but NOT lwks, add to the other list
elseif ($_ -imatch 'wks') {
($OutWks.Content += $_)
}
} | Out-Null # Sends objects to null after pipeline processing.
# Splat each one into the appropriate CSV file
$OutLwks, $OutWks | ForEach-Object {
$_.Content | Export-Csv -Path $_.File -NoTypeInformation }
}
编辑:修正了第二次添加内容时的打字错误,它应该已经读过了。内容+=$_
编辑+:用Foreach替换magic,使其更容易理解;添加Out Null以抑制管道后的输出
希望此脚本能让您更接近于在powershell中编写富流水线处理器。脚本是如何工作的?我的最终目标是:#1、删除标题名为“Ping”和“Ports”的列+删除temp.csv文件中包含字符串“[n/a]”和“[n/s]”“Hostname”的行>>将其保存到dad.csv#2,在新的dad.csv文件中,删除任何不包含“lwks”>>>保存到lwks.csv的行,对任何不包含“wks”>>>的行执行相同的操作将其保存到wks.csv#3,由于脚本编写方面的知识有限,我无法将它们全部放在PowerShell中,感谢您的帮助Hi Eris,您的脚本工作得很好,但只完成了一半。之后生成了两个文件:original-lwks.csv(谢谢,100%完成)+original-wks.csv(空内容=0%)。请帮我拿这个箱子,谢谢!没有任何更改,请尝试运行您的脚本,原始-dad.csv仍然有空内容。谢谢太棒了,它工作得非常好,多亏了10亿厄里斯。老实说,你能再给我一次你的手吗,对于这两个文件original-lwks.csv和original-wks.csv,请帮我将没有标题的IP地址列提取到txt文件,例如:original-lwks.txt和original-wks.txt这将是一个不同的问题。上面的脚本以书面形式回答了问题。是的,无论如何,这对我来说已经足够了。非常感谢你的帮助,埃里斯。