windows - Cannot convert the value of type "System.String" to type "System.Security.SecureString&

I made a script to unlock bitlocker volumes using the password manager autotype feature instead of the

I made a script to unlock bitlocker volumes using the password manager autotype feature instead of the clipboard. It uses gsudo and needs some features available only in 7.4.

$DriveLetter = Read-Host "Drive letter"
$DriveLetter = ($DriveLetter + ":")
$EncUserCredential = Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
gsudo pwsh.exe -CommandWithArgs '`$parm1 = ConvertTo-SecureString -String `$args[1]; Unlock-Bitlocker -MountPoint `$args[0] -Password `$parm1' $DriveLetter $EncUserCredential

When I run the script directly in a 7.4.6 shell I get:

Drive letter: w
Enter Password: ********************
Unlock-BitLocker: Cannot process argument transformation on parameter 'Password'. Cannot convert the value of type "System.String" to type "System.Security.SecureString".

Conversely, if I run it in 5.1 (7.4 is invoked later in the script) it works, why? This forces me to keep 5.1 as the default powershell for my terminal, not a big deal but still... Also, the policy for unsigned scripts is only applied to 5.1, it seems to me that this makes the setting somewhat less relevant.

I made a script to unlock bitlocker volumes using the password manager autotype feature instead of the clipboard. It uses gsudo and needs some features available only in 7.4.

$DriveLetter = Read-Host "Drive letter"
$DriveLetter = ($DriveLetter + ":")
$EncUserCredential = Read-Host "Enter Password" -AsSecureString | ConvertFrom-SecureString
gsudo pwsh.exe -CommandWithArgs '`$parm1 = ConvertTo-SecureString -String `$args[1]; Unlock-Bitlocker -MountPoint `$args[0] -Password `$parm1' $DriveLetter $EncUserCredential

When I run the script directly in a 7.4.6 shell I get:

Drive letter: w
Enter Password: ********************
Unlock-BitLocker: Cannot process argument transformation on parameter 'Password'. Cannot convert the value of type "System.String" to type "System.Security.SecureString".

Conversely, if I run it in 5.1 (7.4 is invoked later in the script) it works, why? This forces me to keep 5.1 as the default powershell for my terminal, not a big deal but still... Also, the policy for unsigned scripts is only applied to 5.1, it seems to me that this makes the setting somewhat less relevant.

Share Improve this question asked Nov 15, 2024 at 20:34 mehmeh 756 bronze badges 2
  • 1 Why do you escape dollars with Grave Accent inside the -CommandWithArgs single-quoted string? – JosefZ Commented Nov 16, 2024 at 20:14
  • @JosefZ: This need arises due to the PowerShell CLI call being passed to gsudo.exe; however, such an invocation is suboptimal and best avoided: in a PowerShell session, you can pass a piece of PowerShell code directly to gsudo.exe, and it is best to use a script block. – mklement0 Commented Nov 16, 2024 at 21:42
Add a comment  | 

1 Answer 1

Reset to default 1

From a PowerShell session, gsudo offers convenient syntax for invoking PowerShell commands directly, using a script block ({ ... }) and optional arguments, which can be passed as an array to the -Args parameter:

gsudo { 
  $parm1 = ConvertTo-SecureString -String $args[1]
  Unlock-Bitlocker -MountPoint $args[0] -Password $parm1
} -Args $DriveLetter, $EncUserCredential

Compared to your attempt to call pwsh, the PowerShell (Core) 7 CLI, explicitly, the above is:

  • more convenient
  • avoids escaping headaches
  • supports returning typed data, within the usual constraints of PowerShell's cross-process serialization infrastructure (see this answer for background information)

