脚本绕过我的for循环来搜索有多少个文件夹

时间:2018-08-24 10:30:12

标签: powershell for-loop

因此,当我昨天去所有工作正常时,我为公司创建了一个应用程序,现在我看到了防病毒脚本块,当我使用个人PC进行工作时,他通过了我的循环

Get-ChildItem -Path $folderpath -Include $filetype -recurse | 
ForEach-Object `
{
 $Result ++
}

所以这是我代码的一部分,并且被忽略了

另一个循环是当我添加其他.xls的信息时创建.xls文件

抱歉,我的英语是比利时人

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles()


    #region begin GUI{ 

        $Form                            = New-Object system.Windows.Forms.Form
        $Form.ClientSize                 = '400,400'
        $Form.text                       = "Fichier Provision CP"
        $Form.TopMost                    = $false
        $Icon = New-Object system.drawing.icon ("C:\Users\KILLIAN\Desktop\SAMPLE\ico.ico")
        $Form.Icon = $Icon

        $ProgressBar1                    = New-Object system.Windows.Forms.ProgressBar
        $ProgressBar1.width              = 230
        $ProgressBar1.height             = 10
        $ProgressBar1.location           = New-Object System.Drawing.Point(80,360)

        $Label1                          = New-Object system.Windows.Forms.Label
        $Label1.text                     = ""
        $Label1.AutoSize                 = $true
        $Label1.width                    = 80
        $Label1.height                   = 10
        $Label1.location                 = New-Object System.Drawing.Point(230,330)
        $Label1.Font                     = 'Microsoft Sans Serif,10'

        $Button1                         = New-Object system.Windows.Forms.Button
        $Button1.text                    = "Entrer"
        $Button1.width                   = 90
        $Button1.height                  = 29
        $Button1.location                = New-Object System.Drawing.Point(151,282)
        $Button1.Font                    = 'Microsoft Sans Serif,10'

        $Label2                          = New-Object system.Windows.Forms.Label
        $Label2.text                     = "Mois de fin"
        $Label2.AutoSize                 = $true
        $Label2.width                    = 25
        $Label2.height                   = 10
        $Label2.location                 = New-Object System.Drawing.Point(15,100)
        $Label2.Font                     = 'Microsoft Sans Serif,10'

        $Label3                          = New-Object system.Windows.Forms.Label
        $Label3.text                     = "Nom du fichier"
        $Label3.AutoSize                 = $true
        $Label3.width                    = 25
        $Label3.height                   = 10
        $Label3.location                 = New-Object System.Drawing.Point(15,60)
        $Label3.Font                     = 'Microsoft Sans Serif,10'

        $Label4                          = New-Object system.Windows.Forms.Label
        $Label4.text                     = "Jour férié"
        $Label4.AutoSize                 = $true
        $Label4.width                    = 25
        $Label4.height                   = 10
        $Label4.location                 = New-Object System.Drawing.Point(15,140)
        $Label4.Font                     = 'Microsoft Sans Serif,10'

        $TextBox2                        = New-Object system.Windows.Forms.TextBox
        $TextBox2.multiline              = $false
        $TextBox2.width                  = 200
        $TextBox2.height                 = 20
        $TextBox2.location               = New-Object System.Drawing.Point(145,100)
        $TextBox2.Font                   = 'Microsoft Sans Serif,10'

        $TextBox1                        = New-Object system.Windows.Forms.TextBox
        $TextBox1.multiline              = $false
        $TextBox1.width                  = 200
        $TextBox1.height                 = 20
        $TextBox1.location               = New-Object System.Drawing.Point(145,60)
        $TextBox1.Font                   = 'Microsoft Sans Serif,10'

        $TextBox3                        = New-Object system.Windows.Forms.TextBox
        $TextBox3.multiline              = $false
        $TextBox3.width                  = 200
        $TextBox3.height                 = 20
        $TextBox3.location               = New-Object System.Drawing.Point(145,140)
        $TextBox3.Font                   = 'Microsoft Sans Serif,10'

        $Form.controls.AddRange(@($ProgressBar1,$Label1,$Button1,$Label2,$Label3,$TextBox1,$TextBox2,$Label4,$TextBox3))


    #Var of the code{
        #C:\Users\student\Desktop\SAMPLE\*\Congé
        #G:\LUXDIVISION\SDE\COLLABORATEURS\*\Congé
        $folderpath = "C:\Users\KILLIAN\Desktop\SAMPLE\test\*\Congé"


        $Exl = New-Object -ComObject "Excel.Application"
        $Exl.Visible = $false
        $Exl.DisplayAlerts = $false

        $objExcel = new-object -comobject excel.application  
        $objExcel.Visible = $false  

        $filetype ="*xls"

        $Result = 0
        $Counter = 0
        $xlContinuous = 1
        $xlThick = 4
        $xlInsideVertical = 11
        $xlEdgeBottom = 9
        $a = 1







    #Calling function}
    function monthnamev (){

    $boolean = $false
            if($month -eq 3){
             $boolean = $True
            }
            elseif($month -eq 6){
             $boolean = $true
            }
            elseif($month -eq 9){
             $boolean = $true
            }
            elseif($month -eq 12){
             $boolean = $true
            }
    return $boolean

        }
    function monthname (){

    $boolean = $false
            if($month -eq 3){
             $namemonth = "Mars"
            }
            elseif($month -eq 6){
             $namemonth = "Juin"
            }
            elseif($month -eq 9){
             $namemonth = "Septembre"
            }
            elseif($month -eq 12){
             $namemonth = "Décembre"
            }
    return $namemonth

        }

    function isNumeric ($x) {
    try {
    0 + $x | Out-Null
    return $true
    } catch {
    return $false
    }
    }


    #Write your logic code here


    $Form.Add_Shown({
        $Year = Get-Date -UFormat "%Y"
       $date = Get-Date -UFormat "%m %Y"
       $TextBox1.Text = "Provision CP FIN $date"
    })



    Get-ChildItem -Path $folderpath -Include $filetype -recurse | 
    ForEach-Object `
    {
    $Result ++
    }

    $Button1.Add_Click({ 
    #Var for the textbox
                $namews = $TextBox1.Text
                $month = $TextBox2.Text
                $pholiday = $TextBox3.Text  

                #Public holiday


       if (isNumeric($pholiday) -eq $true){






        monthname
        if (-not (monthnamev)){
        [System.Windows.MessageBox]::Show('Erreur vous avez entrer une valeur incorrecte!', 'Error !', 0, 16) 
            }
        else{

                #Create a new .xls
                $objWorkbook = $objExcel.Workbooks.Add()  
                $objWorksheet = $objWorkbook.Worksheets.Item(1)

            Get-ChildItem -Path $folderpath -Include $filetype -recurse | 
            ForEach-Object `
            {




                 ## -- Calculate The Percentage Completed 
                    $Counter++ 
                    [Int]$Percentage = ($Counter/$Result)*100 
                    $ProgressBar1.Value = $Percentage
                    $Form.Refresh()
                    $Label1.Text = "Encodage : $Counter/$Result" 



                 #Open the Collab holiday .xls
                    $workbook = $Exl.workbooks.open($_.fullname)





                #Copy the name of the employee
                    $workbook.sheets.item(1).activate()
                    $WorkbookTotal=$workbook.Worksheets.item(1)
                    $value = $WorkbookTotal.Cells.Item(2, 2)
                    $value.Text

                    $objWorksheet.Activate()
                    $objWorksheet.Cells.Item($a, 1)  =  $value
                     Write-Warning $value.text


                #Search Solde of previous year
                    $lastsheet = $workbook.Worksheets|Select -Last 1
                    $lastsheet.Activate()
                    $FoundAS = $lastsheet.Cells.Find('Solde')
                    $RowAS =$FoundAS.Row
                    $ColumnAS = $FoundAS.Column
                    [int]$RowAS2 = $RowAS
                    [int]$ColumnAS2 = $ColumnAS
                    $ColumnAS2 ++
                    $value = $lastsheet.cells.item($RowAS2,$ColumnAS2)


                    if (isNumeric($value) -eq $true){
                    $objWorksheet.Activate()
                    $objWorksheet.Cells.Item($a, 2)  =  $value
                    }
                    else{
                    $value = 0
                    $objWorksheet.Activate()
                    $objWorksheet.Cells.Item($a, 2)  =  $value
                    }





                #Search Holyday 
                    $Found = $lastsheet.Cells.Find('Congés $Year')
                    $Row =$Found.Row
                    $Column = $Found.Column
                    [int]$Row2 = $Row
                    [int]$Column2 = $Column
                    $Column2 ++


                #Search Total 
                    $Foundto = $lastsheet.Cells.Find('Total')
                    $Rowto =$Foundto.Row
                    $Columnto = $Foundto.Column
                    [int]$Rowto2 = $Rowto
                    [int]$Columnto2 = $Columnto
                    $Columnto2 ++





                #Search month
                    $FoundSolde = $lastsheet.Cells.Find((monthname))
                    $RowSolde =$FoundSolde.Row
                    $ColumnSolde = $FoundSolde.Column
                    [int]$RowSolde2 = $RowSolde
                    [int]$ColumnSolde2 = $ColumnSolde
                    $ColumnSolde2 = $ColumnSolde2 + 5

                    $Soldemois = $lastsheet.cells.item($RowSolde2,$ColumnSolde2)
                    [double]$Soldemois2 = $Soldemois.text         

                    if (isNumeric($Soldemois2) -eq $true){
                    $objWorksheet.Activate()
                    }
                    else{
                    [double]$Soldemois2 = 0
                    $objWorksheet.Activate()
                    }


                #Search the correct value
                    $SoldePre = $lastsheet.cells.item($RowAS2,$ColumnAS2)
                    $total = $lastsheet.cells.item($Rowto2,$Columnto2)
                    $Holiday = $lastsheet.cells.item($Row2,$Column2)





                [double]$total2 = $total.text
                [double]$SoldePre2 = $SoldePre.text
                [double]$Holiday2 = $Holiday.text

                Write-Warning "Solde Précedent : $SoldePre2"
                Write-Warning "Solde du fin de mois : $Soldemois2"
                Write-Warning "le total de congé : $total2"
                Write-Warning ""


                $objWorksheet.Activate()

                $objWorksheet.Cells.Item($a, 2)  =  $SoldePre2  
                $objWorksheet.Cells.Item($a, 3)  =  "=($Holiday2/12) * $month"
                $objWorksheet.Cells.Item($a, 4)  =  "=$total2 - $Soldemois2"

                $cell2 = $objWorksheet.Cells.Item($a, 3)  
                $cell3 = $objWorksheet.Cells.Item($a, 4)
                [double]$celld2 = $cell2.text
                [double]$celld3 = $cell3.text

                $objWorksheet.Cells.Item($a, 5)  =  "=($SoldePre2 + $celld2 - $celld3) + $pholiday"



                $workbook.save()

                $workbook.close()

                $a ++
            }

            $objWorkbook.saveas("C:\Users\KILLIAN\Desktop\SAMPLE\$namews.xls")
            $objWorkbook.close() 

            $objExcel.Quit()
            $objExcel = $null

            $Exl.Quit()
            $Exl = $null
         }
        }

        else{
                    [System.Windows.MessageBox]::Show('Le Jour Férié doit être un chiffre !!!', 'Jour Férié incorrect', 0, 16) 
                    }


    })




    $Form.ShowDialog()

这是我的全部代码

Here is when I launch the debbuger

And this is when I execute the second step (I tap F10)

When I launch your line

And the line on the cmd

0 个答案:

没有答案