【问题标题】:Powershell Script is throws Casting Error in Windows 7 and NOT Windows 10Powershell 脚本在 Windows 7 而不是 Windows 10 中引发投射错误
【发布时间】:2016-04-26 13:15:52
【问题描述】:

首先,我将假设我面临的问题是一个 .Net 问题,因为我可以在 Windows 10 中成功运行该脚本,但在 Windows 7 上却不能。

以下是我遇到的错误。因此,我尝试通过在 $CharNum 变量周围放置“”(双引号)来解决该问题以纠正该问题。但是,它不起作用。

所以,如果有人能对这个问题有所了解并帮助我理解为什么这个脚本在 Windows 10 而不是 Windows 7 中有效,我将不胜感激。另外,如果您能帮助我更正代码,使其在两个版本的 Windows 中兼容,我们将不胜感激。

再次,我假设 .Net 是罪魁祸首。

function GetRandomPassword ([char] $length=10, $count=1) {

if ($length -lt 4) {return $null}

# Define list of numbers, this will be CharType 1
$numbers=$null
For ($a=48;$a –le 57;$a++) {$numbers+=,[char][byte]$a }

# Define list of uppercase letters, this will be CharType 2
$uppercase=$null
For ($a=65;$a –le 90;$a++) {$uppercase+=,[char][byte]$a }

# Define list of lowercase letters, this will be CharType 3
$lowercase=$null
For ($a=97;$a –le 122;$a++) {$lowercase+=,[char][byte]$a }

# Define list of special characters, this will be CharType 4
$specialchars=$null
For ($a=33;$a –le 33;$a++) {$specialchars+=,[char][byte]$a }
For ($a=63;$a –le 64;$a++) {$specialchars+=,[char][byte]$a }
For ($a=61;$a –le 61;$a++) {$specialchars+=,[char][byte]$a }
For ($a=45;$a –le 46;$a++) {$specialchars+=,[char][byte]$a }
For ($a=35;$a –le 38;$a++) {$specialchars+=,[char][byte]$a }
For ($a=42;$a –le 43;$a++) {$specialchars+=,[char][byte]$a }



# Need to ensure that result contains at least one of each CharType
# Initialize buffer for each character in the password
$Buffer = @()
For ($a=1;$a –le $length;$a++) {$Buffer+=0 }

# Randomly chose one character to be number
while ($true) {
$CharNum = (Get-Random -minimum 0 -maximum $length)
if ($Buffer["$CharNum"] -eq 0) {$Buffer["$CharNum"] = 1; break}
}

# Randomly chose one character to be uppercase
while ($true) {
    $CharNum = (Get-Random -minimum 0 -maximum $length)
    if ($Buffer["$CharNum"] -eq 0) {$Buffer["$CharNum"] = 2; break}
}

# Randomly chose one character to be lowercase
while ($true) {
    $CharNum = (Get-Random -minimum 0 -maximum $length)
    if ($Buffer["$CharNum"] -eq 0) {$Buffer["$CharNum"] = 3; break}
}

# Randomly chose one character to be special
while ($true) {
    $CharNum = (Get-Random -minimum 0 -maximum $length)
    if ($Buffer["$CharNum"] -eq 0) {$Buffer["$CharNum"] = 4; break}
}

# Cycle through buffer to get a random character from the available types
# if the buffer already contains the CharType then use that type
$Password = ""
foreach ($CharType in $Buffer) {
    if ($CharType -eq 0) {$CharType = ((1,2,3,4)|Get-Random)}
    switch ($CharType) {
        1 {$Password+=($numbers | GET-RANDOM)}
        2 {$Password+=($uppercase | GET-RANDOM)}
        3 {$Password+=($lowercase | GET-RANDOM)}
        4 {$Password+=($specialchars | GET-RANDOM)}
    }
}
return $Password

}

更改路径到正确的目的地

$PW = GetRandomPassword -length 25 -count 300 >> C:\ctdInstall\PW.txt

【问题讨论】:

    标签: .net powershell casting powershell-3.0 powershell-4.0


    【解决方案1】:

    您确定它们运行相同的 powershell 版本吗? Win10内置PS 5.0,Win7内置PS2.0。较新的 powershell 版本可能有一些额外的保护措施来修复转换错误。

    更重要的是:为什么要使用 char? :S

    将其更改为[int]$length = 10,您的所有问题都会消失。

    Function GetRandomPassword ([int] $length=10, $count=1) {
    

    【讨论】:

    • 有趣。所以它真的归结为 Powershell 的版本,而不是 .Net 版本。让我实现这个改变并测试一下。感谢您的快速回复。
    • Powershell 具有许多功能,可以更轻松地使用 .NET 函数,因为它是一种脚本语言。此外,25 是两个字符,如您在错误中看到的那样被截断为 2。
    • Frode,实际上,通过将 $length 从 char 更改为 int 将不起作用,不是吗?使用 char 的原因是因为我选择了密码的长度,包括特殊大小写、大写字母、小写字母和数字。是的,我看到它被截断了,但是让我不知道为什么它不会输出随机密码。它仍然是一个字符,对吗?
    • 太棒了!感谢您的回答。我将 char 转换为 int 并且没有问题。我在某处擦掉了一个括号,但很快就发现了。但你肯定帮我解决了这个问题!谢谢大佬!
    • 谢谢哥们。非常感谢您的帮助。我需要时间成为一名经验丰富的程序员/脚本编写者,但我希望我能从这个社区学到很多东西。我很感激像你这样的人……总是愿意帮助他人,让他人学习和成长。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多