## 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
}
을'& $ Service_Action $ Service_Name' – PetSerAl
추가. 오류 받기 : $ Service_name에 대한 인수를 허용하는 위치 매개 변수를 찾을 수 없습니다. –
전체 오류 메시지를 표시하십시오. – PetSerAl