zoukankan      html  css  js  c++  java
  • invoke-command

     远程执行命令:

    invoke-command -ComputerName $server -Credential $cred -ScriptBlock{param($server,$UserName,$serverpass,$starttime,$startdate)
    $hotfix_setup = schtasks /query |select-string "hotfix_setup" -quiet
    If ($hotfix_setup -eq "true")
    {schtasks /delete /tn "hotfix_setup" /f |out-null}
    schtasks /create /tn "hotfix_setup" /sc once /ru $UserName /rp $serverpass /st $starttime /sd $startdate /tr D:HotfixHotfix_Win20032014-04hotfix_setup.bat
    } -ArgumentList $server,$UserName,$serverpass,$starttime,$startdate

     远程执行脚本(脚本位于本地计算机,非远程):

     invoke-command -ComputerName $servername -Credential $cred -FilePath $script_path -ArgumentList $servername,$serverpass  |Out-File $task_result -append

    ========================================================

    invoke-command -computer remotecomputer 脚本中的变量执行结果不会返回到本地计算机。如果在脚本块中直接让结果显示在控制台,则可以显示。

    取在远程计算机执行结果到本地

    $UserName = "administrator"
    $serverpass = "6019"

    $server = "10.4.19.60"
    $Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
    $cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
    $abc = invoke-command -ComputerName $server -Credential $cred -ScriptBlock {
    hostname
    gwmi win32_operatingsystem
    }

    $abc[1].caption

    invoke-command -AsJob [<SwitchParameter>]
    在远程计算机上将命令作为后台作业运行。使用此参数可运行需要较长时间才能完成的命令。

    AsJob 参数类似于使用 Invoke-Command 远程运行 Start-Job 命令。但是,对于 AsJob,作业是在本地计算机上创建的,即使作业运行在远程计算机上也是如此,而且远程作业的结果会自动返回本地计算机。

    $JobName = "JobUpdateCheck"
    $Null = Start-Job -Name $JobName -scriptblock ${Function:TaskSch} -ArgumentList $TaskCheckName,$UserName,$UserPass,$TaskChecktime,$TaskCheckdate,$TaskCheckScriptPath
    Do {
    Start-Sleep -Milliseconds 500
    $JobState = (Get-Job -Name $JobName).State
    }
    Until ($JobState -eq "Completed")
    Receive-Job -Name $JobName
    Get-Job -Name $JobName |Remove-Job

  • 相关阅读:
    plsqldeveloper永久注册码
    git常用命令
    淘淘中的一些环境的处理
    Hibernate查询
    markdown的学习
    npm如何发布一个自己的package
    聊聊font-family的那些事
    js 随机生成任意长度的字符串
    CSS世界(张鑫旭)系列学习总结 (四)盒尺寸四大家族
    CSS世界(张鑫旭)系列学习总结 (三)width和height作用的具体细节
  • 原文地址:https://www.cnblogs.com/dreamer-fish/p/3716236.html
Copyright © 2011-2022 走看看