无法使用powershell.exe在-Command开关内运行代码

时间:2019-07-15 14:15:09

标签: powershell

我有一个通过powershell.exe调用的PowerShell代码。该代码进行了一些验证,然后调用主脚本来完成实际工作。

下面是代码:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {
    $trademark = 'MMFSL'
    $product = 'OSHardening'
    $version = '3.0'

    Add-Type -AssemblyName System.Windows.Forms
    #Checks if the current powershell session is running in Administrative Mode
    $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
    if (!($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))) {
        [System.Windows.Forms.MessageBox]::Show('Please run the file using Run as Administrator', "$($trademark)-$($product)-v$($version)", 'Ok', 'Error')
        break;
    }

    Expand-Archive -Path .\Windows.zip -DestinationPath $env:ProgramData\$($trademark)\$($product)\$($version) -Force

    Set-Location -Path $env:ProgramData\$($trademark)\$($product)\$($version)

    .\main.ps1
}

下面是错误消息:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace

1 个答案:

答案 0 :(得分:0)

感谢@ JS2010;我通过添加分号(;)将代码格式化为一行。另外,将代码添加到$ gdb a.exe GNU gdb (GDB) (Cygwin 8.1.1-1) 8.1.1 Copyright (C) 2018 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-pc-cygwin". Type "show configuration" for configuration details. For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Find the GDB manual and other documentation resources online at: <http://www.gnu.org/software/gdb/documentation/>. For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from a.exe...done. (gdb) r Starting program: /home/Vin/coding_practice/cpp/google/ctci/ll/2.1/rev_ll/a.exe [New Thread 7076.0x358] [New Thread 7076.0xc58] [New Thread 7076.0x14fc] [New Thread 7076.0xecc] At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 58 At line 61 At line 163 i:8 j:7 At line 168 At line 58 At line 61 i:7 j:6 At line 174 At line 168 [New Thread 7076.0xcac] [Thread 7076.0xecc exited with code 0] Thread 1 "a" received signal SIGINT, Interrupt. std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x60006c170) at /usr/lib/gcc/x86_64-pc-cygwin/7.4.0/include/c++/bits/shared_ptr_base.h:147 147 _M_release() noexcept (gdb) q A debugging session is active. Inferior 1 [process 7076] will be killed. Quit anyway? (y or n) y

下面是修改后的代码:

"& { code }"