start-job function 传递参数
$j2="ww"
$j3="tt"
start-job -ScriptBlock { ##这半个大括号不能换行到下一行,否则出现语法错误
param($j2,$j3)
function wt
{
param ($t1,$t2)
$t1+" l ly "+$t2|out-file d:\j.txt
}
wt -t1 $j3 -t2 $j2
} -ArgumentList $j2,$j3
get-job
##############################################
#停止远程计算机上的服务
#定义存放Server列表的文件名称
$file_server = "Serverlist.txt"
$CurrentPath = $MyInvocation.MyCommand.Path.substring(0,$MyInvocation.MyCommand.Path.LastIndexOf('\')+1)
$server_list = $CurrentPath + $file_server
$servicename = "DHCP"
$servers = Get-Content $server_list
Foreach ($server in $servers)
{
$servername = $server.split(",")[0]
$serverpass = $server.split(",")[1]
$UserName = "administrator"
$Password = ConvertTo-SecureString $serverpass -AsPlainText –Force
$cred = New-Object System.Management.Automation.PSCredential($UserName,$Password)
Write-Host $servername -ForegroundColor Green
invoke-command -ComputerName $servername -Credential $cred -ScriptBlock {param($servicename) net stop $servicename} -ArgumentList $servicename
}