标签: start-job

Powershell Start-Job将无法运行

我正在尝试在PowerShell 2.0中运行一个简单的作业流程,它似乎没有运行.

    $job = Start-Job { Return "Some string." }

当我打电话给$ job时,状态显示它正在运行.但问题是它永远不会完成.

在我的Windows 7机器上尝试了同样的事情,它立即完成.

我正在windows xp上运行powershell 2.0.

有谁知道导致这个问题的原因是什么?我该如何解决这个问题?

这是我的 $PSVersionTable

    Name                           Value
    ----                           -----
    PSVersion                      2.0
    PSCompatibleVersions           {1.0, 2.0}
    BuildVersion                   6.0.6002.18111
    PSRemotingProtocolVersion      2.1
    WSManStackVersion              2.0
    CLRVersion                     4.0.30319.1
    SerializationVersion           1.1.0.1

powershell background start-job

3
推荐指数
1
解决办法
1334
查看次数

Start-Job脚本块 - 如何使用参数调用cmdlet?

我是Start-Jobcmdlet的新手,我在调用带有cmdlet的脚本块时遇到了问题.这是我到目前为止所拥有的:

    Start-Job -ScriptBlock {
       $ServiceObj = Get-Service -Name $ServiceName -ComputerName $Computer -ErrorAction Stop   
       Stop-Service -InputObj $ServiceObj -erroraction stop 
    }
Run Code Online (Sandbox Code Playgroud)

我在运行时看到错误receive-job,-ComputerName参数为null或为空,-InputObj参数为null或为空.在这两种情况都不是这样.上面的代码片段是从两个foreach循环内部调用的:

foreach($Computer in $ComputerNames) {
  foreach($ServiceName in $ServiceNames) {
   #..call snippet above
  }
 }
Run Code Online (Sandbox Code Playgroud)

我曾尝试使用-ArgumentListwhen调用我的脚本块,但也没有运气.我确定我错过了什么?

powershell arguments cmdlet start-job

3
推荐指数
1
解决办法
7248
查看次数

Powershell 作业“已完成”但未完成其任务

我正在尝试通过 powershell 中的作业启动/停止服务。在我的本地机器上进行测试时,作业将进入完成状态,但服务不会更改其状态。有没有办法更好地了解我的工作做了什么?

这是代码:

$service = 'MSSQL$SQLEXPRESS'
$server = 'myPC'
$myJob = Start-Job -Scriptblock {Get-Service -Name $args[0] -computername $args[1] | Set-Service -Status Running} -ArgumentList @($service,$server)
sleep -Seconds 10
get-job
Run Code Online (Sandbox Code Playgroud)

当我运行该代码段时,SQL SQL (EXPRESS) 服务没有启动。

但是,如果我从提升的 shell 运行以下命令,它将启动:

Get-Service -Name $service -computername $server | Set-Service -Status Running
Run Code Online (Sandbox Code Playgroud)

当我运行 Get-Job 时,这是我看到的......

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
10     Job10           BackgroundJob   Completed     True            localhost            Get-Service -Name $arg...
Run Code Online (Sandbox Code Playgroud)

它们都从提升的 shell 运行,所以我绝对有权启动/停止服务。

powershell start-job

3
推荐指数
1
解决办法
2024
查看次数

Powershell启动作业范围

我有一个很长的剧本。我有一个记录功能:

function Log ([string]$Content){
    $Date = Get-Date
    Add-Content -Path $LogPath -Value ("$Date : $Content")
}
Run Code Online (Sandbox Code Playgroud)

