我正在尝试在后台运行一个带有参数的.exe并且目标有空格的作业.例如:
$exec = "C:\Program Files\foo.exe"
Run Code Online (Sandbox Code Playgroud)
我想用参数运行它:
foo.exe /param1 /param2, etc.
Run Code Online (Sandbox Code Playgroud)
我知道这样Start-Job做但我已经尝试了很多不同的组合,它或者由于白色空间或参数而给我一个错误.有人可以帮我解决这里的语法吗?我需要假设这$exec是可执行文件的路径,因为它是配置文件的一部分,可能会在以后更改.
我有一个启动工作的powershell脚本
start-job -scriptblock {
while($true) {
echo "Running"
Start-Sleep 2
}
}
Run Code Online (Sandbox Code Playgroud)
然后它继续执行脚本的其余部分.
那项工作对于该过程的PID来说是一种监控.
我想每隔n秒同步打印PID,而不必结束工作.
例如,当正在执行脚本的其余部分时,我希望在我的控制台中看到输出.
在PowerShell中有类似的东西吗?
谢谢.
我正在尝试在48台计算机的列表上执行cmd文件.我不想执行并等待顺序完成,因为每个cmd大约需要10分钟才能完成.WinRM不是一个选项.WMI也不是.PSExec 是一个选项....但我似乎无法让它在Start-Job中运行.
我做的事情如下:
$sb = {
param
(
$computer = "serverw01",
$userid = "domain2\serviceid",
$password = 'servicepw',
$command = "cd /d d:\ && updateAll.cmd"
)
d:\eps\pstools\PsExec.exe -u $userid -p $password "\\$($computer)" cmd /c $command
}
foreach ($computer in Get-Content "D:\Data\serverlist.txt") {
Start-Job $sb -ArgumentList $computer
}
Run Code Online (Sandbox Code Playgroud)
这创造了一堆工作....但是从来没有完成,如果我接收任何一个工作,我会回来
PS> get-job | receive-job -Keep
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
Run Code Online (Sandbox Code Playgroud)
如果我运行如下函数,它执行得很好:
& …Run Code Online (Sandbox Code Playgroud) 我正在尝试在PowerShell 2.0中执行基本的后台作业,我看到了start-job和invoke-command -asjob的不同内容.
如果我这样做:
start-job -scriptblock {get-process}
我得到一个job对象,但是子作业(由start-job自动创建)总是有一个JobStateInfo为"NotStarted".
但是,这可以按预期工作:
invoke-command -scriptblock {get-process} -computer localhost -asjob
我已经运行了enable-psremoting ....我还需要做些什么才能让后台工作正常工作?
我想对后台作业(以开头start-job)进行计时,并在x几秒钟后将其超时。但是,我很难跟踪每个单独作业的运行时间(我正在运行aprox 400作业)。
我希望有一种方法来超时工作,并将其设置为failed如果不是completed在X秒,但我觉得没有超时参数。
什么是跟踪作业的各个运行时间的好方法?
我想我可以用每个作业的开始时间和作业ID创建一个哈希表,并对照running状态进行检查并进行手动超时,但这听起来有点像“发明轮子”。有任何想法吗?
编辑 谢谢大家在这个主题上进行了富有成果的讨论和启发!
我通过env注入器将凭据传递给脚本(注意这适用于我Invoke-Command)并尝试运行Start-Job但jenkins不喜欢它:
$user = $ENV:user
$pass = $ENV:pass
write-output (cat env:username)
write-output (cat env:user)
write-output (cat env:pass)
$pass = $pass | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($user), $pass
Start-Job -Credential $cred -ScriptBlock {'test'}
write-output (get-job | Receive-Job)
get-job | remove-job
Run Code Online (Sandbox Code Playgroud)
这是我得到的错误(确认的用户名和密码是正确的,当我从控制台运行此脚本时,它具有相同的功能)
Started by user ME
[EnvInject] - Loading node environment variables.
Building in workspace C:\Program Files (x86)\Jenkins\jobs\myjob\workspace
[workspace] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Windows\TEMP\hudson1723222179976241861.ps1'"
MYJENKINSSRV$
correctdomain\correctuser
correctPassword
Id Name PSJobTypeName State …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 powershell 中调用 Start-Job 。当我这样做时,它会生成一个带有以下参数的后台 powershell:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Version 5.0 -s -NoLogo -NoProfile -EncodedCommand [encoded command I want to run in base64]
Run Code Online (Sandbox Code Playgroud)
但是,无论我发送什么命令,powershell 命令似乎都永远不会完成。
我尝试生成一个 powershell 实例,如下所示:
powershell.exe -s
Run Code Online (Sandbox Code Playgroud)
这似乎也创建了一个似乎冻结的实例,没有执行或执行任何操作。在网上查找,我似乎找不到任何对 -s 参数的引用。
有谁知道它的用途或如何摆脱它以便我的起始工作正常工作?
编辑: -s 可能是 -sta 的简写,但我的命令不会使用 -sta 冻结,但会使用 -s 冻结。
Edit2:我后来发现 -s 是 -ServerMode 的简写,显然是旧版 Powershell 2.0 选项。我不知道为什么在使用 Start-Job 时添加它。
Edit3:我使用的命令是:
$deploymentsJobs += Start-Job -InitializationScript { SomeSmallFunction } (AnotherFunction) -ArgumentList $arg1, $arg2, $arg3}
Run Code Online (Sandbox Code Playgroud) 我尝试了这段代码,但只创建了 file1.txt 。File2.txt 没有。我正在寻找使用超时的内置方法。所以不是通过创建自定义循环。
Out-File "file1.txt" #this file is created
$Job = Start-Job -ScriptBlock {
Out-File "file2.txt" #this file is not created
}
$Job | Wait-Job -Timeout 5
$Job | Stop-Job
Run Code Online (Sandbox Code Playgroud) 我们尝试创建一个包含变量的数组,然后将该数组作为扩展传递给脚本,该脚本将由 Start-Job 运行。但实际上却失败了,我们也找不到原因。也许有人可以帮忙!?
$arguments= @()
$arguments+= ("-Name", '$config.Name')
$arguments+= ("-Account", '$config.Account')
$arguments+= ("-Location", '$config.Location')
#do some nasty things with $config
Start-Job -ScriptBlock ([scriptblock]::create("& .'$ScriptPath' [string]$arguments")) -Name "Test"
Run Code Online (Sandbox Code Playgroud)
它失败了
Cannot validate argument on parameter 'Name'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
+ CategoryInfo : InvalidData: (:) [Select-AzureSubscription], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand
+ PSComputerName : localhost
Run Code Online (Sandbox Code Playgroud)
即使 $config.name 设置正确。
有任何想法吗?
先感谢您!
我准备了这个脚本来尝试使用不同的参数多次并行执行相同的函数:
$myparams = "A", "B","C", "D"
$doPlan = {
Param([string] $myparam)
echo "print $myparam"
# MakeARestCall is a function calling a web service
MakeARestCall -myparam $myparam
echo "done"
}
$myparams | Foreach-Object {
Start-Job -ScriptBlock $doPlan -ArgumentList $_
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,输出是
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
79 Job79 BackgroundJob Running True localhost ...
81 Job81 BackgroundJob Running True localhost ...
83 Job83 BackgroundJob Running True localhost ...
85 Job85 BackgroundJob Running True …Run Code Online (Sandbox Code Playgroud) powershell ×10
start-job ×10
timeout ×2
arrays ×1
background ×1
jenkins ×1
process ×1
psexec ×1
scripting ×1
windows ×1