有人请帮我整理一个powershell脚本。我是编程的业余爱好者。我无法联系到该用户进行所需的更改,因此我自己决定自己修改脚本。
基本上,我想传递一个参数,两个文件夹目录,以便脚本可以从源和目标生成哈希数据,然后进行比较。脚本可以正常工作。但是我想检查多个来源和目的地。
我已经进行了一些谷歌搜索,发现以下应该可以工作:
powershell.exe -Command 'C:\temp\hashcheck.ps1' -SourceDir "D:\Temp\Public" -DestDir "D:\Temp\Public2"
“ sourcedir”和“ destdir”是我要传递的字符串的名称。但是,当我从admin命令提示符运行它时,它给我错误“意外令牌'-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"
}
}
答案 0 :(得分:1)
尝试像这样直接调用脚本:
C:\temp\hashcheck.ps1 -SourceDir "D:\Temp\Public" -DestDir "D:\Temp\Public2"
答案 1 :(得分:1)
您不想调用BaseDirectory参数。因此,将源ps1脚本更改为
Param (
[string]$BaseDirectory,
# The Rest of the script
它不再要求基本目录,因为它不再是必需的。
现在使用调用脚本 其余参数。