术语“keytool”未被识别为 cmdlet 的名称

问题描述 投票:0回答:0

我正在拼命尝试为我使用的程序自动化安装过程。该过程的一部分涉及编辑密钥库文件以删除密钥对,我已经验证 keytool 可以很好地处理这个问题。我写了两个 PowerShell 脚本。第一个编辑所有 Windows 设置,安装所有必需的服务器角色/功能,并创建一个计划任务以在下次重新启动后登录时运行第二个 Powershell 脚本。

第二个脚本运行我正在安装的应用程序的 *.exe 文件,然后编辑注册表以确保 javahome 指向正确的位置,然后它尝试使用以下命令编辑密钥库:

keytool -delete -noprompt -alias *.network.internal -Keystore $keystore -Storepass $mypwd

当我运行脚本时,我得到:

keytool : The term 'keytool' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Path\Script.ps1:150 char:13
+             keytool -delete -noprompt -alias *.network.inter ...
+             ~~~~~~~
    + CategoryInfo          : ObjectNotFound: (keytool:String) [], CommandNotFoundException                                 + FullyQualifiedErrorId : CommandNotFoundException 

只是为了笑,我打开了 PowerShell 并自行运行了该行,它运行正常。然后我决定尝试将第二个脚本分成两个脚本。一个运行安装程序,另一个处理证书。然后我创建了一个 bat 文件来分别调用每个脚本。我得到了同样的错误。然后我分别运行这两个脚本,并且没有问题。

我很好奇是否有人对我的问题可能在这里有任何想法。如有必要,我可以发布我的整个代码,但是更改名称以保护罪犯需要一段时间。

编辑:这是我的代码的精简版

#Mount ISO to next available drive letter
    $availableDrive = (68..90 | %{$L=[char]$_; if ((gdr).Name -notContains $L) {$L}})[0]
    $driveLetter = "${availableDrive}:\"
    $diskImg = Mount-DiskImage "C:\Software\Application.ISO" -NoDriveLetter -PassThru
    $volInfo = $diskImg | Get-Volume
    mountvol $driveLetter $volInfo.UniqueId

#Install *.exe from newly mounted drive
    $InstallerFolder = "${availableDrive}:\Software"
    Invoke-Expression -Command "$InstallerFolder\Application.exe"
    Pause

#Update registry to set javahome
    $regFilePath = "C:\Software\Support Tools\KeyStore Explorer"
    regedit.exe /S $regFilePath\Set_Java_Path_for_KSE.reg

#Backup Keystore File

    Copy-Item D:\Folder\Application.keystore -Destination D:\Folder\Application.keystore.bak

#Edit keystore to delete *.network.internal
    keytool -delete -noprompt -alias *.network.internal -Keystore D:\Folder\Application.keystore -Storepass Password123

Write-host "done"
Pause

我第一次运行它时会抛出 keytool 错误。如果我第二次运行它,它会通知我 Application.exe 已经安装,但是 keytool 命令运行没有问题。

powershell keystore keytool
© www.soinside.com 2019 - 2024. All rights reserved.