在脚本的某些时候,我需要并行运行作业。我有一个计算机名称列表,我需要对每个计算机名称使用 psexec。这应该作为并行运行的作业来完成


        Start-Job -ScriptBlock {
        
        Log "$line Has called"
            $Program_List = gc $USERS_DB_PATH\$line.txt | select -Skip 1
            if (Test-Connection $line -Quiet) {
                    ForEach ($program in $Program_List){
                    Log "$line $program"
                    #"line $line bot is $bot pwd is $pwd"
                    psexec \\"$line" -u bla.local\"$bot" -p $pwd cmd bla
                    
                    }
            }
            else{
                  Log "Cannot Connect to $line"
            }
        
        
        }
        #Remove-Item "$USERS_DB_PATH\$line.txt"


}

Run Code Online (Sandbox Code Playgroud)

我知道这与范围有关,但是如何使这个脚本块看到函数 Log 和所有必要的变量?他们都空了

powershell scope start-job scriptblock

2
推荐指数
1
解决办法
2709
查看次数

将写入进度添加到获取作业/等待作业

我使用下面的代码来显示 PowerShell 作业的结果,超时时间为 120 秒。我想通过合并Write-Progress(基于完成的作业数量)来增强此代码。我尝试使用此示例作为参考,但是,当我尝试合并该代码时,进度条会在所有作业全部完成后短暂显示。

    $Jobs = @()
    $ForceStoppedIds = @{}
    
    $Jobs += Get-Job
    $Jobs | Wait-Job -Timeout 120 | Out-Null
    $Jobs | ?{$_.State -eq 'Running'} | Stop-Job -PassThru | %{$ForceStoppedIds[$_.Id] = $true}
    
    foreach ($Job in $Jobs) {
    
        $Name = $Job.Name
        $Output = (Get-Job -Name $Name | Receive-Job)
    
        if ($ForceStoppedIds.Contains($Job.Id)) {
    
            Write-Output "$($Name) - Device unable to process request within 2 minutes"
    
        } else {
    
            Write-Output $Output
    
        }
    
    }
Run Code Online (Sandbox Code Playgroud)

powershell start-job powershell-jobs progress-bar

2
推荐指数
1
解决办法
1294
查看次数

PowerShell:Start-Job,Receive-Job:没有返回值

好的,这是我的脚本

$server=@("SERVER1","SERVER2")
$A=@()

foreach($srv in $server){
    start-job -scriptblock {Get-AppHangs $srv}}

while (Get-Job -State "Running"){}

foreach($JB in Get-Job){$A+=Receive-Job $JB}

Remove-Job *

$A|Out-GridView


