Powershell 脚本在 Windows 7 而不是 Windows 10 中抛出转换错误
Powershell Script is throws Casting Error in Windows 7 and NOT Windows 10
首先,我假设我面临的问题是 .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
您确定它们 运行 是相同的 powershell 版本吗? Win10 有 PS 5.0 built-in 而 Win7 有 PS2.0。较新的 powershell 版本可能有一些额外的保护措施来修复转换错误。
更重要的是:为什么要使用 char? :S
将其更改为[int]$length = 10
,您所有的问题都会消失。
Function GetRandomPassword ([int] $length=10, $count=1) {
首先,我假设我面临的问题是 .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
您确定它们 运行 是相同的 powershell 版本吗? Win10 有 PS 5.0 built-in 而 Win7 有 PS2.0。较新的 powershell 版本可能有一些额外的保护措施来修复转换错误。
更重要的是:为什么要使用 char? :S
将其更改为[int]$length = 10
,您所有的问题都会消失。
Function GetRandomPassword ([int] $length=10, $count=1) {