param (
$ip,
$UserName,
$Password
)
function getremotesession($ip,$UserName,$Password)
{
$PasswordSecure = ConvertTo-SecureString $Password -AsPlainText -Force
$Cred = New-Object System.Management.Automation.PSCredential ($UserName,$PasswordSecure)
Try
{
$Session = New-PSSession -computername $ip -credential $Cred
Write-Host $Session
}
catch{
Write-Host "Connet to $IP Computer failed, please check!"
return $false
}
return $Session
}
function test
{
ipconfig
}
$session = getremotesession $ip $UserName $Password
Invoke-Command -Session $Session -ScriptBlock ${function:test}
执行后的效果图
原文:https://blog.51cto.com/thedream/2534683