function Get-AppHangs($srv){
    $A=@()
    $XX=Get-EventLog -ComputerName $srv -LogName "application" | Where-Object {$_.InstanceID -ge 1000 -and $_.InstanceID -lt 1005} | select -First 5

    foreach($x in $XX){
        $time = $_.Time.ToString()
        $obj=New-Object PSObject
        $obj|Add-Member -MemberType "NoteProperty" -Name Server -Value $srv
        $obj|Add-Member -MemberType "NoteProperty" -Name Index -Value $x.Index
        $obj|Add-Member -MemberType "NoteProperty" -Name Time -Value $x.Time -SecondValue System.string
        $obj|Add-Member -MemberType "NoteProperty" -Name EntryType -Value $x.EntryType …
Run Code Online (Sandbox Code Playgroud)

powershell custom-object start-job

1
推荐指数
1
解决办法
6892
查看次数

无法将参数绑定到参数'command',因为它为null。电源外壳

我有一个类似于以下代码的功能。它接收命令和命令参数。我必须在后台运行此命令并收集输出。但是最后一条语句让我很困惑

错误:

Cannot bind argument to parameter 'Command' because it is null.
+ CategoryInfo          : InvalidData: (:) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.InvokeExpre
ssionCommand
+ PSComputerName        : localhost
Run Code Online (Sandbox Code Playgroud)

码:

$cmd = 'Get-content'
$Arg = 'path to file'
$sb = "$cmd $Arg -ErrorVariable e -ErrorAction Stop"
invoke-Expression $sb #This printsoutput
$job = Start-job -ScriptBlock {Invoke-Expression $sb}
wait-job -id $job.Id
$job | Receive-job #this should print output but throwing error
Run Code Online (Sandbox Code Playgroud)

我很确定最后一行是抛出错误的那一行。

powershell start-job

1
推荐指数
1
解决办法
3781
查看次数

从 Start-Job 调用获取输出

我有以下代码,它可以创建多个作业并在数组 ( $SMSMembers) 中的所有计算机上运行脚本块内的内容。问题是它没有给出任何有意义的输出,我可以用它来确定代码是否成功运行。我已经尝试了大约 100 种不同的方法,但没有一个解决方案对我有用。这是我试图运行的代码,我认为根据我在 StackOverflow 上看到的其他帖子应该可以工作。

$SMSMembers = @("computer1","computer2","computer3")
$output = @()
foreach ($compName in $SMSMembers) {
    $scriptblock = {
        $file = {"test"}
        $filepath = "\\$using:compName\c$\scripts\NEWFILE.txt"
        $file | Out-File -FilePath $filepath
        Start-Sleep -Seconds 5
        Remove-Item $filepath
    }
    $output += Start-Job -ScriptBlock $scriptblock | Get-Job | Receive-Job
}
Get-Job | Wait-Job
foreach ($item in $output) {
    Write-Host $item
}
Run Code Online (Sandbox Code Playgroud)

除了将文件复制到远程计算机然后将其删除之外,该脚本没有做太多事情。我只想获得工作是否成功的输出。就像我说的,这段代码可以正常工作,我只是没有得到反馈。

我的最终目标是能够向一组计算机发送命令 ( $SMSMembers) 并使用此代码请求当前用户并获取用户名输入:

$user = gwmi Win32_ComputerSystem -Comp $compName |
        select Username -ExpandProperty Username
Run Code Online (Sandbox Code Playgroud)

windows powershell multithreading start-job

1
推荐指数
1
解决办法
5203
查看次数

将带有属性的变量传递给argumentlist,丢失属性

$Computers = Get-QADComputer -sizelimit 5
Run Code Online (Sandbox Code Playgroud)

返回五台计算机的列表.我循环

foreach($computer in $computers) {
echo "and then I can do this $computer.name"
Run Code Online (Sandbox Code Playgroud)

从$计算机中只获取计算机名称.但是当我试图将它传递给这样的开始工作时:

    Start-Job -FilePath $ScriptFile -Name $Computer.Name -ArgumentList $Computer
Run Code Online (Sandbox Code Playgroud)

我无法在$ scriptfile中执行$ computer.name.我必须像$ computer.name一样传递它并将其称为$ args [0].但后来我松开了所有其他属性(我在$ scriptfile中使用了一堆.)

我没有到这里来的是什么?你会叫什么电脑?那你叫什么叫$ computer.name?

淑娜:)

powershell arguments start-job

0
推荐指数
1
解决办法
1838
查看次数

等到所有线程完成后再运行下一个任务

我想包里面的东西foreach($computer in $computers)Start-Job使它们同时运行.唯一的问题是,我需要等到所有工作完成才能完成ConvertTo-Json底部工作.

$sb = "OU=some,OU=ou,DC=some,DC=domain"
$computers = Get-ADComputer -Filter {(Enabled -eq $true)} -SearchBase "$sb" -Properties *
$hasmanufacturer = New-Object System.Collections.Generic.List[System.Object]
foreach($computer in $computers)
{
    $drives = try{@(Get-WMIObject -Class Win32_CDROMDrive -Property * -ComputerName $computer.Name -ErrorAction Stop)} catch {$null}
    foreach($drive in $drives)
    {
        if($drive.Manufacturer)
        {
            $hasmanufacturer.Add($computer)
            continue
        }
    } # inner foreach
}

ConvertTo-Json $hasmanufacturer
Run Code Online (Sandbox Code Playgroud)

powershell jobs multithreading start-job

0
推荐指数
1
解决办法
566
查看次数