0
## 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> 

: 서비스 조치를 받기 서비스 중지, 서비스, 서비스-시작하고

이름은 -

: 코드 줄에 표현이나 문에 예기치 않은 토큰 :
Command: Get-Service Servicename 

나는 오류를 얻고있다

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

을'& $ Service_Action $ Service_Name' – PetSerAl

+0

추가. 오류 받기 : $ Service_name에 대한 인수를 허용하는 위치 매개 변수를 찾을 수 없습니다. –

+0

전체 오류 메시지를 표시하십시오. – PetSerAl

답변

0
## 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` 
-1

명령을 문자열로 함수에 전달하므로 $Service_Action $Service_Name으로 구문 적으로 수행하는 작업은 연산자를 연결하지 않고 한 줄에 두 개의 문자열 객체를 참조하는 것입니다. 그것이 예외의 이유입니다.

Invoke-Expression "$Service_Action $Service_Name" 

:

하나의 옵션이 Invoke-Expression cmdlet을 단일 문자열로 명령을 전달하는 것입니다 :

당신이 몇 가지 옵션이 명령으로 문자열을 실행하려는, PowerShell을 말하려면 또는 call-Operator &을 사용할 수 있습니다.이 명령은 powershell에 명령을 문자열로 처리하도록 지시합니다. 이 경우 하나의 문자열에 cmdlet을 및 인수를 제공하지만, 두에서 할 수 없습니다

& $Service_Action $Service_Name 
+0

업데이트 된 스크립트 ... –