0

I have a PowerShell code which I am calling via powershell.exe. The code does some validation and then calls the main script to do the actual job.

Below is the code:

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
}

Below is the error message:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command {
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndCurlyBrace
9
  • Where are you running it from? Outside of powershell, it would have to be on line line. You can run a script instead. Commented Jul 15, 2019 at 15:11
  • I think, he puts this stuff in a batch file and wants to start it by double-clicking it (or right-click, run as administrator) Commented Jul 15, 2019 at 16:09
  • 1
    @RonnyKaufmann "It would have to be on one line." that should say. Commented Jul 15, 2019 at 16:15
  • What does main.ps1 look like? The error message clearly states that you are not properly closing a script block. Is that issue in main.ps1? Commented Jul 15, 2019 at 17:05
  • 1
    Just use semicolons and put all the lines together. Commented Jul 15, 2019 at 18:50

1 Answer 1

0

Thanks to @JS2010; I formatted the code into a single line by adding semicolons (;). Also, added the code under "& { code }"

Below is the modified code:

powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {$trademark = 'MMFSL';$product = 'OSHardening';$version = '3.0';Add-Type -AssemblyName System.Windows.Forms;$PCWD = (Get-WmiObject -Class Win32_ComputerSystem).PartOfDomain;if($PCWD -eq $true){[System.Windows.Forms.MessageBox]::Show('This is application is supported on Workgroup/Non-Domain Joined computers only.', "$($trademark) + '-' + $($product) + '-v' + $($version)", 'Ok', 'Error');break;}elseif ($PCWD -eq $false){Expand-Archive -Path .\Windows.zip -DestinationPath $env:ProgramData\$($trademark)\$($product)\$($version) -Force;Set-Location -Path $env:ProgramData\$($trademark)\$($product)\$($version);Invoke-Expression -Command .\main.ps1;}}"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.