As for what you tried:

  • By passing an explicit pwsh CLI call to gsudo, you're complicating the escaping needs even further (compared to passing just a single argument containing PowerShell code as a string) and, for reasons unknown to me, the -Command (-c) parameter behaves differently from the (v7.4+) -CommandWithArgs (-cwa) parameter.

    • E.g., gsudo pwsh -c ' `$HOME + """!"""; Get-Date ' works (but note the obscure escaping requirements),
      whereas the equivalent gsudo pwsh -cwa ' `$HOME + """!"""; Get-Date ' does not (note the missing ! in the output).
  • If you were to pass the command string alone - in which case no extra escaping is required - things would be simpler (e.g., gsudo ' $HOME + "!"; Get-Date '), but then you couldn't pass arguments separately, the way you tried via -CommandWithArgs

  • However, given the simpler and more robust alternative based on script blocks shown at the top, this isn't a problem that needs solving.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745672421a4639485.html

相关推荐

  • Doxygen常用注释规则

    Doxygen简介Doxygen 是一款在软件开发中广泛使用的文档生成工具。它通过解析类、函数和变量的相关信息,自动从源代码注释生成文档,并以 HTML 和 PDF 等格式输出。通过简化和标准化文档流程,Doxygen 加强了不同编程语言和

    1小时前
    00
  • 聊聊四种实时通信技术:短轮询、长轮询、WebSocket 和 SSE

    这篇文章,我们聊聊 四种实时通信技术:短轮询、长轮询、WebSocket 和 SSE 。1 短轮询浏览器 定时(如每秒)向服务器发送 HTTP 请求,服务器立即返回当前数据(无论是否有更新)。优点:实现简单,兼容性极佳缺点:高频请求浪费资源

    1小时前
    10
  • C#高性能开发之类型系统:从C# 7.0 到C# 14的类型系统演进全景

    自C# 7.0以来,C#语言在类型系统方面引入了众多新数据类型、类型构造和语言特性,以提升性能、类型安全性和开发效率。本文全面整理了从C# 7.0到C# 14.0(截至2025年4月,C# 14.0为预览版)类型系统的新增内容,包括值元组、

    1小时前
    20
  • 解码NVIDIA RecSys

    在信息爆炸的时代,推荐系统已成为互联网世界的"数字导购员"。从电商平台"猜你喜欢"到短视频平台的"无限下拉",这些看似懂你的智能推荐背后,藏着怎样的技术魔法?NVIDIA开源的Re

    1小时前
    10
  • 修改 win10 右键“新建”菜单(原理、两种方法及注意事项)

    修改 win10 右键“新建”菜单操作原理注意事项其他方法参考 修改 win10 右键“新建”菜单 这件事,基本上是新装系统之后必调校的一件事,每次都要搜索一下。但是网络上搜索到的答案&

    1小时前
    10
  • Windows网络重置后无法连接网络

    Windows网络重置后重启电脑无法连接到网络 打开命令提示符快捷键winR输入msconfig点击确定 在系统配置中找到服务先全部禁用,在全部启用,点击应用后重启电脑就可以了

    1小时前
    10
  • 更正

    之前文章 地表最强基准-ADR1001 中:这个地方有错误业界流量很高的号,百花潭也转载了,不改正的话变成了错误永流传。有读者留言说,恒温控制晶振(OCXO)也有类似的设计:对于某些应用,TCXO(温度补偿)的频率-温度稳定性指标仍无法满足

    1小时前
    10
  • 我把AI接上了Figma、WhatsApp、浏览器……然后它开始自己动起来了!

    大家好,你有没有幻想过这样一幕:你家的 AI 助手,突然接过你的手机,自己发了条微信。 紧接着,它点开了 Chrome,滑动了几下网页,做了个表单提交。然后它打开了 Figma,开始画 UI 界面。 最后,它还用自己的声音给人打了个电话,说

    1小时前
    00
  • 蜜罐检测

    核心功能功能模块技术实现URL去重输入文件哈希去重+结果文件增量校验智能限速令牌桶算法(支持动态时序调整)并发控制ThreadPoolExecutor线程池管理异常处理7大类错误分类统计(含QUIC协议错误) 蜜罐识别Cookie检测+备注

    1小时前
    00
  • Python+AI提示词出租车出行轨迹预测:梯度提升GBR、KNN、LR回归、随机森林融合及贝叶斯概率异常检测研究

    原文链接:tecdat?p=41693在当今数字化浪潮席卷全球的时代,城市交通领域的海量数据如同蕴藏着无限价值的宝藏等待挖掘。作为数据科学家,我们肩负着从复杂数据中提取关键信息、构建有效模型以助力决策的使命。我们团队承接并完成了一项极具挑

    1小时前
    00
  • 如何判断字符串是不是数字

    在编程的世界里,验证用户输入是否符合预期格式,是常见又必要的一环。一个常见的验证场景,就是检查某个字符串是否表示一个有效的数字。今天我们就来看看,如何用 Java 判断一个字符串是不是合法的数字格式。笨办法一个直接的方法是遍历字符串的每个字

    1小时前
    00
  • 手把手教你使用 mcp

    手把手教你使用 mcp-server —— vscode + 多款 MCP Server 实现高德地图旅游攻略生成并分享给其他人Author:GoritDate:2025年4月24日目标:看完这篇文章你就能学会如何使用调用高德地图 MC

    1小时前
    00
  • minio使用简介

    在云原生和微服务时代,对象存储已成为存储非结构化数据(如图片、日志、备份等)的首选方案。MinIO 是一款高性能、兼容 S3 API 的开源对象存储服务,而它的官方 Go SDK —— minio-go,则可以让你在 Go 语言项目中轻松集

    58分钟前
    00
  • Andorid平台实现高性能低延迟的多路RTSP播放器

    ​在当今的视频监控、流媒体传输等领域,RTSP(Real Time Streaming Protocol)协议被广泛用于音视频数据的实时传输。为了满足多路 RTSP 流的同时播放需求,基于大牛直播SDK开发了一款功能丰富、性能稳定的多路 R

    57分钟前
    00
  • Ascend 910b vllm运行报错: cannot import name 'log' from 'torch.distributed.elastic

    在Ascend 910b上运行vllm报错. ImportError: cannot import name 'log' from 'torch.distributed.elastic.

    52分钟前
    00
  • 安利一个超强的linux脚本

    想像一个场景,你突然接手管理一个系统,之前对接的人没有留下任何材料,主机上也没有历史命令能够看到之前做了哪些操作。怎么办?今天介绍一个简单的脚本记录linux历史执行记录的方法,可以在现有主机上配置,然后后面主机初始化都配置上。Linux命

    44分钟前
    00
  • 业内首次! 全面复现DeepSeek

    机器之心发布机器之心编辑部OpenAI 的 o1 系列和 DeepSeek-R1 的成功充分证明,大规模强化学习已成为一种极为有效的方法,能够激发大型语言模型(LLM) 的复杂推理行为并显著提升其能力。然而,这些推理模型的核心训练方法在其技

    41分钟前
    00
  • Rust 现状全揭示:开发者生产力提升,信心却在下滑?

    作者 | Tim Anderson 译者 | Sambodhi 策划 | Tina图片2024 年 Rust 现状调查结果已经发布,显示生产力有所提升,但编译慢和调试困难仍然是重大挑战。另一个令人关注的问题是,尽管 Rust

    30分钟前
    00
  • MobileNetV2:面向移动端的高效神经网络架构革新——突破轻量化模型的设计边界

    我们倾向于将“移动”与更小、更高效的事物联系起来,所以我原本以为这个网络的设计会占用更少的计算资源,而这正是作者的目标。这篇论文描述了一种专为移动和资源受限环境量身定制的全新神经架构——MobileNetV2。他们强调了MobileNetV

    22分钟前
    00
  • 从0到精通,System.Text.Json进阶技巧曝光,性能提升3倍!

    一、引言在现代软件开发中,JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,被广泛应用于前后端交互、配置文件管理以及分布式系统间的数据传输。System.Text.Json 是 .NET Core

    19分钟前
    00

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信