born2Learn
born2Learn

Reputation: 1413

unexpected token in expression or statement - powershell

## To run the script
# .\get_status.ps1 -Hostname <host> -Service_Action <action> -Service_Name <name>

#$Hostname = "hostname"
#$Service_Action = "Get-Service"
#$Service_Name = "service_name"

param(
    [string]$Hostname,
    [string]$Service_Action,
    [string]$Service_Name
)

$ScriptBlockContent = {
    param($Service_Action, $Service_Name)
    & $Service_Action $Service_Name
    }

# user credentials
$Username = "username"
$Password = "password"

# To avoid Manual entry of Username and Password
$Secure_String = convertto-securestring $Password -asplaintext -force
$User_cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Secure_String

# Create a Session
$pso = New-PSSessionOption -NoMachineProfile
$sess = New-PSSession -ComputerName $Hostname -SessionOption $pso -credential $User_cred

#Run a powershell script in the session.
Invoke-Command -Session $sess -ScriptBlock $ScriptBlockContent -ArgumentList $Service_Action, $Service_Name

# Remove session
Remove-PSSession $sess

To run the script:

.\<script_name>.ps1 -Hostname <host> -Service_Action <action> -Service_Name <name>

For ex: Service Action is- Get-Service, Stop-Service, Start-Service and then Name

Command: Get-Service Servicename

I am getting an error: Unexpected token in expression or statement on this line of code:

$ScriptBlockContent = {
        param($Service_Action, $Service_Name)
        $Service_Action $Service_Name # here is the error
        }

Upvotes: 4

Views: 18259

Answers (2)

born2Learn
born2Learn

Reputation: 1413

## To run the script
# .\get_status.ps1 -Hostname <host> -Service_Action <action> -Service_Name <name>

#$Hostname = "hostname"
#$Service_Action = "Get-Service"
#$Service_Name = "service_name"

param(
    [string]$Hostname,
    [string]$Service_Action,
    [string]$Service_Name
)

$ScriptBlockContent = {
    param($Service_Action, $Service_Name)
    & $Service_Action $Service_Name
    }

# user credentials
$Username = "username"
$Password = "password"

# To avoid Manual entry of Username and Password
$Secure_String = convertto-securestring $Password -asplaintext -force
$User_cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $Username, $Secure_String

# Create a Session
$pso = New-PSSessionOption -NoMachineProfile
$sess = New-PSSession -ComputerName $Hostname -SessionOption $pso -credential $User_cred

#Run a powershell script in the session.
Invoke-Command -Session $sess -ScriptBlock $ScriptBlockContent -ArgumentList $Service_Action, $Service_Name

# Remove session
Remove-PSSession $sess`enter code here`

Upvotes: 1

Manuel Batsching
Manuel Batsching

Reputation: 3616

You are passing your commands as strings to your function, so what you are syntactically doing with $Service_Action $Service_Name is to refer to two string objects in one line without any operator connecting them. That is the reason for the exception.

To tell powershell, that you want to execute a string as a command you have several options:

One option is to pass the commands as a single string to the Invoke-Expressioncmdlet:

Invoke-Expression "$Service_Action $Service_Name"

Alternatively you can use the call-Operator &, which also tells powershell to treat a command as string. In this case you cannot give cmdlet and arguments in a single string, but in two:

& $Service_Action $Service_Name

Upvotes: 4

Related Questions