【问题标题】:passing guid and path to bcdedit /set as a variable将 guid 和路径作为变量传递给 bcdedit /set
【发布时间】:2021-09-14 20:10:55
【问题描述】:

我需要运行这 4 个命令来将新映像引导到设备中:

bcdedit /copy {current} /d "Describe"
bcdedit /set {guid} osdevice vhd=somepath 
bcdedit /set {guid} device vhd=somepath
bcdedit /default {$guid} 

为了自动化我的脚本,我想提取作为第一个命令的输出返回的 guid/标识符,并将其作为参数传递给其他 3 个命令。 现在,我是这样做的:

$guid = Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock {cmd /c "bcdedit /copy {current} /d "Describe""}

#output
#$guid = This entry was successfully copied to {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

$guid = $guid.Replace("This entry was successfully copied to {","")
$guid = $guid.Replace("}","")

$path1 = "xxx/xxxx/...."

#passing the guid and path as inputs
Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock {cmd /c "bcdedit /set {$guid} osdevice vhd=$path1"}
Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock {cmd /c "bcdedit /set {$guid} device vhd=$path1"}
Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock {cmd /c "bcdedit /default {$guid} "}

但是,每次我得到一个错误:

元素数据类型无法识别,或不适用于指定条目。 运行“bcdedit /?”用于命令行帮助。 未找到元素

当我在 UI 中手动复制和粘贴路径时,这可以正常工作,但我不确定如何实现自动化。

【问题讨论】:

    标签: powershell automation powershell-remoting bcdedit


    【解决方案1】:
    • 正如this answer 对您之前的问题所解释的那样,无需使用cmd /c 来调用bcdedit - 只需引用 包含@ 的文字参数987654325@ 和 },因为这些字符在不带引号的情况下在 PowerShell 中具有特殊含义。

      • 通过cmd /c 调用不仅效率低下(尽管在实践中这并不重要),而且还会引起引用问题。[1]
    • 传递给Invoke-Command -ComputerName ... 的脚本块远程执行,因此无法访问调用者的变量;从调用者的作用域中包含变量值的最简单方法是通过$using: 作用域(参见this answer)。

    因此:

    $guid = Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock { 
      bcdedit /copy '{current}' /d "Describe" 
    }
    
    # Extract the GUID from the success message, which has the form
    # "This entry was successfully copied to {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
    $guid = '{' + ($guid -split '[{}]')[1] + '}'
    
    $path1 = "xxx/xxxx/...."
    
    # Passing the guid and path as inputs, which must
    # be done via the $using: scope.
    Invoke-Command -ComputerName $comp -Credential $cred -ScriptBlock { 
      bcdedit /set $using:guid osdevice vhd=$using:path1
      bcdedit /set $using:guid vhd=$using:path1
      bcdedit /default $using:guid
    }
    

    [1] 例如,cmd /c "bcdedit /copy {current} /d "Describe"" 并没有像你想象的那样工作;它必须是cmd /c "bcdedit /copy {current} /d `"Describe`""

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多