小编Loc*_*eds的帖子

在远程服务器上获取所有打开的PS会话(从新的控制台窗口)

我可以在远程服务器上启动5个新的PS会话,并通过运行Get-PSSession查看它们

PS C:\> New-PSSession -ComputerName MyServerName

     Id Name            ComputerName    State         ConfigurationName     Availability
     -- ----            ------------    -----         -----------------     ------------
      1 Session1        MyServerName   Opened        Microsoft.PowerShell     Available

    [repeat 4 more times]
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,当我尝试打开第6次会议,我得到的错误说,这是一个没有没有(5个并发远程PSSession中由于PoswerShells默认限制).但是运行Get-Session会显示所有5个会话,所以到目前为止一切正常:

PS C:\> New-PSSession -ComputerName MyServerName
    New-PSSession : [......maximum number of 5 concurrent shells]

PS C:\> Get-PSSession

     Id Name            ComputerName    State         ConfigurationName     Availability
     -- ----            ------------    -----         -----------------     ------------
      1 Session1        MyServerName   Opened        Microsoft.PowerShell     Available
      2 Session2        MyServerName   Opened        Microsoft.PowerShell     Available
      3 Session3        MyServerName   Opened        Microsoft.PowerShell     Available
      4 Session4        MyServerName   Opened        Microsoft.PowerShell …
Run Code Online (Sandbox Code Playgroud)

powershell remote-access powershell-3.0

13
推荐指数
1
解决办法
5万
查看次数

PowerShell定时器/秒表精度

我发现System.Diagnostics.Stopwatch类具有看似可测量的不准确性,即使在很短的时间内(即20秒)也是如此.我的过程显示编码为运行20秒的进程的耗时20.3秒:

$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
write-host "Started at $(get-date)"

for ($t=1; $t -le 20; $t++) {
    Write-Host "Elapsed Time: $($elapsed.Elapsed.ToString())"
    sleep 1
}

write-host "Ended at $(get-date)"
write-host "Total Elapsed Time: $($elapsed.Elapsed.ToString())"

    Started at 02/02/2013 15:08:43
    Elapsed Time: 00:00:00.0329924
    Elapsed Time: 00:00:01.0417435
    Elapsed Time: 00:00:02.0547547
    Elapsed Time: 00:00:03.0689716
    Elapsed Time: 00:00:04.0820497
    Elapsed Time: 00:00:05.0963413
    Elapsed Time: 00:00:06.1113915
    Elapsed Time: 00:00:07.1244044
    Elapsed Time: 00:00:08.1396105
    Elapsed Time: 00:00:09.1528952
    Elapsed Time: 00:00:10.1659905
    Elapsed Time: 00:00:11.1800884
    Elapsed Time: 00:00:12.1940009
    Elapsed Time: 00:00:13.2081824
    Elapsed Time: 00:00:14.2223585 …
Run Code Online (Sandbox Code Playgroud)

powershell timer stopwatch

6
推荐指数
2
解决办法
2万
查看次数

在PowerShell v3中运行并行的Invoke-WebRequest作业

在PowerShell中同时运行后台后台作业非常简单,但我似乎无法使其与新(在v3中)cmdlet Invoke-WebRequest一起使用。

我有数千个文件正在通过PowerShell脚本下载。效果很好,但是连续进行需要几天的时间:

for($f=0;$f -lt $urlList.Count;$f++)
{
    $remote = $urlList[$f] + $fileList[$f]
    $local = 'C:\folder\' + $fileList[$f]
    Invoke-WebRequest $remote -Method Get -OutFile $local -UserAgent FireFox
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过利用“ AsJob”方法进行多次尝试,但是它们要么出错,要么完全正常,但是没有保存任何本地文件。这是后者的一个示例:

for($f=0;$f -lt $urlList.Count;$f++)
{
    $remote = $urlList[$f] + $fileList[$f]
    $local = 'C:\folder\' + $fileList[$f]
    $command = "Invoke-WebRequest $remote -Method Get -OutFile $local -UserAgent FireFox"
    Start-Job {Invoke-Expression -Command $command}
}
Get-Job|Wait-Job
Run Code Online (Sandbox Code Playgroud)

输出示例:

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command                  
--     ----            -------------   -----         -----------     --------             -------                  
339    Job339          BackgroundJob   Running       True            localhost            Invoke-Expression …
Run Code Online (Sandbox Code Playgroud)

powershell download background-process cmdlet powershell-3.0

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