powershel遠程執行命令

常常須要登陸到windows執行操做,遠程桌面開多了也挺麻煩。這裏就寫個powershell的腳本用來支持平常工做shell

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}

執行後的效果圖
powershel遠程執行命令windows

相關文章
相關標籤/搜索