• invoke-command


    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

  • 相关阅读:
    ASP.NET配置文件Web.config 详细解释
    Firefox 3.6最新功能:网页可根据设备方位调整角度
    ascx + wrapper page + jQuery的Ajax解决方案
    C#中常用的文件操作方法
    Excel鲜为人知的35招秘技
    NHibernate.Search 基于Lucene.NET的全文索引
    Firefox和IE之间7个JavaScript的差异
    .net2.0使用json的知识,要点,问题和解决方案
    服务器响应HTTP的类型ContentType大全
    欢迎Clonezilla,再见Symantec Ghost
  • 原文地址:https://www.cnblogs.com/zmwgz/p/10678108.html
Copyright © 2020-2023  润新知