4
## 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
        }
born2Learn
  • 1,253
  • 5
  • 14
  • 25
  • `& $Service_Action $Service_Name` – user4003407 Oct 06 '17 at 17:59
  • Added. Getting error: A positional parameter cannot be found that accepts argument for $Service_name – born2Learn Oct 06 '17 at 18:31
  • Please show full error message. – user4003407 Oct 06 '17 at 18:48
  • ```Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed. Invoke-Command : A positional parameter cannot be found that accepts argument 'service_name'. At C:\Users\xxxxxxx\Source\Repos\xxxxxxx\Tests\get_status.ps1:32 char:15 + Invoke-Command <<<< -Session $sess -ScriptBlock $ScriptBlockContent -ArgumentList $Service_Action $Service_Name + CategoryInfo : InvalidArgument: (:) [Invoke-Command], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand``` – born2Learn Oct 06 '17 at 19:13
  • 1
    `Invoke-Command ... -ArgumentList $Service_Action, $Service_Name` – user4003407 Oct 06 '17 at 19:14
  • Perfect!!! I was missing it. Thanks a ton @PetSerAI – born2Learn Oct 06 '17 at 19:17

2 Answers2

4

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
Manuel Batsching
  • 3,406
  • 14
  • 20
1
## 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`
born2Learn
  • 1,253
  • 5
  • 14
  • 25