gpt4 book ai didi

windows - 将参数从命令行传递到 PowerShell 脚本

转载 作者:行者123 更新时间:2023-12-02 23:33:59 26 4
gpt4 key购买 nike

有人好心地为我整理了一个 powershell 脚本。我是一个完全的编程业余爱好者。我一直无法联系到该用户进行我想要的更改,因此我自己亲自修改了脚本。

基本上我想传递一个参数,两个文件夹目录,以便脚本可以从源和目标生成哈希数据,然后比较它们。该脚本可以正常工作。但我想检查多个来源和目的地。

我做了一些谷歌搜索,发现以下应该有效:

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

其中“sourcedir”和“destdir”是我要传递的字符串的名称。但是当我从管理命令提示符运行它时,它给了我错误“意外的 token '-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 完全是业余爱好者。提前致谢。

编辑:这是完整的代码,也许会更有意义。我基本上想在分配给“BaseDirectory”的“#Run this for source”和“#Run this for Destination”部分下传递文件夹名称
        # 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"
}
}

最佳答案

尝试像这样直接调用脚本:

C:\temp\hashcheck.ps1 -SourceDir "D:\Temp\Public" -DestDir "D:\Temp\Public2"

关于windows - 将参数从命令行传递到 PowerShell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018747/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com