小编Jel*_*phy的帖子

Security.SecureString参数不接受字符串

将字符串作为参数传递给此参数时:

[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[Security.SecureString]$password=$(Throw "Password required.")
Run Code Online (Sandbox Code Playgroud)

使用-password参数时出现此错误.

无法将System.String类型转换为System.Security.SecureString类型

如果我没有传递-password参数,则会显示提示并接受输入.

passwords powershell parameter-passing

6
推荐指数
1
解决办法
9718
查看次数

以 24 小时格式显示 [datetime] 对象

我有一些代码检查有效日期,简单的例子:

[datetime]::ParseExact(
    '201809222130',
    'yyyyMMddHHmm',
    [System.Globalization.CultureInfo]::InvariantCulture
)
Run Code Online (Sandbox Code Playgroud)

这输出:

Saturday, September 22, 2018, 9:30:00 PM
Run Code Online (Sandbox Code Playgroud)

我正在尝试以 24 小时格式显示小时,所需的输出:

Saturday, September 22, 2018, 21:30:00 PM
Run Code Online (Sandbox Code Playgroud)

可以使用Get-DateCmdlet 显示 24 小时格式,例如:Get-Date -UFormat %R,但我在创建对象时无法使用它[datetime]

如何显示24小时格式?

powershell datetime

3
推荐指数
2
解决办法
3万
查看次数

从外部 Windows 命令实用程序捕获异常

我试图在运行外部命令时终止脚本导致错误。考虑这个简单的代码:

try {
    where.exe Test-App
}

catch {
    Write-Error "Exception caught." -ErrorAction Continue
    throw
}

Write-Host "Test message!"
Run Code Online (Sandbox Code Playgroud)

输出:

where.exe : INFO: Could not find files for the given pattern(s).
At line:2 char:5
    where.exe Test-App
...
Test message!
Run Code Online (Sandbox Code Playgroud)

当外部命令导致错误时,是否可以进入 catch 块并抛出?

期望的输出:

C:\Scripts\Test-Script.ps1 : Exception caught.
Run Code Online (Sandbox Code Playgroud)

powershell exception-handling

2
推荐指数
1
解决办法
1414
查看次数