## 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
要运行脚本:意外的标记 - PowerShell的
.\<script_name>.ps1 -Hostname <host> -Service_Action <action> -Service_Name <name>
对于例如:服务行动IS-GET-服务,停止,服务,启动服务 然后命名
在这行代码 意外令牌表达式或语句:Command: Get-Service Servicename
我得到一个错误
$ScriptBlockContent = {
param($Service_Action, $Service_Name)
$Service_Action $Service_Name # here is the error
}
'&$ Service_Action $ Service_Name' – PetSerAl
增加。获取错误:无法找到接受$ Service_name的参数的位置参数 –
请显示完整的错误消息。 – PetSerAl