Rajiv Iyer
Rajiv Iyer

Reputation: 183

Unable to run the code inside -Command switch using powershell.exe

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

Upvotes: 0

Views: 295

Answers (1)

Rajiv Iyer
Rajiv Iyer

Reputation: 183

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;}}"

Upvotes: 0

Related Questions