Передача аргумента из командной строки в сценарий PowerShell - PullRequest
2 голосов
/ 01 февраля 2020

Кто-то любезно написал для меня сценарий powershell. Я абсолютный любитель программирования. Мне не удалось связаться с этим пользователем, чтобы внести изменения, которые я хотел бы внести, поэтому я сам решил изменить скрипт.

По сути, я хочу передать аргумент, два каталога папок. , так что скрипт может генерировать ха sh данные как из источника, так и из назначения, а затем сравнивать их. Сценарий прекрасно работает для этого. Но я хочу проверить несколько источников и мест назначения.

Я провел поиск в Google и обнаружил, что должно работать следующее:

powershell.exe -Command 'C:\temp\hashcheck.ps1' -SourceDir "D:\Temp\Public" -DestDir "D:\Temp\Public2"

Где "sourcedir" и "destdir" - это имена из строк, которые я хочу передать. Но когда я запускаю его из командной строки администратора, он выдает ошибку «неожиданный токен -SourceDir»

В скрипте powershell уже есть пара параметров аргументов, но я добавил «sourcedir» и «destdir» следующим образом:

            param (

                [Parameter(Mandatory=$True)]
                [String]
                $BaseDirectory,

                [Parameter(Mandatory=$True)]
                [String]
                $SourceDir,

                [Parameter(Mandatory=$True)]
                [String]
                $DestDir,

                [Parameter(Mandatory=$True)]
                [ValidateScript({!(Test-Path -path $_ -Type Leaf) })]
                [String]
                $OutputFilePath

            )

и затем передать этот аргумент в следующих командах:

        # Run this for Source
        Get-TreeCheckSum -BaseDirectory "$SourceDir" -OutputFilePath C:\temp\PublicSource.xml -Verbose

        # Run this for Destination
        Get-TreeCheckSum -BaseDirectory "$DestDir" -OutputFilePath C:\temp\PublicDestination.xml -Verbose

Спасибо за любую помощь. Возможно, это простое решение, но, как я уже сказал, я полностью любитель PowerShell. Заранее спасибо.

РЕДАКТИРОВАТЬ: Вот полный код, может быть, будет иметь больше смысла. Я в основном хочу передать имя папки в разделах «# Run this for source» и «#Run this for Destination», назначенных «BaseDirectory»

        # Created by P33500k for fun..
        # Use at your own risk follow directions at end to run the commands
        function Get-TreeCheckSum {
            [CmdletBinding()]
            param (

                [Parameter(Mandatory=$True)]
                [String]
                $BaseDirectory,

                [Parameter(Mandatory=$True)]
                [ValidateScript({!(Test-Path -path $_ -Type Leaf) })]
                [String]
                $OutputFilePath


            )

            begin {
                # Define $results as an array of objects
                $results = @()


            }

            process {
                Write-Verbose "Getting all files in $BaseDirectory..."
                # check files
                $allfiles = Get-ChildItem -path $BaseDirectory -Force -File -Recurse | select-object Name,FullName,Length

                Write-Verbose "Found $($allfiles.count) files in $BaseDirectory..."

                $n = 0
                Foreach ($File in $allfiles) {
                    $n++
                    Write-Progress -Activity "CheckSumming $BaseFolder" -Status "Checksumming $($File.FullName)" -PercentComplete (($n / ($allfiles | measure-object | Select-object -ExpandProperty Count)) * 100)
                    Write-Verbose "Checksumming $($file.fullname)"
                    $tempobj = New-Object -TypeName psobject -Property @{
                        'FullPath' = $file.FullName
                        'FileName' = $file.Name
                        'Size' = $file.Length
                        'Hash' = (Get-FileHash -Path $file.fullname | select-object -ExpandProperty Hash)
                        'RelativePath' = ($file.fullname.substring($BaseDirectory.Length))
                    }
                    $results += $tempobj
                    $tempobj = $null
                }
                Write-Verbose "Hashing Complete for file set in $BaseDirectory"

            }

            end {
                Write-Verbose "Writing Output File to $OutputFilePath"
                $results | Export-Clixml -Path $OutputFilePath
            }
        }

        # Run this for Source
        Get-TreeCheckSum -BaseDirectory "D:\Temp\Public" -OutputFilePath C:\temp\PublicSource.xml -Verbose

        # Run this for Destination
        Get-TreeCheckSum -BaseDirectory "D:\Temp\Public2" -OutputFilePath C:\temp\PublicDestination.xml -Verbose


        # Run this code for comparing

        # Global variables for comparing
        $Filesnotondestination = ".\FilesNotonDestination.txt"
        $Filesnotonsource = ".\FilesNotonSource.txt"
        $FailedHashes = ".\Failedchecksums.txt"

        $Source = import-clixml C:\temp\PublicSource.xml
        $Destination = import-clixml C:\temp\PublicDestination.xml

        Write-Host "Comparing objects.."
        $compare = Compare-Object -ReferenceObject $Source -DifferenceObject $Destination -Property RelativePath -IncludeEqual
        Write-Host "Writing file names to files only on source.."
        $compare | Where-object {$_.SideIndicator -eq "<="} | Select-object -expandproperty RelativePath | Set-content -path $Filesnotondestination
        Write-Host "Writing file names to files only on destination.."
        $compare | Where-object {$_.SideIndicator -eq "=>"} | Select-object -expandproperty RelativePath | Set-content -path $Filesnotonsource
        Write-Host "Comparing files on both source and destination... "
        $verify = $compare | Where-object {$_.SideIndicator -eq "=="}
        Write-Host "Found $($verify.count) files that are in both paths.." 
        $n = 0
        foreach ($v in $verify) {
            $n++
            Write-progress -Activity "Comparing file hashes.." -Status "Processing File: $($v.relativepath)" -PercentComplete (($n / ($verify | measure-object | Select-object -ExpandProperty Count)) * 100)
            #Write-Host "Comparing $($v.relativepath).."
            #Write-Host $v.relativepath
            Foreach ($s in $source) {
                If ($s.relativepath -eq $v.relativepath) {
                    $sourceh = $s.hash
                    #Write-Host "Source Hash: $Sourceh"
                }
            }
            Foreach ($d in $Destination) {
                If ($d.relativepath -eq $v.relativepath) {
                    $desth = $d.hash
                    #Write-Host "Destination Hash: $desth"
                }
            }
            If ($desth -eq $sourceh) {
                #Write-Host "File hashed match"
            }
            Else {
                Write-Warning "$($v.relativepath) - File Hashes Do Not Match"
                Add-content -path $FailedHashes -value "$(get-date) - $($v.relativepath) failed checksum: $sourceh not equal to $desth"
            }
        }

Ответы [ 2 ]

1 голос
/ 01 февраля 2020

Вы не хотите вызывать параметр BaseDirectory. Итак, измените исходный сценарий ps1 на

Param (
  [string]$BaseDirectory,
  # The Rest of the script

Он не будет запрашивать базовый каталог, поскольку он больше не является обязательным.

Теперь вызовите сценарий с остальными параметрами.

1 голос
/ 01 февраля 2020

Попробуйте вызвать скрипт напрямую так:

C:\temp\hashcheck.ps1 -SourceDir "D:\Temp\Public" -DestDir "D:\Temp\Public2"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...