Вы можете добавить функцию для проверки файла:
function Test-FileSafety {
# This function simply returns $true when the file is ok or the user decided to
# go ahead and run it even though he/she was warned.
# If the user decided it was too tricky and bailed out, the function returns $false
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[Alias ('Path')]
[string]$FileName
)
Add-Type -AssemblyName System.Windows.Forms
$letsGo = $true
# first test if the file was downloaded from internet (and was not already unblocked)
if (Get-Item $FileName -Stream zone*) {
$message = "Run only scripts that you trust.`r`n"
$message += "While scripts from the Internet can be useful this script can potentially harm your computer.`r`n`r`n"
$message += "Do you want to allow '$FileName' ?"
$result = [System.Windows.Forms.MessageBox]::Show($message, "Security Warning", 3)
# evaluate the users response and act upon it
switch ($result) {
Yes { <# user wants to run #> Unblock-File -Path $FileName ; break }
No { <# user decided not to run the script #> ; $letsGo = $false; break }
Cancel { <# user bailed out #> ; $letsGo = $false; break }
}
}
# next test if the file is digitally signed or not
if ($letsGo -and (Get-AuthenticodeSignature -FilePath $FileName).Status -eq 'NotSigned') {
$message = "Run only scripts that you trust.`r`n"
$message += "The script is not digitally signed.`r`n`r`n"
$message += "Do you still want to run '$FileName' ?"
$result = [System.Windows.Forms.MessageBox]::Show($message, "Security Warning", 3)
# evaluate the users response and act upon it
switch ($result) {
Yes { <# user wants to run even though the script is not digitally signed #> ; break}
No { <# user decided it was too dangerous and does not want to run the script #> ; $letsGo = $false; break}
Cancel { <# user bailed out #> ; $letsGo = $false; break}
}
}
return $letsGo
}
и использовать его как:
if ((Test-FileSafety -FileName "C:\temp\anz.ps1")) {
# run the script
}
else {
# do nothing, the user cancelled
}