powershell - 循环期间变量未重置会产生误导性输出

标签 powershell error-handling

我这里有一个脚本,它读取桌面列表并更改这些桌面的管理员密码。它有效,但我有一个小问题。一旦错误出现在 Powershell 中,该值就永远不会重置。例如,第二个 DT 失败,第三个和第四个 DT 成功。在错误栏下,即使密码更改成功,它也会显示第二个DT的错误。但是,它在输出文件 (outputs.txt) 上运行良好。我试过 $error.clear() 但它没有解决我的问题。也许我把它放在错误的地方?如果您能帮我解决这个问题,我将不胜感激,因为我已经完成了 99.9%。谢谢你。

[cmdletbinding()]
param (
[parameter(mandatory = $true)]
$InputFile,
$OutputDirectory
)

#Log failures in an output file called "outputs.txt" stored in the directory of input file
if(!$outputdirectory) {
    $outputdirectory = (Get-Item $InputFile).directoryname
}   
$failedcomputers    =   Join-Path $outputdirectory "outputs.txt"
$stream = [System.IO.StreamWriter] $failedcomputers
$stream.writeline("ComputerName `t IsOnline `t PasswordChangeStatus `t Error")
$stream.writeline("____________ `t ________ `t ____________________ `t _____")

#Prompt to confirm password twice and read password in a secure manner
$password = Read-Host "Enter the password" -AsSecureString
$confirmpassword = Read-Host "Confirm the password" -AsSecureString

#Convert password into plain text for comparison
$pwd1_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($password))
$pwd2_text = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($confirmpassword))

#If the passwords don't match, script exits; otherwise, it continues.
if($pwd1_text -ne $pwd2_text) {
    Write-Error "Entered passwords are not same. Script is exiting."
    exit
}

#Check if input file exists. If file not found, script exits.
if(!(Test-Path $InputFile)) {
    Write-Error "File ($InputFile) not found. Script is exiting."
    exit
}

#Read contents of input file using Get-Content cmdlet and store list in an array called $computers
$Computers = Get-Content -Path $InputFile

#Loop through each computer account in the array and check if online or not using Test-Connection cmdlet
foreach ($Computer in $Computers) {
    $Computer   =   $Computer.toupper()
    $Isonline   =   "OFFLINE"
    $Status     =   "SUCCESS"
    Write-Verbose "Working on $Computer"
    if((Test-Connection -ComputerName $Computer -count 1 -ErrorAction 0)) {
        $Isonline = "ONLINE"
        Write-Verbose "`t$Computer is Online"
    } else { Write-Verbose "`t$Computer is OFFLINE" }

    #If ping is successful, password is changed
    try {
        $account = [ADSI]("WinNT://$Computer/Administrator,user")
        $account.psbase.invoke("setpassword",$pwd1_text)
        Write-Verbose "`tPassword Change completed successfully"
    }

    #If password change fails, respective error is recorded
    catch {
        $status = "FAILED"
        Write-Verbose "`tFailed to Change the administrator password. Error: $_"
        $e = $_.Exception
        $errmsg = $e.Message
        while ($e.InnerException) {
            $e = $e.InnerException
            $errmsg = $e.Message
        }
    }

    $obj = New-Object -TypeName PSObject -Property @{
        ComputerName = $Computer
        IsOnline = $Isonline
        PasswordChangeStatus = $Status
        Error = $errmsg
    }

    $obj | Select ComputerName, IsOnline, PasswordChangeStatus, Error

    if($Status -eq "FAILED" -or $Isonline -eq "OFFLINE") {
        $stream.writeline("$Computer `t`t $isonline `t`t $status `t $errmsg")
    }else{
        $stream.writeline("$Computer `t`t $isonline `t`t $status")
    }

}
$stream.close()
Write-Host "`n`nFailed computers list is saved to $failedcomputers"

最佳答案

从你的小图片中我可以看出,这条输出线正在引起你的悲伤:

$obj | Select ComputerName, IsOnline, PasswordChangeStatus, Error

特别是 Error属性,因为并不总是存在错误状态。总之你需要解决变量$errmsg的内容这就是填充上述属性的内容。在循环的每次传递中,您都应该重置或清除变量。这种思路有几个选择。
  • 在每个循环传递的开头将变量设置为空字符串

    foreach ($Computer in $Computers) {
        $Computer   =   $Computer.toupper()
        $Isonline   =   "OFFLINE"
        $Status     =   "SUCCESS"
        $errmsg     =   ""
    .... more code stuff...
    
  • 您也可以使用 cmdlet Clear-Variable在 try block 之前或输出行之后的类似位置。 备注 $故意省略。

    Clear-Variable errmsg
    
  • 关于powershell - 循环期间变量未重置会产生误导性输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30904024/

    相关文章:

    go - 检查struct中的int字段是否为nil?

    java.lang.reflect.InaccessibleObjectException : Unable to make field private final java. lang.Object java.util.Optional.value 可访问:

    error-handling - 类型错误 : __init__() got an unexpected keyword argument 'progress_bar_refresh_rate'

    powershell - 如何将带空格的路径传递给脚本

    循环中的 Powershell 值失去其值(value)

    c# - Entity Framework 核心3.0-捕获错误

    ios - 从应用程序的 View 中删除 map 时出现奇怪的错误

    regex - 在Powershell中替换文本 block

    powershell - Where-Object 多个条件不起作用

    windows - 从 Console2 复制到剪贴板