在PowerShell的批处理文件 [英] powershell in batch file
问题描述
我试图创建一个批处理脚本包含PowerShell的,但它不工作,如下
I'm trying to create a batch script with powershell included, but it does not work, as below
@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可以发现 这里
感谢您的帮助。
But, it worked very well if I did run individual command from the above batch script using cmd. The temp.csv can be found here Thanks for your help.
推荐答案
好吧,你原来的剧本是可怕的低效和严重的设计。这里是PowerShell来你想要做什么,不需要的临时文件。通常情况下,我不会有这个几乎很多的意见,但我想,以确保每个人都明白我做了什么。输出文件将是从那里执行这种方法的工作目录。
Ok, your original script was horribly inefficient and badly designed. Here is the powershell to do what you want, no temp files needed. Normally, I wouldn't have nearly this many comments, but I want to make sure everyone understands what I did. The output files will be in the working directory from where you execute this method.
使用(在cmd中/批):PowerShell的-command&放大器; {\\ FileContainsThisFunction.ps1;提取,主机original.csv}
Usage(in cmd/batch): powershell -command "& { . \FileContainsThisFunction.ps1; Extract-Hosts original.csv }"
使用(在PowerShell中)。 \\ FileContainsThisFunction.ps1;提取物 - 主机original.csv
Usage(in powershell): . \FileContainsThisFunction.ps1; Extract-Hosts original.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 }
}
编辑:在第二个内容除了固定的错字,它应该已经阅读OutWks.Content + = $ _
编辑+:替换神奇在哪里用foreach,使其更容易理解;新增外空共进晚餐后,管道preSS输出。
Fixed typo in second Content addition, it should have read OutWks.Content += $_ Edit+: Replaced Where magic with Foreach to make it easier to understand; Added Out-Null to suppress output after pipeline.
我们希望,该脚本将带你一步步接近到PowerShell的著述丰富管线处理器。
Hopefully, this script will take you one step closer to writing rich pipeline processors in powershell.
这篇关于在PowerShell的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!