在powershell中循环

时间:2017-12-13 13:46:26

标签: powershell

Hello Guys需要一些帮助,脚本提示:

$path = ".\" # path do txt
$server = "server"  # server.txt
$paczki = ".\paczki\"
$missingi = "$path\$server.txt"  
$plik = get-content $missingi

foreach ($j in $plik) {
    Write-Output "1"
    $wynik = Get-ChildItem "$paczki" | ? {$_.name -match "$j"} 
    if ($wynik -eq $null) {
        # Write-Host $i 
    }
    else {  
        Write-Output "2"
        Write-Host $wynik "znaleziono"   
        Copy-Item $paczki\$wynik  -Destination \\$server\c$\temp\ -force 
    }
}

#### BAT GENERATOR #####
Write-Output "3"
#  & .\bat_generator.ps1  

$zapis = "$path\test.bat" 
"pushd %~dp0" > $zapis 
$nazwa = Get-ChildItem  "\\$server\c$\temp\" | select name 

foreach ($i in $nazwa) {
    $text = $i.name + " /norestart /quiet" 
    $text >> $zapis
}
"ppd0" >> $zapis # dodaj ppd0

move-item -path .\test.bat -destination \\$server\c$\temp\ -Force # skopiuj .bat na server

首先我创建一个名为server的文件,例如server.txt在这个服务器中我们有KB列表。脚本在文件夹paczki中搜索该KB存在,如果是,则在服务器中复制它并创建.bat

我想添加自动搜索所有.txt文件,例如server.txt,& server1.txt并在循环中使用它,我想到了类似的东西:

$pliki_txt= Get-ChildItem $path -Filter "*.txt" |  % {$_.BaseName}

并把它放在循环中,但它没有真正起作用,我尝试在这个地方添加循环:

for ($i in pliki_txt)
    $path = ".\" # path do txt
     $server="server"  # server.txt
    $pliki_txt= Get-ChildItem $path -Filter "*.txt" |  % {$_.BaseName}
            (....)

我做错了什么?有没有更简单的方法?脚本仅在我手动设置$server $server="serwer"时设置socket

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

$path = ".\"

Get-ChildItem $path -Filter *.txt |  %{ 
    $content = Get-content $_.FullName
    Foreach($server in $content){
        write-host $server
    }
}

答案 1 :(得分:0)

如果我做对了,这里的问题是你没有按照正确的顺序排列。

从原始代码中我将更改以下内容

def pipe(self, docs, batch_size=1000, n_threads=2):
    """Match a stream of documents, yielding them in turn.
    docs (iterable): A stream of documents.
    batch_size (int): Number of documents to accumulate into a working set.
    n_threads (int): The number of threads with which to work on the buffer
        in parallel, if the implementation supports multi-threading.
    YIELDS (Doc): Documents, in order.
    """
    for doc in docs:
        self(doc)
        yield doc

这样您就可以检查该路径中的每个服务器* .txt文件并相应地处理它。

或者你甚至可以把它变成像这样的参数化脚本

$path = ".\" # path do txt
$server = "server"  # server.txt
$paczki = ".\paczki\"
# $missingi = "$path\$server.txt"  
$missingi = Get-ChildItem -Path $path -Filter server*.txt | Select -ExpandProperty Name
foreach ($m in $missingi) {
    $plik = get-content $m
    ( ... )
}

然后你会像这样运行它:

Param(
    [Parameter(Mandatory = $true)]
    [String]$path,
    [Parameter(Mandatory = $true)]
    [String]$pattern,
    [Parameter(Mandatory = $true)]
    [String]$packzi
)
$missingi = Get-ChildItem -Path $path -Filter *.txt | Select -ExpandProperty | Select-String "$pattern"
foreach ($m in $missingi) {
    $plik = get-content $m
    foreach ($j in $plik) {
        Write-Output "1"
        $wynik = Get-ChildItem "$paczki" | ? {$_.name -match "$j"} 
        if ($wynik -eq $null) {
            # Write-Host $i 
        }
        else {  
            Write-Output "2"
            Write-Host $wynik "znaleziono"   
            Copy-Item $paczki\$wynik  -Destination \\$server\c$\temp\ -force 
        }
    }

    #### BAT GENERATOR #####
    Write-Output "3"
    #  & .\bat_generator.ps1  

    $zapis = "$path\test.bat" 
    "pushd %~dp0" > $zapis 
    $nazwa = Get-ChildItem  "\\$server\c$\temp\" | select name 

    foreach ($i in $nazwa) {
        $text = $i.name + " /norestart /quiet" 
        $text >> $zapis
    }
    "ppd0" >> $zapis # dodaj ppd0

    move-item -path .\test.bat -destination \\$server\c$\temp\ -Force # skopiuj .bat na server
}

如果您想要更改源路径,名称模式或搜索补丁,这将为您提供更大的灵活性。

我希望这会有所帮助。