代码之家  ›  专栏  ›  技术社区  ›  Jee

使用带输入验证和陷阱的读主机

  •  0
  • Jee  · 技术社区  · 6 年前

    我只是一个典型的管理员,试图为远程办公室中的一些IT助理编写一个简单的脚本,使域连接更容易,同时最大限度地减少潜在的错误。脚本的最终目的是运行一行命令 Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart .

    但整个脚本应该包括对计算机新名称的输入验证,以及对远程办公室的两个字母代码的目标OU的输入验证。

    搜索几天的代码片段,特别是像你这样的网站,非常有用。但现在的问题是,我找不到合适的代码来组合读主机、输入长度验证和陷阱,以便在不丢失变量值的情况下一起工作。

    请原谅我的编码,因为显然我不是真正的ps脚本编写者,而且我知道它的错误部分非常基本。如果我有奢侈,我会花更多的时间,但如果你能给我指明正确的方向,我会非常感激。

    提前非常感谢。

    请看下面我的代码:

    # START: Display name and purpose of invoked script
    $path =  $MyInvocation.MyCommand.Definition 
    Clear-Host
    Write-Host $path
    Write-Host " " 
    Write-Host "This script will allow you to do the following in a single-step process:"
    Write-Host "(1) RENAME this computer"
    Write-Host "(2) JOIN it to MYDOMAIN"
    Write-Host "(3) MOVE it to a target OU"
    Write-Host "(4) REBOOT" 
    Write-Host " "
    Pause
    
    
    # Function: PAUSE
    Function Pause ($Message = "Press any key to continue . . . ") {
    if ((Test-Path variable:psISE) -and $psISE) {
        $Shell = New-Object -ComObject "WScript.Shell"
        $Button = $Shell.Popup("Click OK to continue.", 0, "Script Paused", 0)
    }
    else {     
        Write-Host -NoNewline $Message
        [void][System.Console]::ReadKey($true)
        Write-Host
    }
    Write-Host " " 
    }
    
    # Function: Define the parameters
    Function Define-Parameters {   
    
    # Specify new computer name, with validation and TRAP
    $WS_NewName = $null
    while ($null -eq $WS_NewName) {
    [ValidateLength(8,15)]$WS_NewName = [string](Read-Host -Prompt "NEW NAME of computer (8-15 chars.)" )
    TRAP {"" ;continue} 
    }
    Write-Host " "
    
    # Domain to join.
    $DomainToJoin = 'mydomain.net' 
    
    # Specify the target OU, with validation and trap
    $baseOU='OU=Offices OU,DC=mydomain,DC=net'
    $OU2 = $null
    while ($null -eq $OU2) {
    [ValidateLength(2,2)]$OU2 = [string](Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)' )
    TRAP {"" ;continue} 
    }
    Write-Host " "
    $LocDN = "OU=$OU2,$baseOU"  
    }
    
    
    
    # Function: Summary and confirmation screen for defined parameters.
    Function Confirm-Parameters {
    Write-Host "==========================================================================="
    Write-Host "Please confirm that you are joining this computer to 
    $DomainToJoin (MYDOMAIN)"
    Write-Host "with the following parameters:" 
    Write-Host ""
    Write-Host ""
    Write-Host "Computer's NEW NAME:   $WS_NewName" 
    # Write-Host "Domain to Join:      $DomainToJoin" 
    Write-Host "TARGET mission OU:     $OU2" 
    }
    
    
    # Call Define-Parameters Function
    Define-Parameters
    
    # Call Confirm-Parameters Function
    Confirm-Parameters
    
    <#
    Some more code here
    #>
    
    
    # FINAL COMMAND if all else works: Join the computer to the domain, rename it, and restart it. 
    # Add-Computer -DomainName $DomainToJoin -OUPath $LocDN -NewName $WS_NewName -Restart
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Maximilian Burszley    6 年前

    在您的代码中,有很多东西定义得很奇怪。您的函数创建了一个新的作用域,并且您试图在其中定义的变量在调用后将消失,除非您将变量作用域(在本例中,改为 $script: $global: )此外,要使用函数,您需要首先定义它们(您的 Pause 实际上什么都不做)

    这是你可以做的事情 Define-Parameters 功能(我建议查看 Get-Verb )

    # Domain to join.
    $DomainToJoin = 'mydomain.net' 
    
    # Function: Define the parameters
    Function Get-Parameters {
        do {
            $global:WS_NewName = Read-Host -Prompt 'NEW NAME of computer (8-15 chars)'
        } until ($WS_NewName.Length -gt 7 -and $WS_NewName.Length -lt 16)
        ''
    
        do {
            $global:OU2 = Read-Host -Prompt 'Target OU (TWO-LETTER code for your office)'
        } until ($OU2 -match '^[a-z]{2}$')
        ''
        $OU2 = "OU=$global:OU2,OU=Offices OU,DC=mydomain,DC=net"
    }
    

    我强烈建议您离开ISE,在实际的PowerShell控制台中进行测试。

        2
  •  0
  •   ps1psm1    6 年前

    可能是尝试/捕获块而不是陷阱?

    Try {
        [ValidatePattern('^\w{8,15}$')]$compname=read-host 'enter comp name' -ErrorAction Stop
        [ValidatePattern('^\w{2}$')]$OU=read-host 'enter OU name' -ErrorAction Stop
    }
    Catch {
    $ErrorMessage = $_.Exception.Message
        $ErrorLineNumber = $_.InvocationInfo.ScriptLineNumber
        $ErrorCommandName = $_.InvocationInfo.InvocationName
        Write-Error -Message "The error message was: <$ErrorMessage>, script line: <$ErrorLineNumber>, command name: <$ErrorCommandName>"
        exit 255
    }