使用具有输入验证和TRAP的Read-host

时间:2018-05-23 11:07:47

标签: powershell

我只是一个典型的管理员,试图为远程办公室的一些IT助手制作一个简单的脚本,使域连接更容易,同时最大限度地减少潜在的错误。脚本的最终游戏是运行单行命令/** * @Route("/") */ class HomeController extends Controller { public function index(Request $request) { //Form to add new documents $form = $this->newForm(); $form->handleRequest($request); $user = $this->getDoctrine()->getRepository(User::class)->find($this->getUser()); //Gets all user documents $files = $user->getDocuments(); //Gets all categories $categories = $this->getDoctrine()->getRepository(Category::class)->findAll(); //Adds new document to database if($form->isSubmitted() && $form->isValid()) { $article = $form->getData(); $article->setUser($this->getUser()); $entityManager = $this->getDoctrine()->getManager(); $entityManager->persist($article); $entityManager->flush(); return $this->redirectToRoute('index'); } return $this->render('home/home.html.twig', [ 'files' => $files, 'categories' => $categories, 'form' => $form->createView(), ]); } }

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

Google搜索代码段几天,尤其是来自像你这样的网站,非常有帮助。但我现在遇到的问题是我找不到合适的代码来组合Read-Host,输入长度验证和TRAP一起工作而不会丢失我的变量值。

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

提前非常感谢你。

请参阅下面的代码:

/**
 * @Route("/{categoryId}")
 */
public function categories(request $request, $categoryId)
    {
        $form = $this->newForm();
        $form->handleRequest($request);

        $user = $this->getDoctrine()->getRepository(User::class)->find($this->getUser());
        $categories = $this->getDoctrine()->getRepository(Category::class)->findAll();

        $category = $this->getDoctrine()->getRepository(Category::class)->findOneBy(["id" => $categoryId]);
        $categoryFiles = $this->getDoctrine()->getRepository(Document::class)->categoryFiles($category, $user);

        if($form->isSubmitted() && $form->isValid()) {
            $article = $form->getData();
            $article->setUser($this->getUser());
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($article);
            $entityManager->flush();
            return $this->redirectToRoute('index');
        }

        return $this->render('home/home.html.twig', [
            'files' => $categoryFiles,
            'categories' => $categories,
            'form' => $form->createView(),
        ]);
    }

2 个答案:

答案 0 :(得分:1)

在你的代码中,你有许多非常奇怪的定义。您的函数会创建一个新范围,并且您调用它们的变量将在调用后消失,除非您更改变量范围(在本例中为$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控制台中进行测试和测试。

答案 1 :(得分:0)

也许Try / Catch阻止而不是陷阱?

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
}