经常需要登录到windows执行操作,远程桌面开多了也挺麻烦。这里就写个powershell的脚本用来支持日常工作

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}

执行后的效果图